收藏
回答

onBLECharacteristicValueChange无回调,弄了好久好久

请大佬们帮我看看,就是发送给蓝牙数据后无法接受到回调值,之前是用app对接蓝牙设备的(app上设备正常回调),现在要应用到小程序上,数据发送了,但一直没有接收到回调的值,需要回调的值进行解密。以下是网下的一个dome,做了一些调整,弄了一个星期了求大佬大神指点




//发送

lanya8: function () {

var that = this;

// 这里的回调可以获取到 write 导致的特征值改变

var buf = new ArrayBuffer(16)

var dataView = new DataView(buf)

wx.request({

url: globalData.serveUrl + '*******.do',//后台加密返回请求

success: function (data) {

var str = data.data.infoData;

var arr = str.split(",");

console.log(arr);

for (var i = 0; i < arr.length; i++) {

dataView.setInt8(i, arr[i]);

}

console.log('str', buf);//例如["12", "88", "68", "118", "52", "16", "1", "0", "0", "-13", "-65", "-28", "114"]

wx.writeBLECharacteristicValue({

deviceId: that.data.connectedDeviceId,

// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取

serviceId: that.data.notifyServicweId,

// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取

characteristicId: that.data.notifyCharacteristicsId,

// 这里的value是ArrayBuffer类型

value: buf,

success: function (res) {

console.log('writeBLECharacteristicValue success', res.errMsg)   //这里发送成功,公司的蓝牙设备显示了绿灯代表能接收到

}

})

},

fail: function (res) {

console.log(that.data.notifyServicweId);

console.log(that.data.notifyCharacteristicsId);

console.log('writeBLECharacteristicValue Error', res.errMsg)

}

})

},

//启用低功耗蓝牙设备特征值变化时的 notify 功能

lanya9: function () {

var that = this;

wx.notifyBLECharacteristicValueChange({

type: "indication",

state: true, // 启用 notify 功能

// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取

deviceId: that.data.connectedDeviceId,

// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取

serviceId: that.data.notifyServicweId,

// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取

characteristicId: that.data.notifyCharacteristicsId,

success: function (res) {

console.log('notifyBLECharacteristicValueChange success', res.errMsg);//这里开启成功  返回了ok

that.lanya10();//执行监听


},

fail: function (res) {

console.log(that.data.notifyServicweId);

console.log(that.data.notifyCharacteristicsId);

console.log('notifyBLECharacteristicValueChangeError', res.errMsg)

},

})

},

//接收消息

lanya10: function () {

var that = this;

// 必须在这里的回调才能获取

wx.onBLECharacteristicValueChange(function (characteristic) {

console.log("66666666666");

let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16)).slice(-2)).join('');

console.log("hexhexhexhexhexhex:" + hex)

wx.request({

url: globalData.serveUrl + '***.do',

data: { hexString: hex },

method: "POST",

header: {

'content-type': 'application/x-www-form-urlencoded'

},

success: function (data) {

var res = data.data.infoData;

console.log(res)

that.setData({

jieshou: res

})

},

fail: function (res) {

console.log('onBLECharacteristicValueChangeError', res.errMsg)

}

})

})

}


最后一次编辑于  2018-10-23
回答关注问题邀请回答
收藏

4 个回答

  • 好久不见
    好久不见
    2019-12-04

    楼主,这个问题是不是因为获取硬件的特征值里面的read是false,然后就无法得到回调呀?

    2019-12-04
    有用
    回复 6
    • 2019-12-05
      就是你们产品有个共同的特征,要拿到正确的特征才能回调
      2019-12-05
      回复
    • 好久不见
      好久不见
      2019-12-05回复
      我看了有两个特征值,然后获取到正确的那一个特征值,但是发现那个特征值的一些属性,read 是false ,write是true,notify是true,是不是回调没有是因为read的原因呢
      2019-12-05
      回复
    • 2019-12-05回复好久不见
      是的 要read为true 你的只有两个特征吗
      2019-12-05
      回复
    • 2019-12-05回复好久不见
      看你的特征只能写不能读
      2019-12-05
      回复
    • 好久不见
      好久不见
      2019-12-05回复
      对的,只有两个,而且两个都是 read 是 false
      2019-12-05
      回复
    查看更多(1)
  • 王牌
    王牌
    2019-10-12

    求问 我也是到这步了但是没有任何回调,前面的几步都连接正常,到这一步却没成功,请楼主咋解决的,可否详细点说呢 qq731358969

    2019-10-12
    有用
    回复
  • 2018-11-05

    手机型号是啥?我这里notify是可以的,可能是没碰到你的那个手机

    2018-11-05
    有用
    回复 6
    • 2018-11-05

      谢谢你的回答,已经解决了。要跟硬件沟通拿正确的特征。有错误的小伙伴可以尝试下是不是这个问题

      2018-11-05
      回复
    • 好久不见
      好久不见
      2019-12-04回复
      楼主是不是硬件的特征值里面的read属性是false导致的问题啊
      2019-12-04
      回复
    • 2020-01-16
      你是因为特征值的read属性为false才导致notify接受不到信息吗
      2020-01-16
      回复
    • 莫问归期
      莫问归期
      2021-09-22回复好久不见
      你好,请问你的问题解决了吗?
      2021-09-22
      回复
    • 莫问归期
      莫问归期
      2021-09-22
      我这里拿到了硬件的notify的特征值,但是这个特征值里的read为false,一直监听不到硬件返回来的value是因为read为false吗?
      2021-09-22
      回复
    查看更多(1)
  • 2018-10-23

    没有人愿意帮助一下吗

    2018-10-23
    有用
    回复
登录 后发表内容