小程序BLE在调试模式下一切正常,而脱离调试模式就无法获取到全部特征字
发布于 5 年前 作者 na44 12890 次浏览 来自 问答
  • 当前 Bug 的表现(可附上截图)

我在做小程序和BLE模块的通信开发,在找到设备连接成功后,调试模式和非调试模式(直接在微信里打开开发版小程序)都可以获取到services列表,但在获取Characteristics时就产生了不同:调试模式可以正常获取到所有Characteristics,而非调试模式只能获取到个别Characteristics,每次获取到的Characteristics有时还不一样(有时这个能获取,有时另外的能获取),因为不能获取到全部或者自己关心的Characteristics,所以,非调试模式虽然能连接上BLE设备,但却无法实现正常通信。

   顺便提另外一个现象:在调试模式获取不全Characteristics时(偶尔),若重新调用获取Services和Characteristics,感觉底层或有重复,因为在连接后向BLE模块发送指令时,发送一次会出现多次成功回调。

  • 预期表现

期望无论在调试模式还是非调试正常运行模式,都能获取到所有Services和Characteristics.

  • 复现路径

连接上BLE设备,获取其Services并对每一个Service获取其Characteristics

  • 提供一个最简复现 Demo

// 获取连接设备的service服务
  getServices: function (dev) {
    var that = this;
    var servindex = that.data.devicesT.servindex;
    servindex.push(dev.index);
    that.setData({
      [“devicesT.servindex”]: servindex
    })
    wx.getBLEDeviceServices({
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: dev.deviceId,
      success: function (res) {
        var servindex = that.data.devicesT.servindex;
        var dev = that.data.devicesT.devices[servindex.shift()];
        console.log(‘device ‘+ dev.name +’ services found:’, JSON.stringify(res.services));
        dev.services = res.services
        that.setData({
          [“devicesT.servindex”]: servindex,
          [“devicesT.devices[” + dev.index + “].services”]: dev.services,
          //msg: JSON.stringify(dev.services),
        });
        console.log(‘getBLEDeviceServices’, dev.nickName + ’ services:’ + res.services.length)
        setTimeout(function(){
          that.getCharactors(dev);
        },100,dev)
       
      },fail: function(res){
        var servindex = that.data.devicesT.servindex;
        var dev = that.data.devicesT.devices[servindex.shift()];
        var sett_device = ‘devicesT.devices[’+dev.index+’]’;
        that.setData({
          [“devicesT.servindex”]: servindex,
          gerrcode : res.errCode,
          [sett_device+’.errCode’]: res.errCode
        })
      }
    })
  },

  getCharactors: function (dev) {
    var that = this;
    var devId = dev.deviceId
    var index = dev.index
    //对所有预定义的服务通道进行初始化填充uuid
    var defineChnls = that.data.devicesT.defineChnls;
    for (var dch in defineChnls) {
      var defChnl = defineChnls[dch];
      for (var sv in dev.services) {
        var serv = dev.services[sv];
        if(that.data.gerrcode!=‘0’) { 
          return;
        }    
        if (defChnl.servId != serv.uuid.substr(4, 4)){
          continue;
        }       
        console.log(“findingChL:”, “dev:”+ dev.nickName+","+defChnl.servId + “**” + serv.uuid + “**”); 
      
        var servchnls = { servUUID: serv.uuid, chnls: [] };               

        var devservidx = that.data.devicesT.devservidx;
        devservidx.push({
          device: dev,
          defchnls: defChnl,
          servchls: servchnls
        })
        var keyName = “devicesT.devices[” + index + “].channels”;
        that.setData({
          [“devicesT.devservidx”]: devservidx,
        });
        wx.getBLEDeviceCharacteristics({
          // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
          deviceId: devId,
          // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
          serviceId: serv.uuid,
          success: function (res) {
            //res里没有当初的deviceId和serviceId真是恶心人呀,得从根儿里自己匹配
           
            var devservidx = that.data.devicesT.devservidx;
            var idxOne = devservidx.shift()
            var dev = idxOne.device;
            var servchls = idxOne.servchls;
            var defChnl = idxOne.defchnls;

            console.log(‘getBLEDeviceCharacteristics’, dev.nickName + ‘,’ + servchls.servUUID+’,’+res.errMsg);

            var channels = dev.channels;
            if (channels == undefined) {
              channels = new Array();
            }

            for (var fc in defChnl.chnls) {//只提取那些已经事先定义好的Channels
              var defCh = defChnl.chnls[fc];
              for (var cs in res.characteristics) {
                var ch = res.characteristics[cs];
                var shId = ch.uuid.substr(4, 4);
                //定位目标通道
                if (shId != defCh.chId) {
                  continue;
                }
                var chnl = { charUUID: ch.uuid }
                var isExist = false;
                for (var j in servchls.chnls){
                  var cl = servchls.chnls[j].charUUID
                  if (cl == ch.uuid) {
                    isExist = true;
                    break;
                  }
                }
                if(!isExist){
                  servchls.chnls.push(chnl);
                  console.log(“name:” + dev.name + “,servId:” + defChnl.servId + “,chnlId:” + ch.uuid);
                }
               
                break;
              }
            }
            isExist = false;
            for (var j in channels) {             
              var scId = channels[j].servUUID
              if (scId == servchls.servUUID) {
                isExist = true;
                break;
              }
            }
            if (!isExist) {
              channels.push(servchls);
            }
            dev.channels = channels;
           
            var sett_device = “devicesT.devices[” + dev.index + “]”;
            that.setData({
              [sett_device+’.channels’]: dev.channels,
              [“devicesT.devservidx”]: devservidx,
              //msg: JSON.stringify(channels),
            });
            console.log(JSON.stringify(dev.channels));
           
            //这里确认我们定义的属性通道特征字是否已经获取齐全,如果齐全了,则配置使能事件通知           
            if(!that.channelsReady(dev)) return;
            setTimeout(function () {             
              that.blecallbackinit(dev);//配置使能事件通知
            }, 200, dev);
          },fail: function(res){
            var devservidx = that.data.devicesT.devservidx;
            var idxOne = devservidx.shift()
            var dev = idxOne.device;
            var servchls = idxOne.servchls;
            var sett_device = “devicesT.devices[” + dev.index + “]”;
            console.log(dev.nickName + ‘,’ + servchls.servUUID + ‘,’ + JSON.stringify(res));
            that.setData({
              gerrcode: res.errCode,
              [sett_device + ‘.errCode’]: res.errCode,
              [sett_device+’.channels’]: dev.channels,
              [“devicesT.devservidx”]: devservidx,
              //msg: JSON.stringify(channels),
            });
          }  
        })
      }
    };   
  },

1 回复

请提供出现问题的机型和微信版本、基础库版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

回到顶部