收藏
回答

notifyBLECharacteristicValueChange() 监听接收不到数据问题?

.bin拆包后共拆192个数据包,蓝牙发送数据包后wx.notifyBLECharacteristicValueChange() 监听接收数据,

1)当wx.writeBLECharacteristicValue() 发送成功后,

2)wx.notifyBLECharacteristicValueChange() 监听己收到数据后,继续调用(1)发送,直到192数据包发送完成。

3)为什么当wx.notifyBLECharacteristicValueChange() 监听到第20个接收数据后就没有反应,详见附件图:

回答关注问题邀请回答
收藏

2 个回答

  • 简爱
    简爱
    2021-04-08

    已经上传android日志。

    2021-04-08
    有用
    回复 1
    • 简爱
      简爱
      2021-04-19
      查到什么问题了吗?
      2021-04-19
      回复
  • Cjiang
    Cjiang
    2021-03-22

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

    麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点(具体到分钟)。

    2021-03-22
    有用
    回复 17
    • 简爱
      简爱
      2021-03-23
      已经上传,2021-3-22 下午 14:47:xx----2021-3-22 下午 14:49:xx  ,  微信号就是目前我登录的微信号。  vivo 手机android  pd1901L_
      a_1.21.21  微信7.0.22。  同样问题在 iphone7plus ios13.4.1 微信8.0.2 也出现,只上传了android 日志。
      2021-03-23
      1
      回复
    • 简爱
      简爱
      2021-03-23
      小应用里的js wx蓝牙通讯部分都是异步,我使用wx 消息实现同步处理传输 当数据发送成功后,蓝牙监听接收数据包然后校验返回结果后再发送wx 消息 ,继续发送1......192新数据包。


      代码片段如下:


      function notifyBLECharacteristicValueChange() {
       var that = this;
       ......
       wx.notifyBLECharacteristicValueChange({
        state: true,
        deviceId: getApp().globalData.currentDeviceId,
        serviceId: getApp().globalData.uuid_services,
        characteristicId: getApp().globalData.uuid_notify,
        complete: function (res) {
            //用来监听手机蓝牙设备的数据变化
            wx.onBLECharacteristicValueChange(function(res) {
          let buffer = res.value;
          let writeBuffer = new DataView(buffer);
          let dataResult = [];
          ...........
      var valuess = new Object();
          valuess['code'] ='0000';
          valuess['com'] = ........;
          valuess['msg'] = dataResult;


          wxNotif.postNotificationName('recvWxBleOtaNotification', valuess);
         })
        },
        fail: function (res) {
         console.log('接收数据失败');
         that.showNotificationMsg('9999', 'cmdNOne', '启用低功耗蓝牙设备监听失败', 'recvWxBleNotification');
        }
       })
      }
      .......................


      recvWxOtaNotificationFn:function (info){
          var that = this;
          let code = info['code'];
          
          if (code == '0000') {
           // OTA升级发送命令成功, 接收返回
           
           if (getApp().globalData.uploadCount == getApp().globalData.packageLength) {
            console.log('OTA固件升级包传送完毕');
             that.showView = false;
             wx.hideLoading();
           } else {
            getApp().globalData.uploadCount += 1;
              getApp().globalData.resentFlag = 0;
              //拆分包
              goPureBtCommand.splitPackage(getApp().globalData.uploadCount);
           }
          }
         },
      .....................................


      function splitPackage(uploadCount) {
       let that = this;

       wx.writeBLECharacteristicValue({
           deviceId: getApp().globalData.currentDeviceId,
           serviceId: getApp().globalData.uuid_services,
           characteristicId: getApp().globalData.uuid_send,
           value: sendDataBuffer,
           success: function (res) {
               console.log('message发送成功');


              ............
           },
           fail: function (res) {
            console.log('发送数据失败');
             ............
           },
           
       });



      注意⚠️ :如下写不写没有任何作用!!!!!!!!!!!!


       // /**
       // * 坑就在这里了,对于安卓系统,需要添加下面这段代码。你写完数据后,
       // * 还必须读一次,才能被onBLECharacteristicValueChange监听到,
       // * 才会把数据返回给你,但在苹果系统里面就不能有下面这段代码了,因
       // * 为如果你添加上去的话,会出现发一次指令出现两次返回值的情况
       // */
       // wx.readBLECharacteristicValue({
       // deviceId: getApp().globalData.currentDeviceId,
       // serviceId: getApp().globalData.uuid_services,
       // characteristicId: getApp().globalData.uuid_notify,
       // success: function (res) {
       // console.log('readBLECharacteristicValue.......');
       // }
       // });
      }
      2021-03-23
      回复
    • 简爱
      简爱
      2021-03-23回复简爱
      ios 不能真机联调,只能通过上传到公众平台小程序后,执行体验版。
      2021-03-23
      回复
    • 简爱
      简爱
      2021-03-23
      (1)通过以,修改了一下代码,不校验notifyBLECharacteristicValueChange监听是否接收到数据, 在 writeBLECharacteristicValue函数发送数据包成功后,调用recvWxBleOtaNotification 消息,可以完成1......192个包的发送。都是成功的。
      (2)但是我们设备需要同步校验notifyBLECharacteristicValueChange返回值,后再调用在 writeBLECharacteristicValue函数发送数据包。
      (3)ios 和 android 原声开发通过ble蓝牙发送数据包和监听数据包都可以正常使用。实现ota升级。
      (4)目前在考虑通 微信小应用实现 蓝牙指令已经通过了没有问题,ota升级目前遇到问题、如题。
      2021-03-23
      回复
    • 简爱
      简爱
      2021-03-23
      代码片段连接https://developers.weixin.qq.com/s/NYRsC0mt74pe
      2021-03-23
      回复
    查看更多(12)
登录 后发表内容
问题标签