收藏
回答

getBluetoothDevices 不出数据找不到问题所在,求解惑

{
  data: {
    status: "",
    sousuo:"",//--是否在搜索中
    devices:[]//--设备列表
  },
  onLoad: function () {
    var that=this;
    if (wx.openBluetoothAdapter) {//--判断基库的版本是否支持操作蓝牙
      wx.openBluetoothAdapter({
        success: function (res) {
          console.log('1.初始化蓝牙适配器:',res);
          that.setData({ status: "1.初始化成功!"});
          //--监听新设备-------------------------------
          wx.onBluetoothDeviceFound(function (devices) {
            var temp = that.data.devices;
            temp.push(devices)
            that.setData({
              devices: temp
            })
            console.log('0.1发现新蓝牙设备', devices)
            console.log('0.2设备id' + devices.deviceId)
            console.log('0.3设备name' + devices.name)
            console.log('0.4.详情:',devices)
          });
          //监听蓝牙适配器状态
          wx.onBluetoothAdapterStateChange(function (res) {
            console.log('2.1.状态发生变化:', res);
            that.setData({
              sousuo: res.discovering ? "搜索中.." : "搜索完成",
              status: res.available ? "可用" : "不可用",
            });
          });
          //1--------------------------------------------------
          //获取蓝牙适配器状态
          wx.getBluetoothAdapterState({
            success:function(res){
              console.log('2.获取蓝牙状态:', res);
              that.setData({
                sousuo: res.discovering ? "搜索中.." : "未搜索",
                status: res.available ? "可用" : "不可用",
              });
              if (!res.discovering){//--如果没有搜索就执行搜索设备
                //--开始搜索蓝牙设备-------------------
                wx.stopBluetoothDevicesDiscovery({success:function(){}});//--页面返回要先干掉上一个搜索
                wx.startBluetoothDevicesDiscovery({
                  //services: [],
                  success: function (res) {
                    console.log('3.搜索成功返回适配器状态:', res);
                  },
                  fail: function (res) {
                    console.log('-3.搜索失败:', res);
                    that.setData({ status: "-3.搜索失败!" });
                  },
                });
              }
            }
          });
           

          /*************************************求助***********************************************************


          【那位高手帮看一下这里为什么一天就出过几次数据剩下的设备列表全为空,疯了整理两天了,找不出来问题】

          【手机真机测试,getBluetoothDevices 不出数据88%,怎么回事跪求帮解一下疑惑


          wx.getBluetoothDevices({
            success: function (res) {
                console.log('3.1.成功获取设备:', res);
                that.setData({
                  status: "3.1.成功获取设备列表",
                  devices: res.devices
                });
            },
            fail: function (res) {
              console.log('-3.1.设备获取失败:', res);
              that.setData({ status: "-3.1.设备获取失败!" });
            }
          });
           
          //setTimeout(_get_d,5000);
          //--------------------------------------------------1
        },
        fail:function(res){
          console.log('-1.蓝牙初始化失败:',res)
          that.setData({ status: "-1.初始化失败!" })
        }
      });
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      });
    }
 
  },
}


回答关注问题邀请回答
收藏
登录 后发表内容