收藏
回答

调用wx.getBLEDeviceCharacteristics时,API与其他函数执行顺序不正确?

ios微信版本8.0.6,基础库版本2.17.3。在函数中_getBLEDeviceCharacteristics中调用wx.getBLEDeviceCharacteristics时,程序会先执行wx.getBLEDeviceCharacteristics之后的代码,再回头执行wx.getBLEDeviceCharacteristics。算是wx.getBLEDeviceCharacteristics这个API不执行的一种情况吧。想知道这是怎么回事,bug吗 还是我代码的问题。

  _getBLEDeviceServices(deviceId) {// 获取服务特征值,是否可读,是否可写,是否可以开启notify通知等等
    once();
    wx.getBLEDeviceServices({
      deviceId,
      success: (res) => {
        console.log("获取服务成功",res)
        for (let i = 0 + service_num; i < res.services.length; i++) {
          if (res.services[i].isPrimary) {
            this._getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
            return
          }
        }
      }
    })
  },
  _getBLEDeviceCharacteristics(deviceId, serviceId) {
    console.log(canNotify,canWrite,"新建");//断点1
    wx.getBLEDeviceCharacteristics({
      deviceId,
      serviceId,
      success: (res) => {
        console.log('getBLEDeviceCharacteristics success', res.characteristics);
        for (let i = 0; i < res.characteristics.length; i++) {//依次查看每个服务能否读写
          let item1 = res.characteristics[i];
          if (item1.properties.read) {
            canRead[i] = 1;
            wx.readBLECharacteristicValue({
              deviceId,
              serviceId,
              characteristicId: item1.uuid,
            })
          }else{canRead[i] = 0};
          if (item1.properties.write) {
            canWrite[i] = 1;
            this._deviceId = deviceId;
            this._servcieId = serviceId;
            this._characteristicId = item1.uuid;
            this.writeBLECharacteristicValue();
          }else{canWrite[i] = 0};
          if (item1.properties.notify || item1.properties.indicate) {
            canNotify[i] = 1;
            wx.notifyBLECharacteristicValueChange({
              deviceId,
              serviceId,
              characteristicId: item1.uuid,
              state: true,
            });
          }else{canNotify[i] = 0;console.log("这里是notify",canNotify[i])}
        }
      },
      fail(res) {
        console.error('getBLEDeviceCharacteristics', res)
      }
    })//断点2
    console.log("canNotify:",canNotify);//断点3
  if (canNotify[0]==0){//断点4
    this._getBLEDeviceServices(deviceId);//断点5
    console.log(canNotify[0]==0,canWrite,service_num,checkCan(canNotify));//断点6
    service_num += 1;
  };

这段代码是先通过_getBLEDeviceServices调用 _getBLEDeviceCharacteristics,之后发现这个服务不是想要的服务,通过_getBLEDeviceCharacteristics重新调用_getBLEDeviceServices来获取另一个服务。但是发现 _getBLEDeviceCharacteristics这个函数里面的wx.getBLEDeviceCharacteristics这个API不按照顺序执行,导致回调判断不成功,没办法往下走了。看了一下执行流程,执行完断点1后,会直接执行断点3的语句,跳过整个断点2的API,然后执行断点4处if的判断,跳过断点5的回调,执行断点6,之后执行断点2的整个API,最后再执行断点5的回调。有没有大神解释一下,为什么是这个执行顺序。

这个是控制台调试的输出顺序,符合上面的推论。

canNotify等是前面声明过的全局变量。

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

2 个回答

  • Cjiang
    Cjiang
    2021-06-15

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

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

    2021-06-15
    有用
    回复
  • 栗晨
    栗晨
    2021-06-13

    是因为我A调用B,B调用C,C再调用A导致的程序逻辑问题吗

    2021-06-13
    有用
    回复 1
    • 栗晨
      栗晨
      2021-06-16
      解决了。是函数调用问题,A调用B,B调用A时,会接着执行 B调用A 处后面的语句,等B被完整的执行一遍后,再接着执行A
      2021-06-16
      回复
登录 后发表内容