uni-app startBluetoothDevicesDiscovery 无法重复搜索到设备

uni-app startBluetoothDevicesDiscovery 无法重复搜索到设备

开发环境 版本号 项目创建方式
Mac 10.15.3 HBuilderX
iOS iOS 15

操作步骤:

  • 初始化完成适配器后,直接开始搜索

预期结果:

  • 能重复搜索到设备

实际结果:

  • 只能搜到一次

bug描述:

  • startBluetoothDevicesDiscovery 接口无法重复搜索到蓝牙设备,已经确认正确设置了 allowDuplicatesKey 参数为true。

更多关于uni-app startBluetoothDevicesDiscovery 无法重复搜索到设备的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app startBluetoothDevicesDiscovery 无法重复搜索到设备的实战教程也可以访问 https://www.itying.com/category-93-b0.html


根据描述,这是一个典型的蓝牙设备重复发现的问题。以下是可能的原因和解决方案:

  1. allowDuplicatesKey参数验证: 虽然设置了allowDuplicatesKey为true,但建议在调用时再次确认:
uni.startBluetoothDevicesDiscovery({
  allowDuplicatesKey: true,
  success: () => console.log('开始搜索'),
  fail: err => console.error(err)
})
  1. iOS平台特性: iOS对蓝牙扫描有限制,建议:
  • 确保在每次扫描前调用stopBluetoothDevicesDiscovery
  • 添加2-3秒延迟后再重新扫描
  1. 完整流程示例
async function scanDevices() {
  await uni.stopBluetoothDevicesDiscovery()
  setTimeout(() => {
    uni.startBluetoothDevicesDiscovery({
      allowDuplicatesKey: true,
      success: () => console.log('开始搜索'),
      fail: err => console.error(err)
    })
  }, 2000)
}
  1. 监听设备发现事件: 确保正确监听了onBluetoothDeviceFound事件:
uni.onBluetoothDeviceFound(devices => {
  console.log('发现设备:', devices)
})
回到顶部