help,wx.onBLECharacteristicValueChange 在android上无法接受返回值,notify开启是成功的,有趣的是在iOS下面一切正常,问了好多人,又说开启notify之后立即就调用了write会发生问题,尝试了延迟和重发依旧没有任何返回值,下图是测试返回的结果
····························································································································
上图是在ios设备的截图,能接收到返回值
··································································································
这实在android设备的,没有返回值
···································································································
- 求问技术支持,这问题该如何解决,是否有解决方案,下面贴出我的代码,帮忙看看是不是我写的有问题
Page({ /** 页面的初始数据 */ data: { deviceId: 'FD:76:74:35:99:92' , serviceId: '' , services: [], writeCharacteristicsId: "" , // 可写特征值uuid readCharacteristicsId: "" , // 可读特征值uuid notifyCharacteristicsId: "" , // 通知特征值uuid }, /** 生命周期函数--监听页面加载 */ onLoad: function (opt) { //初始化蓝牙适配器 wx.openBluetoothAdapter({ success: function (res) { console.log( "初始化蓝牙适配器成功" , res); }, fail: function (err) { console.log( "初始化失败" , err) } }) //监听蓝牙适配器状态变化事件 wx.onBluetoothAdapterStateChange(res => { console.log( "状态变化" , res) }) }, start: function () { let that = this ; console.log( '监听设备的连接状态' ); /** 连接设备 */ wx.createBLEConnection({ deviceId: that.data.deviceId, success: function (res) { console.log( '连接设备' , res); /** 监听设备的连接状态 */ wx.onBLEConnectionStateChanged( function (res) { console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`) }); /** 连接成功,后开始获取设备的服务列表 */ wx.getBLEDeviceServices({ deviceId: that.data.deviceId, success: function (res) { console.log( 'device services:' , res); that.setData({ services: res.services, serviceId: res.services[0].uuid }, () => { wx.getBLEDeviceCharacteristics({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, success: function (res) { console.log( 'device getBLEDeviceCharacteristics:' , res.characteristics); for ( var i = 0; i < res.characteristics.length; i++) { if (res.characteristics[i].properties.notify) { that.setData({ notifyCharacteristicsId: res.characteristics[i].uuid }) } if (res.characteristics[i].properties.write) { that.setData({ writeCharacteristicsId: res.characteristics[i].uuid }) } if (res.characteristics[i].properties.read) { that.setData({ readCharacteristicsId: res.characteristics[i].uuid }) } } /** 顺序开发设备特征notifiy */ wx.notifyBLECharacteristicValueChanged({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.notifyCharacteristicsId, state: true , success: function (res) { console.log( 'notifyBLECharacteristicValueChanged success' , res); }, fail: function (res) { console.log( 'notifyBLECharacteristicValueChanged fail :' , res) }, complete: function (res) { } }); /** 回调获取 设备发过来的数据 */ wx.onBLECharacteristicValueChange( function (res) { console.log( 'characteristic value comed:' , res.value); //{value: ArrayBuffer, deviceId: "D8:00:D2:4F:24:17", serviceId: "ba11f08c-5f14-0b0d-1080-007cbe238851-0x600000460240", characteristicId: "0000cd04-0000-1000-8000-00805f9b34fb-0x60800069fb80"} let buffer = res.value let dataView = new DataView(buffer) let dataResult = [] for (let i = 0; i < dataView.byteLength; i++) { dataResult.push(dataView.getUint8(i).toString(16)) } const result = dataResult console.log(result); }); /** 发送开锁命令 */ that.send(); }, fail: function (res) { console.log(res); } }) }); } }) }, fail: function (res) { console.log( '失败' , res); } }) }, /** 发送 数据到设备中 */ send: function () { let that = this , hex = '100031B0000018FFFFFFFF0000008900' ; const typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map( function (h) { return parseInt(h, 16) })) console.log(typedArray); const buffer1 = typedArray.buffer console.log(buffer1) wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.writeCharacteristicsId, value: buffer1, success: function (res) { console.log( "success 指令发送成功" ); console.log(res); /** 读取设备 */ // setTimeout(() => { // wx.readBLECharacteristicValue({ // deviceId: that.data.deviceId, // serviceId: that.data.serviceId, // characteristicId: that.data.readCharacteristicsId, // success: function (res) { // console.log('readBLECharacteristicValue:', res); // } // }) // }, 3000) }, fail: function (res) { console.log(res); } }) }, /** 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** 生命周期函数--监听页面显示 */ onShow: function () { }, /** 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** 生命周期函数--监听页面卸载, 同时断开蓝牙 */ onUnload: function () { let me = this ; wx.closeBLEConnection({ deviceId: me.data.deviceId, success: function (res) { console.log( "蓝牙断开" , res) me.setData({ deviceId: "" , }) } }) }, }) |
在线等。
我来跟进下这个问题
我->设置->帮助与反馈->右上角上传日志,并提供微信号与时间点哈。
--
按这个方式来份日志?
是在微信上上传日志吗?已上传了哦
微信号:bin_garfield 时间点:15:09 - 15:11 分左右
特征值属性支持notification和indication两种,一般比较少这样的case,
Android 小程序对这种情况默认走的都是indication这一种,
可以通过notify接口加参数 type:notification 先规避掉这个问题。
---
ps:之前也有回复过这样的case,可以先搜索下噢,thx.
增加过 type:notification 这个参数,然后是一部分命令有返回,大部分没有,我也好无奈,还有其他的解决方案吗
问题已解决,谢谢管理
问题是什么,解决方案是什么呢?
可以描述一下么..thx
我知道原因了!!!大家查下自己的getBLEDeviceCharacteristics获取特征值里面的read是false还是true,不走回调肯定是false!那就跟你getBLEDeviceServices获取服务选的uuid有关系了,把返回的数组里的uuid都试一遍!我的试到最后一个read才是true,最后走了回调
楼主是怎么解决的呀?
我开发中也出现了这个问题 谢谢楼主的反馈。
我这边还有一个问题,我看到楼主也是开锁关锁的。
在 onBLECharacteristicValueChange 我接收不到手动关锁后 发出的蓝牙广播,想问一下楼主关锁这里是怎么做的。
问题已解决
楼主最后怎么解决的啊?能说一下吗?是添加延迟执行回调还是怎么?
你可以尝试下 把send() 放在 notifyBLECharacteristicValueChange 成功回调里面,做一点点延时再执行。
试过了,还是不行