蓝牙控件wx.getBluetoothDevices获取不到设备?
发布于 6 年前 作者 minduan 11560 次浏览 来自 官方Issues
小米手机调用蓝牙控件wx.getBluetoothDevices每次返回值都是空,确定手机蓝牙已打开,微信定位权限也打开,GPS也打开,但是每次返回结果都是{errMsg: "getBluetoothDevices:ok", devices: Array(0)}
openBluetoothAdapter:function(){
    var that=this;
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter({
        success: function (res) {
          /* 获取本机的蓝牙状态 */
          setTimeout(() => {
            that.getBluetoothAdapterState()
          }, 1000)
        },
        fail: function (err) {
          wx.showToast({
            title: '请打开手机蓝牙',
            icon: 'Loading',
            duration: 1000
          })
        }
      })
    } else {

    }
  },
  getBluetoothAdapterState() {
    var that = this;
    wx.getBluetoothAdapterState({
      success: function (res) {
        that.startBluetoothDevicesDiscovery()
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  startBluetoothDevicesDiscovery() {
    var that = this;
    setTimeout(() => {
      wx.startBluetoothDevicesDiscovery({
        success: function (res) {
          /* 获取蓝牙设备列表 */
          that.getBluetoothDevices()
        },
        fail(res) {
        }
      })
    }, 1000)
  },
  getBluetoothDevices() {
    var that = this;
    setTimeout(() => {
      wx.getBluetoothDevices({
        services: [],
        allowDuplicatesKey: false,
        interval: 0,
        success: function (res) {
          console.log(res)
          if (res.devices.length > 0) {
            console.log(res),
            console.log(JSON.stringify(res.devices))
          }
        },
        fail(res) {
          console.log(res, '获取蓝牙设备列表失败=====')
        }
      })
    }, 2000)
  }

回到顶部