//index.js //获取应用实例 const app = getApp() var deviceId; var i = 0; var serviceId = []; var characteristicId = []; var aleApi = require('../../utils/antsBleApi.js'); Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), sendData: 'FF01020AEE', writeDatas: '' }, //事件处理函数 bindViewTap: function () { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, getUserInfo: function (e) { console.log(e) app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, inArray(arr, key, val) { for (let i = 0; i < arr.length; i++) { if (arr[i][key] === val) { return i; } } return -1; }, // ArrayBuffer转16进度字符串示例 ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); }, myStringtoHex(str) { str = str.toLowerCase(); let newBuffer = new ArrayBuffer(str.length / 2); let hexBuffer = new Uint8Array(newBuffer, 0); let h = 0, l = 0; for (let i = 0; i < str.length / 2; i++) { h = str.charCodeAt(2 * i); l = str.charCodeAt(2 * i + 1); if (48 <= h && h <= 57) { h = h - 48; } else { h = h - 97 + 10; } if (48 <= l && l <= 57) { l = l - 48; } else { l = l - 97 + 10; } hexBuffer[i] = h * 16 + l; } return hexBuffer; }, // 初始化蓝牙模块 openadapter() { var that = this wx.openBluetoothAdapter({ success: function (res) { console.log('初始化蓝牙适配器成功' + JSON.stringify(res)) that.msg = '初始化蓝牙适配器成功' wx.showModal({ title: '蓝牙适配情况', content: '初始化蓝牙适配器成功' }) }, fail: function () { that.msg = '初始化蓝牙适配器失败' wx.showModal({ title: '蓝牙适配情况', content: '蓝牙适配失败,请检查手机蓝牙和定位功能是否打开' }) }, complete: function (res) { console.log(res) console.log('初始化蓝牙适配器完成') } }) }, closeadapter: function () { wx.closeBluetoothAdapter({ success: function (res) { console.log(res, "success") }, fail: function (res) { console.log(res, "fail") }, }) // wx.getBluetoothAdapterState({ // complete: function (res) { // console.log("currentstate:", res) // } // }) }, opendiscovery: function () { wx.startBluetoothDevicesDiscovery({ services: [], success: function (res) { console.log(res) }, fail: function (res) { console.log(res, "fail") }, }) }, closediscovery: function () { wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) }, fail: function (res) { console.log(res, "fail") }, }) }, getdevice: function () { function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } wx.getBluetoothDevices({ success: function (res) { console.log(res) i = 0; while (res.devices[i]) { console.log(i); console.log(res.devices[i].name, res.devices[i].deviceId); if (res.devices[i].name == 'LY808') { deviceId = res.devices[i].deviceId; console.log(deviceId); } i++; } } }) }, getconnecteddevice: function () { wx.getConnectedBluetoothDevices({ //services:[], success: function (res) { console.log(res) } }) }, connecteddevice: function () { wx.createBLEConnection({ deviceId: deviceId, success: function (res) { console.log(res); }, }) }, getservice: function () { wx.getBLEDeviceServices({ deviceId: deviceId, success: function (res) { console.log(res.services); i = 0; while (res.services[i]) { serviceId[i] = res.services[i].uuid; console.log(serviceId[i]); i++; } }, }) }, getcharacteristics: function () { wx.getBLEDeviceCharacteristics({ deviceId: deviceId, serviceId: serviceId[0], success: function (res) { console.log('device getBLEDeviceCharacteristics:', res.characteristics) } }) wx.getBLEDeviceCharacteristics({ deviceId: deviceId, serviceId: serviceId[0], success: function (res) { i = 0; while (res.characteristics[i]) { characteristicId[i] = res.characteristics[i].uuid; console.log(characteristicId[i]); i++; } } }) }, startread: function () { wx.readBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId[0], characteristicId: characteristicId[1], success: function (res) { console.log('readBLECharacteristicValue:', res.errCode) } }) }, startnotify: function () { wx.notifyBLECharacteristicValueChange({ state: true, deviceId: deviceId, serviceId: serviceId[0], characteristicId: characteristicId[1], success: function (res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) } }) function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } wx.onBLECharacteristicValueChange(function (res) { console.log('characteristic value :', res.value) console.log('characteristic value comed:', ab2hex(res.value)) }) }, startwrite: function () { var that = this let buffer = new ArrayBuffer(15) let dataView = new DataView(buffer) dataView.setUint8(1, 'FF') dataView.setUint8(2, '02') dataView.setUint8(3, '01') dataView.setUint8(4, '523F33F3D64455FF') dataView.setUint8(5, '02') dataView.setUint8(6, '05') dataView.setUint8(7, '06') dataView.setUint8(8, 'EE') // let data = that.data.sendData.split(',') // let dataBuffer = new ArrayBuffer(data.length) // let dataView = new DataView(dataBuffer) // for (let j = 0; j < data.length; j++) { // dataView.setUint8(j, '0x' + data[j]) // } // for (let i = 0; i < dataView.byteLength; i++) { // var writeData = '0x' + dataView.getUint8(i).toString(16) // that.data.writeDatas = that.data.writeDatas + '\n' + writeData // } // var hex = "FF0401383838383838EE" //要发送的信息 // console.log('要发送的信息是:'+hex) // var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { // return parseInt(h, 16) // })) // console.log(typedArray) // var buffer1 = typedArray.buffer // wx.writeBLECharacteristicValue({ // deviceId: deviceId, // serviceId: serviceId[0], // characteristicId: characteristicId[1], // value: buffer1, // success: function (res) { // console.log('writeBLECharacteristicValue success', res.errMsg) // }, // fail(res){ // console.log('写入失败', res.errMsg) // } // }) var value_initial_1 = "FF 04 01 38 38 38 38 38 38 EE"; var write_array = []; //用于发送数据的数组。 /* 判断是否存在空格 */ if (value_initial_1.indexOf(' ') > 0) { var value_initial = that.splitStr(value_initial_1, ' '); //存在空格时 console.log('删除掉空格', value_initial); } else { var value_initial = value_initial_1; //不存在空格时 } /* 判断字节是否超过20字节 */ if (value_initial.length > 20) { //当字节超过20的时候,采用分段发送 //选择16进制发送时 var value_initial_exceed = value_initial; //将输入框的值取过来,方便循环 var value_initial_average = Math.ceil(value_initial_exceed.length / 20); //将value_initial_exceed的长度除以20,余数再向上取一,确定循环几次 console.log('需要循环的次数', value_initial_average); for (var i = 0; i < value_initial_average; i++) { if (value_initial_exceed.length > 20) { var value_initial_send = value_initial_exceed.slice(0, 20); //截取前20个字节 console.log('截取到的值', value_initial_send); value_initial_exceed = value_initial_exceed.substring(20); //value_initial_exceed替换为取掉前20字节后的数据 write_array.push(value_initial_send); //将所有截取的值放在一个数组 } else { write_array.push(value_initial_exceed); } } console.log('write_array数组', write_array); write_array.map(function (val, index) { setTimeout(function () { var value_set = val; console.log('value_set', value_set); var write_function = that.write(value_set); //调用数据发送函数 }, index * 100) }); } else { //当字节不超过20的时候,直接发送 var value = value_initial; var write_function = that.write(value); //调用数据发送函数 } }, openDoor: function () { var that = this // progressUtils.showLoding(); aleApi.sendCommand({ deviceId: "xx:xx:xx:xx:xx:xx", sendCommend: "xxxxxxxxx", onSuccessCallBack: function onSuccessCallBack(result) { wx.showToast({ title: result, icon: '', image: '', duration: 1500, }) console.log('result:' + result); } }); }, //写入数据 write: function (str) { var that = this; var value = str; console.log('value', value); /* 将数值转为ArrayBuffer类型数据 */ var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })); var buffer = typedArray.buffer; wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId[0], characteristicId: characteristicId[1], value: buffer, success: function (res) { console.log('数据发送成功', res); }, fail: function (res) { console.log('调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId[0], characteristicId: characteristicId[1], value: buffer, success: function (res) { console.log('第2次数据发送成功', res); }, fail: function (res) { console.log('第2次调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId[0], characteristicId: characteristicId[1], value: buffer, success: function (res) { console.log('第3次数据发送成功', res); }, fail: function (res) { console.log('第3次调用失败', res); } }); } }); } }); }, /* 去除输入框输入的值中的空格 */ splitStr: function (str, s) { var newStr = ""; var strArray = str.split(s); for (var i = 0; i < strArray.length; i++) { newStr += strArray[i]; } return newStr; }, getBinaryData: function (message) { //将数据转为二进制数组 let buffer = new ArrayBuffer(6); let dataView = new DataView(buffer); var numTitle = 0; for (var i = 0; i < message.length; i = i + 2) { var numStr16 = message.substr(i, 2); var num = parseInt(numStr16, 16); dataView.setUint8(numTitle, num); numTitle++; } return buffer; } })