收藏
回答

蓝牙第二次搜索出来设备缺少?

求助大神帮助,现场有7-8蓝牙模块,第一次搜到一个能显示出来,第二次能显示两个,但后台显示有四个,为什么不能全部显示出来?

Page({
  data:{
    devices:[],
  },
  onPullDownRefresh:function(){
    this.onBluetoothDeviceFound();
  },
  show_device:function(){
    //初始化蓝牙模块
    wx.openBluetoothAdapter({
      success(res) => {
        console.log('openBluetoothAdapter success',res);
        //获取本机蓝牙适配器状态
        wx.getBluetoothAdapterState({
          success:(res)=>{
            console.log('getBluetoothAdapterState',res);
            //discovering是否在搜索
            if(res.discovering){
              this.onBluetoothDeviceFound();
            //available蓝牙适配器是否可用  
            }else if(res.available){
              wx.startBluetoothDevicesDiscovery({
                allowDuplicatesKeytrue,
                success:(res)=>{
                  console.log('startBluetoothDevicesDiscovery success',res);
                  this.onBluetoothDeviceFound();
                },
                fail:(res)=>{
                  console.log('搜索蓝牙失败');
                }
              })
            }
          }
        })
      },
    })
    console.log(this.data.devices);
  },  
  onBluetoothDeviceFound:function(){
    var i=0;
    wx.getBluetoothDevices({
      success(res) => {
        console.log(res);
        res.devices.forEach((device)=>{
          if(!device.name||!device.localName){
            return;
          }
          var name=`devices[${i}].name`;
          var deviceId=`devices[${i}].deviceId`;
          var rssi=`devices[${i}].rssi`;        
          var localName=`devices[${i}].localName`;                
          this.setData({[name]:device.name,[deviceId]:device.deviceId,[rssi]:device.RSSI,[localName]:device.localName});
          i=+1;
        })
      },
    })
  },
  stop_device:function(){
    this.stopBluetoothDevicesDiscovery();
  },
  stopBluetoothDevicesDiscovery:function(){
    wx.stopBluetoothDevicesDiscovery();
  },
  conn_event:function(e){
    console.log(e);
  }
})

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

1 个回答

登录 后发表内容
问题标签