我开发中也出现了这个问题 谢谢楼主的反馈。 我这边还有一个问题,我看到楼主也是开锁关锁的。 在 onBLECharacteristicValueChange 我接收不到手动关锁后 发出的蓝牙广播,想问一下楼主关锁这里是怎么做的。
wx.onBLECharacteristicValueChange 在安卓无回调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: [代码][代码]""[代码][代码], })[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码] [代码]})[代码] 在线等。
2019-05-09建议你先去看一下蓝牙和ble的区别
用蓝牙API获取不到设备使用官方提供的模板,两台手机就在旁边,搜索不到对方的蓝牙,官方提供的模板里面吧没有name的都去掉啦,如果没去掉会看到一群位置设备,是搜索不到手机蓝牙吗
2019-04-29