蓝牙api allowDuplicatesKey参数设置
发布于 5 年前 作者 gang15 18353 次浏览 来自 问答

wx.startBluetoothDevicesDiscovery(OBJECT)

根据文档所述我调用该api时将allowDuplicatesKey设为了false,在

wx.onBluetoothDeviceFound(CALLBACK)这个回调内依然检测出deviceId和name相同的设备

剔除业务逻辑后的代码如下:

wx.openBluetoothAdapter({ // 初始化蓝牙适配器
     success: function (res) {
       wx.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
         allowDuplicatesKey: false,
         success: function (res) {
           wx.getBluetoothDevices({ // 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备
             success: function (res) {
               wx.hideLoading();
             },
             fail: function (res) {
               wx.hideLoading();
             }
           })
           wx.onBluetoothDeviceFound(function (devices) {
             console.log(devices.devices[0]);
           })
         },
         fail: function (res) {
           wx.hideLoading();
         }
       })
     },
     fail: function (res) {
       wx.hideLoading();
     }
   })

调试结果:

可以看到console出来的设备有很多是重复的这里仅列举了一个,不知道是使用的姿势不对吗?调试用的真机是iOS10.3.2,微信版本是6.5.9

6 回复

@ifwang 目前这个问题暂时不影响我们的业务逻辑,因为我们测试用的蓝牙设备是非专业人士焊接的,引脚貌似有点问题经常松动,所以我弄了一个测试页面就想保证一下能发现测试用蓝牙设备。您可以把我的问题优先级靠后,麻烦您了。由于提问题时的代码现在已经废弃了,我重新改动了一下,这里贴上测试页面的完整代码,请您关注7月14日11:00开始的连接行为,感谢!微信号是Fireloli

var app = getApp();
Page({
  data: {
    devices: []
  },
  clear() {
    this.setData({
      devices: []
    })
  },
  retry() {
    this.bluetoothConn('none');
  },
  stop() {
    wx.stopBluetoothDevicesDiscovery({ // 如果成功后就停止搜索
      success: function (res) { }
    })
  },
  bluetoothWarn(url, msg, confirm) {
    let self = this;
    wx.showModal({
      title: '提示',
      content: msg,
      confirmText: confirm,
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          self.bluetoothConn(url);
        } else if (res.cancel) {
          console.log('已取消');
        }
      }
    })
  },
  bluetoothConn(url) {
    let self = this,
      data = [];
    if (wx.openBluetoothAdapter) {
      wx.showLoading({
        title: '正在获取商品'
      })
      wx.openBluetoothAdapter({ // 初始化蓝牙适配器
        success: function (res) {
          wx.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
            allowDuplicatesKey: false,
            success: function (res) {
              wx.getBluetoothDevices({ // 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备
                success: function (res) {
                  wx.hideLoading();
                },
                fail: function (res) {
                  wx.hideLoading();
                  self.bluetoothWarn(url, '获取蓝牙设备失败', '重连');
                }
              })
              wx.onBluetoothDeviceFound(function (devices) {
                console.log(devices.devices[0]);
                data.push(devices.devices[0]);
                // if (devices.devices[0].name == 'BLE SPS') {
                //   wx.hideLoading();
                //   wx.createBLEConnection({
                //     deviceId: devices.devices[0].deviceId,
                //     success: function (res) {
                //       wx.hideLoading();
                //       wx.getConnectedBluetoothDevices({
                //         services: devices.devices[0].advertisServiceUUIDs,
                //         success: function (res) {
                //           console.log(res)
                //         }
                //       })
                //       wx.stopBluetoothDevicesDiscovery({
                //         success: function (res) {
                //         }
                //       })
                //     },
                //     fail: function (res) {
                //       wx.hideLoading();
                //       bluetoothWarn(url, '连接蓝牙设备失败', '重连');
                //     }
                //   })
                // }
                self.setData({
                  devices: data
                })
              })
            },
            fail: function (res) {
              wx.hideLoading();
              self.bluetoothWarn(url, '请保持在机器附近', '重连');
            }
          })
        },
        fail: function (res) {
          wx.hideLoading();
          self.bluetoothWarn(url, '请打开手机蓝牙', '已开启');
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
      return;
    }
  },
  onLoad: function (options) {
    this.bluetoothConn('none');
  },
})

结果还是一样,不过之前重复的那台65设备已经不见了

@ifwang 开启蓝牙的macbook也会重复上报,马掉的是我的名字见谅,所以我想应该不是这些设备的原因?

@ifwang 测试环境在密集的办公楼内 蓝牙设备有很多。。不止我司的

@ifwang  部分设备(包括图上AF65)固定重复出现,有时候不止两次,部分不固定重复

如果设备确定的话是否可以指定下 services参数,减少发现的设备,来解决设备密集的情况呢?

参数类型必填说明
servicesArray蓝牙设备主 service 的 uuid 列表

方便提供下出问题的微信号么?我看下日志是否能看出问题,该接口是透传系统返回的数据的,一般不会重复。

和您确认一下,发现重复设备是固定会重复发现两次么?

回到顶部