评论

微信小程序ble功能开启notify后几率出现连接断开问题,大家如何看?

ble notify

我的ble操作中,采取连接成功后,开启通信服务(兼容ios),服务开启成功后,延迟开启notify服务。代码片段如下所示:

wx.createBLEConnection({
    deviceId: obj.data.lockMac,
    successfunction (res{
      wx.hideLoading();
      wx.showToast({
        title'连接成功',
        icon'success',
        duration1000
      })
      //更改全局连接状态
      isConnected = true;
      console.log("连接设备成功");
      console.log("连接success回调:" + JSON.stringify(res));
      //关闭扫描
      stopScanAroundDevice(obj);
      
      //连接设备成功   开启相应的设备通信服务,同时也需要开启notify监听事件
      firstReadCharacValBefore(obj);
    },
    failfunction (res{
      wx.hideLoading();
      wx.showToast({
        title'连接设备失败',
        icon'cancel',
        duration1000
      })
      console.log("连接设备失败")
      console.log("连接fail回调:" + res);
      obj.setData({
        btnDisfalse,
      });
      //执行 quit 机制
      quit(obj);
    }
  })


开启通信服务和notify代码如下:

// 新增ios的开关所操作流程
// 关锁操作,只进行数据的notify变化监听操作(关锁成功,将关锁状态信息上报)
function firstReadCharacValBefore(obj{
    wx.getBLEDeviceServices({
      // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
      deviceId: obj.data.lockMac,
      success(res) {
        console.log(JSON.stringify(res))
        console.log('device services-----:', res.services)
        console.log('device services-----:', apps.ab2hex(res))
        console.log('device services-----:', apps.ab2hex(res.services))


        //获取设备特征对象
        wx.getBLEDeviceCharacteristics({
          deviceId: obj.data.lockMac,
          serviceId: apps.bleProperties.bleServiceUUID,
          successfunction (res{
            console.log(JSON.stringify(res))
            console.log("wx.getBLEDeviceCharacteristics  ---->\n" + apps.ab2hex(res));
            
            // 需要延迟开启(避免出现开启notify失败的问题)
          setTimeout(() => {
            wx.notifyBLECharacteristicValueChange({
              characteristicId: apps.bleProperties.readCharacUUID,
              deviceId: obj.data.lockMac,
              serviceId: apps.bleProperties.bleServiceUUID,
              statetrue,
              type:"notification"// 默认为 indication(有ack) ,notification无ack
              success:function(res){
                console.log("wx.notifyBLECharacteristicValueChange  success回执:\n"+JSON.stringify(res));
                
                // // 开启notify监听之后,需要开启特征值数据变更回执
                console.log("######################################  wx.onBLECharacteristicValueChange  前");
                wx.onBLECharacteristicValueChange(function(res{
                  console.log("######################################  wx.onBLECharacteristicValueChange  后");
                  console.log("----->   wx.onBLECharacteristicValueChange 监听事件:\n" + JSON.stringify(res));
                  isOpenCharacValueChange = true;


                  //需要区别是第一次读操作还是第二次读操作
                  var readVal = apps.ab2hex(res.value);
                  console.log("读取到的数据:" + readVal);
                  // obj.setData({
                  //   btnDisabled:false , // 释放按钮
                  // });
                  
                  // 加密数据,需要解密,解密放在该函数中
                  getNotifyValue(readVal,obj);
                })


                // 成功开启notify后,延迟进行断开连接操作
                delayCloseConnect(obj);


              },
              fail:function(){
                wx.showModal({
                  title'温馨提示',
                  content'开启notify失败!',
                  showCancelfalse
                });
                obj.setData({
                  btnDisabledfalse,
                });
                quit(obj);
              }
            })
          }, apps.bleProperties.bleNotifyTimeout);
            
          },
          failfunction () {
            wx.showModal({
              title'温馨提示',
              content'获取特征对象失败!',
              showCancelfalse
            });
            quit(obj);
          }
        })
      },
      failfunction () {
        wx.showModal({
          title'温馨提示',
          content'获取服务失败!',
          showCancelfalse
        });
        quit(obj);
      }
    })
}


// 延迟关闭连接操作(默认 1min)
function delayCloseConnect(obj){
  if(quitTimeout != null){
    // 清楚settimeout 计时器,重新计时
    clearTimeout(quitTimeout);
    quitTimeout = null;
  }
  quitTimeout = setTimeout(() => {
    console.log("----->延迟断开连接");
    quit(obj);
  }, apps.bleProperties.delayCloseConnectTimes);
  // setTimeout(() => {
  //   quit(obj);
  // }, apps.bleProperties.delayCloseConnectTimes);
}





其中监听连接状态信息如下所示:

wx.onBLEConnectionStateChange(function (res{
        console.log("蓝牙设备连接状态监听回调:\n" + JSON.stringify(res));
        if (!res.connected) {
          //为false  表示连接断开
          wx.showToast({
            title'连接断开',
            icon'success',
            duration1000
          })
          quit(obj);
        }
      });

测试运行正常的话,日志信息如下所示:

但很大几率出现,延迟开启notify成功后,连接状态立马变更为 链接失败,如下所示:

请问这种问题出现的原因是什么?


最后一次编辑于  09-10  
点赞 1
收藏
评论

2 个评论

登录 后发表内容