// pages/operation/deviceTest.js // deviceId:'679F9ABF-3C57-1C70-38D8-030C8DBEAB5B' const util = require("../../utils/util") Page({ /** * 页面的初始数据 */ data: { deviceId: '', deviceState: 0, serviceId: '', characteristicId: '', isloading: false, scanCode: '', instructType: 1, // 1发送密码指定;2发送出货指令;3发送出货结果确认; instructReceive: '', instructSend: '', gridNum: 20 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) {}, scanCodeEvent() { var that = this; wx.scanCode({ onlyFromCamera: true, // 只允许从相机扫码 success(res) { console.log("扫码成功:" + JSON.stringify(res)) that.setData({ scanCode: res.result, isloading: true }) that.loadblue() } }) }, inputConfirm(e){ var that = this console.log(e) if(e.target.dataset.type){ if(!e.target.dataset.val){ wx.showToast({ title: '请输入设备号', duration: 3000 }) return } that.setData({ scanCode: e.target.dataset.val, isloading: true }) }else{ if(!e.detail.value){ wx.showToast({ title: '请输入设备号', duration: 3000 }) return } that.setData({ scanCode: e.detail.value, isloading: true }) } that.loadblue() }, loadblue(){ var that = this wx.openBluetoothAdapter({ success: function (res) { console.log('初始化蓝牙适配器成功' + JSON.stringify(res)) wx.startBluetoothDevicesDiscovery({ services: [], success: function (res) { console.log('搜索设备') console.log(res) //5s内未搜索到设备,关闭搜索,关闭蓝牙模块 setTimeout(function () { wx.getBluetoothDevices({ success: function (res) { let i = 0; while (res.devices[i]) { console.log(i); console.log(res.devices[i].name, res.devices[i].deviceId); // if (res.devices[i].deviceId == that.data.scanCode) { // that.setData({ // deviceId: res.devices[i].deviceId // }) // } // scanCode LY808 console.log("scanCode:" + that.data.scanCode) if (that.data.scanCode != "" && res.devices[i].name.indexOf(that.data.scanCode) > -1) { that.setData({ deviceId: res.devices[i].deviceId }) } i++; } if (!that.data.deviceId) { that.setData({ isloading: false }); wx.showToast({ title: '搜索设备超时', duration: 3000 }) console.log("搜索设备超时"); wx.stopBluetoothDevicesDiscovery({}); //关闭搜索 wx.closeBluetoothAdapter({}); //关闭蓝牙模块 } else { wx.stopBluetoothDevicesDiscovery({}); //关闭搜索 that.connectBLEDevice(); //去连接蓝牙设备 } } }) }, 10000); }, }) }, fail: function () { that.setData({ isloading: false }); util.toast('请打开蓝牙!') }, complete: function () { console.log('初始化蓝牙适配器完成') } }) }, closediscovery: function () { var that = this wx.closeBLEConnection({ deviceId: that.data.deviceId, success: (res) => { that.setData({ deviceId: '', deviceState: -1 }) console.log(res) } }) }, connectBLEDevice() { var that = this wx.createBLEConnection({ deviceId: that.data.deviceId, success: function (res) { console.log(res); wx.getBLEDeviceServices({ deviceId: that.data.deviceId, success: function (res) { console.log(res.services); let i = 0; while (res.services[i]) { if (res.services[i].uuid.indexOf('FFE0') > -1) { that.setData({ serviceId: res.services[i].uuid }) } i++; } if (that.data.deviceId.length > 0) { wx.getBLEDeviceCharacteristics({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, success: function (res) { let i = 0; while (res.characteristics[i]) { if (res.characteristics[i].uuid.indexOf('FFE1') > -1) { that.setData({ characteristicId: res.characteristics[i].uuid }) } i++; } if (that.data.characteristicId.length > 0) { that.startnotify() setTimeout(function () { let pwd = that.strToHexCharCode('888888') that.setData({ instructType: 1, instructSend: "FF0401" + pwd + "EE", instructReceive: "ee040101ff" }) that.startwrite(that.data.instructSend) }, 2000) } }, fail: function (res) { that.setData({ deviceState: -1, isloading: false }) } }) } }, fail: function (res) { that.setData({ deviceState: -1, isloading: false }) } }) }, fail: function (res) { that.setData({ deviceState: -1, isloading: false }) } }) }, startnotify: function () { var that = this wx.notifyBLECharacteristicValueChange({ state: true, deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicId, success: function (res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) }, fail: function (res) { util.toast(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)) var resStr = ab2hex(res.value) switch (that.data.instructType) { case 1: if (resStr == that.data.instructReceive) { that.setData({ deviceState: 1, isloading: false }) } break; case 2: // var total = resStr.length var num = parseInt(resStr.substr(22, 2)) if (num == 1) { var resInstruct = resStr.substr(24, 2) if (resInstruct == "00") { util.toast('当前格子已打开!'); // 回复蓝牙模块确认 let instruct = resStr.substr(6, 16) instruct = "FF0202" + instruct + "EE" that.startwrite(instruct) } else if (resInstruct == "01") { util.toast('当前格子关闭!'); } } if (resStr == that.data.instructReceive) { let instruct = "FF0202523F33F3D64455FFEE"; that.setData({ instructType: 3, instructSend: instruct, instructReceive: '' }) that.startwrite(that.data.instructSend) } break; default: break; } }) }, startwrite: function (value_initial) { var that = this if (!value_initial) { value_initial = "FF 04 01 38 38 38 38 38 38 EE"; } var write_array = []; //用于发送数据的数组。 /* 判断是否存在空格 */ if (value_initial.indexOf(' ') > 0) { var value_initial = that.splitStr(value_initial, ' '); //存在空格时 console.log('删除掉空格', value_initial); } else { var value_initial = value_initial; //不存在空格时 } /* 判断字节是否超过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); //调用数据发送函数 // } }, //写入数据 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: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicId, value: buffer, success: function (res) { console.log('写入成功', res); }, fail: function (res) { console.log('调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicId, value: buffer, success: function (res) { console.log('写入成功', res); }, fail: function (res) { console.log('第2次调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicId, value: buffer, success: function (res) { console.log('写入成功', 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; }, openGrate(e) { var that = this if (that.data.deviceState == 0) { util.toast("未连接设备!") return } let goodsRoad = '' switch (e.currentTarget.dataset.index + 1) { case 1: goodsRoad = '01' break; case 2: goodsRoad = '02' break; case 3: goodsRoad = '03' break; case 4: goodsRoad = '04' break; case 5: goodsRoad = '05' break; case 6: goodsRoad = '06' break; case 7: goodsRoad = '07' break; case 8: goodsRoad = '08' break; case 9: goodsRoad = '09' break; case 10: goodsRoad = '0A' break; case 11: goodsRoad = '0B' break; break; case 12: goodsRoad = '0C' break; case 13: goodsRoad = '0D' break; case 14: goodsRoad = '0E' break; case 15: goodsRoad = '0F' break; case 16: goodsRoad = '10' break; case 17: goodsRoad = '11' break; case 18: goodsRoad = '12' break; case 19: goodsRoad = '13' break; case 20: goodsRoad = '14' break; case 21: goodsRoad = '15' break; case 22: goodsRoad = '16' break; case 23: goodsRoad = '17' break; case 24: goodsRoad = '18' break; default: goodsRoad = '01' break; } var clientid = util.generateClientID() let strclientid = that.strToHexCharCode(clientid) let instruct = "FF 02 01 " + strclientid + " 01 " + goodsRoad + " EE"; strclientid = strclientid.toLowerCase() that.setData({ instructType: 2, instructSend: instruct, instructReceive: 'ee0201' + strclientid.toLowerCase() + '0100ff', instructConfirm: 'ee0201' + strclientid.toLowerCase() + 'ff', }) that.startwrite(that.data.instructSend) }, strToHexCharCode(str) { if (str === "") return ""; var hexCharCode = []; hexCharCode.push("0x"); for (var i = 0; i < str.length; i++) { hexCharCode.push((str.charCodeAt(i)).toString(16)); } return hexCharCode.join("").replace('0x', ''); }, oneKeyOpen() { var that = this if (that.data.deviceState == 0) { util.toast("未连接设备!") return } let goodsRoad = '' let res = '' for (let index = 1; index <= that.data.gridNum; index++) { goodsRoad += (Array(2).join(0) + index).slice(-2) res += '00' } var clientid = util.generateClientID() let strclientid = that.strToHexCharCode(clientid) let instruct = "FF 02 01 " + strclientid + that.data.gridNum + goodsRoad + " EE"; strclientid = strclientid.toLowerCase() that.setData({ instructType: 2, instructSend: instruct, instructReceive: 'ee0201' + strclientid.toLowerCase() + that.data.gridNum + res + 'ff' }) that.startwrite(that.data.instructSend) }, inputedit: function (e) { let _this = this; let dataset = e.currentTarget.dataset; let value = e.detail.value; let name = dataset.name; _this.data[name] = value; _this.setData({ [name]: _this.data[name] }); }, })