const app = getApp() //main Page({ //Data data: { OnlyReadOne: null, // OnlyOneCheackStateMents receiveText: '', // explain ReciveMesage connectedDeviceId: '',// BLE_List_ID service_uuid: '', // BLE_UUID workstate: false, // 工作状态 target: 1, // 强度 current: 5, // 时间 mode: 1, // 模式 Rworkstate: 0, // 工作状态 Rtarget: 0, // 强度 Rcurrent1: 0, // 分 Rcurrent2: 0, // 秒 Rmode: 0, // 模式 RBattery: 0, // 电量 LastSendingTime: 0, timeInterval:1000, DisplayBLEconflag: 0, //show ble offline tisrc: "/subpagesTwo/images/zmdevice/target1.png", tisrc1: "/subpagesTwo/images/zmdevice/target1.png", tisrc2: "/subpagesTwo/images/zmdevice/target.png", cisrc: "/subpagesTwo/images/zmdevice/current.png", cisrc1: "/subpagesTwo/images/zmdevice/current1.png", cisrc2: "/subpagesTwo/images/zmdevice/current.png", tcisc: 1, ccisc: 0, }, //响应强度按键 handleClick(e) { var that = this; const value = e.currentTarget.dataset.value; this.setData({ mode: value, }); if (this.data.workstate) { this.setData({ workstate: false }); setTimeout(() => { this.setData({ workstate: true }); that.setDevdata(that,true); }, 500); } // 这里可以添加其他逻辑 console.log('当前值:', value); }, setDevdata(that,flag=false) { var timestamp = new Date().getTime(); if ( !flag&&(that.data.LastSendingTime !== 0 && timestamp - that.data.LastSendingTime < that.data.timeInterval)) { console.log('发送间隔小于500ms') return } that.data.LastSendingTime = timestamp // Makeup Inquire Message // 0B 4D(帧头) // 01(设备id) // 10(写) // 00 70(命令字) // 00 02(命令个数) // 04(数据个数) // WorkState(工作状态) Vvalue(强度) Tvalue(时间) 模式(mode) // CRCL CRCH (校验) // 1C 0D(帧尾) var temp_data_send = new Array(0x01, 0x10, 0x00, 0x70, 0x00, 0x02, 0x04, that.data.workstate, that.data.current, that.data.target, that.data.mode); let temp_crc = app.CRC16(temp_data_send) temp_data_send.push(temp_crc[0]); temp_data_send.push(temp_crc[1]); // Add HeadTail var senddata = new ArrayBuffer(17); let bufferview = new DataView(senddata); bufferview.setUint8(0, 0x0b); bufferview.setUint8(1, 0x4d); for (var i = 0; i < 13; i++) { bufferview.setUint8([i + 2], temp_data_send[i]); } bufferview.setUint8(15, 0x1c); bufferview.setUint8(16, 0x0d); let RecoedData = senddata // console.log("==========================", that.data.target) wx.writeBLECharacteristicValue({ deviceId: that.data.connectedDeviceId, serviceId: that.data.service_uuid, characteristicId: "0003CDD2-0000-1000-8000-00805F9B0131", value: RecoedData, success: function (res) { console.log('查询发送') }, fail: function (err) { console.log(err, '查询发送失败') } }) }, // init onLoad: function (options) { var that = this; that.setData({ connectedDeviceId: options.connectedDeviceId, OnlyReadOne: null, service_uuid: options.serviceId, OnlyReadOne: setInterval(function () { that.setDevdata(that); }, that.data.timeInterval), }) // 检测连接 wx.onBLEConnectionStateChange(function (res) { console.log(res.connected) if (res.connected == 0 && that.data.DisplayBLEconflag == 0) { wx.showModal({ title: '提示', content: '设备已断开', showCancel: false, success: function (res) { that.setData({ connected: false, workstate: 0 }) } }) } that.setData({ connected: res.connected, }) }) wx.onBLECharacteristicValueChange(function (res) { console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`) that.setData({ receiveText: app.ab2hex(res.value) }) let seceiver_buff = new Uint8Array(res.value); for (let i = 0; i < seceiver_buff.length; i++) { console.log(seceiver_buff[i].toString(16).padStart(2, '0') + ' '); } //开始处理蓝牙返回指令 for (var i = 0; i < (seceiver_buff.length - 10); i++) { if (seceiver_buff[i] == 0x0b) { if ((seceiver_buff[i + 1] == 0x4d) && (seceiver_buff[i + 2] == 0x01) && (seceiver_buff[i + 3] == 0x10) && (seceiver_buff[i + 4] == 0x00) && (seceiver_buff[i + 5] == 0x70) && (seceiver_buff[i + 6] == 0x00) && (seceiver_buff[i + 7] == 0x03) && (seceiver_buff[i + 8] == 0x06)) { if ((seceiver_buff[i + 17] == 0x1C) && (seceiver_buff[i + 18] == 0x0D)) { let dataToVerify = seceiver_buff.subarray(i + 2, i + 15); //索引2-14 let calculatedCRC = app.CRC16(dataToVerify); if (calculatedCRC[0] == seceiver_buff[i + 15] && calculatedCRC[1] == seceiver_buff[i + 16]) { that.setData({ connected: true, Rworkstate: seceiver_buff[i + 10], // 工作状态 Rtarget: seceiver_buff[i + 11], // 强度 target: seceiver_buff[i + 11], Rcurrent1: seceiver_buff[i + 12], // 分 Rcurrent2: seceiver_buff[i + 13], // 秒 Rmode: seceiver_buff[i + 9], // 模式 RBattery: seceiver_buff[i + 14], // 电量 }) // console.log("收到数据:"); // console.log("设备模式:",seceiver_buff[i+9]); // console.log("设备状态:",seceiver_buff[i+10]); // console.log("设备挡位:",seceiver_buff[i+11]); break; } } else { continue } } else { continue } } else { continue } } }) }, //开始停止 Send: function () { var that = this if (that.data.workstate == false) { that.setData({ workstate: true }) } else { that.setData({ workstate: false }) } this.setDevdata(that,true); }, // 强度 VoltagecheckboxChange: function (e) { if (e.detail.value != "") { this.setData({ tcisc: 1, ccisc: 0, tisrc: this.data.tisrc1, cisrc: this.data.cisrc2, }); } else { this.setData({tcisc: 0, tisrc: this.data.tisrc2}); } this.setDevdata(this,true); }, // 时间 currentcheckboxChange: function (e) { if (e.detail.value != "") { this.setData({ ccisc: 1, tcisc: 0, cisrc: this.data.cisrc1, tisrc: this.data.tisrc2, }); } else { this.setData({ccisc: 0, cisrc: this.data.cisrc2}); } this.setDevdata(this,true); }, //左键 leftTouchEnd: function (event) { var that = this; if (this.data.workstate == true) { if (that.data.tcisc == 1) { if (that.data.target > 1) { that.setData({ target: that.data.target - 1, }); this.setDevdata(that,true); } else { console("强度越界"); return; } } else if (that.data.ccisc == 1) { if (that.data.current > 5) { that.setData({ current: that.data.current - 5, }); this.setDevdata(that,true); } else { console.log("时间越界"); return; } } else { console.log("选择出错"); return; } } else { wx.showModal({ title: '提示', content: '设备未开始工作', showCancel: false, success: function (res) { that.setData({}) } }) } }, //右键 rightTouchEnd: function (event) { var that = this; if (this.data.workstate == true) { if (that.data.tcisc == 1) { if (that.data.target < 30) { that.setData({ target: that.data.target + 1, }); this.setDevdata(that,true); } else { console.log("强度越线"); return; } } else if (that.data.ccisc == 1) { if (that.data.current < 20) { that.setData({ current: that.data.current + 5, }); this.setDevdata(that,true); } else { console.log("时间越线"); return; } } else { console.log("选择失败"); return; } } else { wx.showModal({ title: '提示', content: '设备未开始工作', showCancel: false, success: function (res) { that.setData({}) } }) } }, onReady: function () { }, onShow: function () { var that = this; if (that.data.connected == false) { clearInterval(this.data.OnlyReadOne); } }, onHide: function () { }, onUnload: function () { this.data.DisplayBLEconflag = 1, // 关查询定时 clearInterval(this.data.OnlyReadOne) // 关蓝牙连接 wx.closeBLEConnection({ deviceId: this.data.connectedDeviceId, success(res) { console.log(res) } }) }, // Back to Search handlerGobackClick(delta) { wx.navigateBack({ delta: 2 }); }, })