- wx.onBLECharacteristicValueCha 无回调,回调参数为undefined?
我贴出代码,这个问题卡了快一周了很多办法都尝试了,还是不能解决。 问题前景:与蓝牙设备连接进行指令交互 /** * 连接低功耗蓝牙设备。 */ BLEConn: function() { const that = this; wx.createBLEConnection({ deviceId: that.data.deviceId, success: function(res) { console.log('createBLEConnection success'); wx.stopBluetoothDevicesDiscovery({ success: function(res) { console.log('stopBluetoothDevicesDiscovery success'); } }) that.getBLEDeviceServices(); that.data.connected = true; }, fail: function(res) { console.log('createBLEConnection fail'); } }) }, /** * 获取蓝牙设备所有服务(service) */ getBLEDeviceServices() { const that = this; wx.getBLEDeviceServices({ deviceId: that.data.deviceId, success: function(res) { console.log('getBLEDeviceServices success'); for (let i = 0; i < res.services.length; i++) { if (res.services[i].uuid.indexOf("BA11F08C") != -1) { that.setData({ serviceId : res.services[i].uuid, }) setTimeout( function() { that.getBLEDeviceCharacteristics(res.services[i].uuid); },1500); } } }, fail: function(res) { console.log('getBLEDeviceServices fail'); } }) }, /** * 获取蓝牙设备某个服务中所有特征值(characteristic) */ getBLEDeviceCharacteristics(serviceId) { const that = this; wx.getBLEDeviceCharacteristics({ deviceId: that.data.deviceId, serviceId: serviceId, success: function(res) { console.log('getBLEDeviceCharacteristics success'); let item = res.characteristics for (let i = 0; i < item.length; i++) { if (item[i].uuid.indexOf("CD01") != -1) { that.setData({ cd01 : item[i].uuid, }) } if (item[i].uuid.indexOf("CD02") != -1) { that.setData({ cd02: item[i].uuid, }) } if (item[i].uuid.indexOf("CD03") != -1) { that.setData({ cd03: item[i].uuid, }) } if (item[i].uuid.indexOf("CD04") != -1) { that.setData({ cd04: item[i].uuid, }) } if (item[i].properties.write && writeFlag) { that.data.writeCha = item[i].uuid; that.data.writeSer = serviceId; } } wx.onBLECharacteristicValueChange(function (msg) { if (msg.characteristicId.indexOf("CD01") != -1) { console.log(`蓝牙设备发送的指令:${ab2hex(msg.value)}`) console.log('----------------------') } if (msg.characteristicId.indexOf("CD04") != -1) { console.log(`蓝牙设备发送的数据:${ab2hex(msg.value)}`) console.log('----------------------') let temp = ab2hex(msg.value).slice(6, 10); console.log(`截取后的temp:${temp}`) if (that.data.temperature && temp.indexOf('0e') === 0) { console.log(`设备发送的数据转化为温度:${theTemperature(temp)}`); } } }) wx.notifyBLECharacteristicValueChange({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.cd01, state: true, success: function (res) { console.log(`${that.data.cd01}:notifyBLECharacteristicValueChange success`); }, fail: function (res) { console.log('notifyBLECharacteristicValueChange fail'); } }) wx.notifyBLECharacteristicValueChange({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.cd02, state: true, success: function (res) { console.log(`${that.data.cd02}:notifyBLECharacteristicValueChange success`); }, fail: function (res) { console.log('notifyBLECharacteristicValueChange fail'); } }) wx.notifyBLECharacteristicValueChange({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.cd03, state: true, success: function (res) { console.log(`${that.data.cd03}:notifyBLECharacteristicValueChange success`); }, fail: function (res) { console.log('notifyBLECharacteristicValueChange fail'); } }) wx.notifyBLECharacteristicValueChange({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.cd04, state: true, success: function (res) { console.log(`${that.data.cd04}:notifyBLECharacteristicValueChange success`); }, fail: function (res) { console.log('notifyBLECharacteristicValueChange fail'); } }) }, fail: function(res) { console.log('getBLEDeviceCharacteristics fail '); } }) }, /** * 写操作 */ sendMsg:function () { const that = this let buffer = hexStringToArrayBuffer(that.data.sendStr); wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.writeSer, characteristicId: that.data.writeCha, value: buffer, success: function (res) { console.log('writeBLEValue success', res.errMsg) console.log(`写入的数据:${ab2hex(buffer)}`) }, fail: function (res) { console.log('writeBLEValue fail', res.errMsg) } }) },
2020-03-09 - wx.onBLECharacteristicValueChange函数接收不到设备传回的数据和指令?
在与设备对接时需要先进行密码校验,我在开启使能notify后,发送密码指令。设备在接收到之后应该会回一个密码对错的指令并自动上传数据。现在是这个wx.onBLECharacteristicValueChange 接收不到返回的数据 [图片][图片][图片]
2020-03-05 - wx.onBLECharacteristicValueChange 怎么样可以一直获取设备的值?
api:wx.onBLECharacteristicValueChange微信版本号:最新 基础库版本:最新 与一个产品在数据交互过程中只能获取一次设备的值,获取不到之后改变的值 /** * 获取该服务所有特征值 */ getCharacteristics(serviceId) { const that = this wx.getBLEDeviceCharacteristics({ deviceId: deviceId, serviceId: serviceId, success: function(res) { for (let i = 0; i < res.characteristics.length; i++) { if (res.characteristics[i].properties.write && writeFlag) { //写操作 that.writeBLEValue(serviceId, res.characteristics[i].uuid) writeFlag = false } if (res.characteristics[i].properties.notify && notifyFlag) { //开启notify操作 that.notifyBLEValue(serviceId, res.characteristics[i].uuid) notifyFlag = false } if (res.characteristics[i].properties.read && readFlag) { //读操作 that.readBLEValue(serviceId, res.characteristics[i].uuid) readFlag = false } } }, }) this.onCharacteristic() },
2020-03-02