uni-app uni.startBluetoothDevicesDiscovery app超高频搜索并且搜不到蓝牙
uni-app uni.startBluetoothDevicesDiscovery app超高频搜索并且搜不到蓝牙
示例代码:
//搜索蓝牙
const startBluetoothDiscovery = () => {
state.connectedId = ''
state.connectedDeviceId = ''
uni.startBluetoothDevicesDiscovery({
success: function (res) {
console.log('开始搜索蓝牙设备', res);
state.bluetoothDevices = []
// 监听搜索到的蓝牙设备
listenToBluetoothDevices();
},
fail: function (err) {
console.error('开始搜索蓝牙设备失败', err);
}
});
} //监听搜索到的蓝牙设备
const listenToBluetoothDevices = () => {
uni.onBluetoothDeviceFound(function (res) {
// 在 res.devices 中获取搜索到的蓝牙设备信息
let foundDevices = res.devices;
const pattern = /^[^@]*@[0-9]+$/;
// 在这里进行处理,比如展示设备列表等
// 过滤掉重复且设备名称不为空、广告数据不为空的设备
const validDevices = res.devices.filter(device => {
return (
device.connectable === true &&
pattern.test(device.name) &&
!state.bluetoothDevices.some(existingDevice => existingDevice.deviceId === device.deviceId) // 判断是否重复
);
});
state.bluetoothDevices = [...state.bluetoothDevices, ...validDevices];
console.log(state.bluetoothDevices)
});
}
操作步骤:
搜索startBluetoothDiscovery附近蓝牙
预期结果:
中等频率搜索到附近蓝牙,监听搜索到的蓝牙设备并进行赋值.
实际结果:
超高频搜索附近蓝牙,并且搜不到附近的蓝牙设备,以及超高频率导致手机卡顿.
bug描述:
在安卓app上uni.startBluetoothDevicesDiscovery一直超高频搜索,并且搜不到附近蓝牙,在小程序中安卓与ios都正常搜索蓝牙。目前安卓试了多个手机,小米、vivo、华为都试过。
| 信息类别 | 详细信息 |
|---|---|
| 产品分类 | uniapp/App |
| PC开发环境操作系统 | Windows |
| PC开发环境操作系统版本号 | win10 |
| HBuilderX类型 | 正式 |
| HBuilderX版本号 | 3.98 |
| 手机系统 | Android |
| 手机系统版本号 | Android 13 |
| 手机厂商 | vivo |
| 手机机型 | V2056A |
| 页面类型 | vue |
| vue版本 | vue3 |
| 打包方式 | 云端 |
| 项目创建方式 | HBuilderX |
更多关于uni-app uni.startBluetoothDevicesDiscovery app超高频搜索并且搜不到蓝牙的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app uni.startBluetoothDevicesDiscovery app超高频搜索并且搜不到蓝牙的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在使用 uni-app 进行蓝牙设备搜索时,uni.startBluetoothDevicesDiscovery 是用于启动蓝牙设备搜索的 API。如果你遇到超高频搜索并且搜不到蓝牙设备的问题,以下是一些可能的原因和解决方案:
1. 权限问题
-
原因:Android 和 iOS 都需要特定的权限来访问蓝牙功能。如果权限未正确配置,可能会导致搜索不到设备。
-
解决方案:
- 确保在
manifest.json中正确配置了蓝牙权限。 - 对于 Android,需要
ACCESS_FINE_LOCATION权限,因为蓝牙扫描需要位置权限。 - 对于 iOS,需要在
Info.plist中添加NSBluetoothAlwaysUsageDescription和NSBluetoothPeripheralUsageDescription的描述。
// manifest.json for Android { "permissions": [ "android.permission.ACCESS_FINE_LOCATION" ] }<!-- Info.plist for iOS --> <key>NSBluetoothAlwaysUsageDescription</key> <string>我们需要访问蓝牙以连接设备</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>我们需要访问蓝牙以连接设备</string> - 确保在
2. 蓝牙未开启
-
原因:如果设备的蓝牙未开启,自然无法搜索到设备。
-
解决方案:
- 在使用
uni.startBluetoothDevicesDiscovery之前,检查蓝牙是否已开启。可以使用uni.openBluetoothAdapter来初始化蓝牙适配器。
uni.openBluetoothAdapter({ success(res) { console.log('蓝牙适配器初始化成功'); uni.startBluetoothDevicesDiscovery({ success(res) { console.log('开始搜索蓝牙设备'); }, fail(err) { console.error('启动搜索失败', err); } }); }, fail(err) { console.error('蓝牙适配器初始化失败', err); } }); - 在使用
3. 搜索频率过高
- 原因:如果频繁调用
uni.startBluetoothDevicesDiscovery,可能会导致设备无法正常响应。 - 解决方案:
- 避免频繁调用搜索 API,适当控制搜索间隔时间。
4. 设备未处于可被发现状态
- 原因:蓝牙设备可能未处于可被发现的状态。
- 解决方案:
- 确保目标蓝牙设备已开启并处于可被发现的状态(通常需要在设备上手动开启可被发现模式)。
5. 设备不支持低功耗蓝牙(BLE)
- 原因:
uni.startBluetoothDevicesDiscovery主要用于搜索低功耗蓝牙(BLE)设备。如果目标设备不支持 BLE,可能无法搜索到。 - 解决方案:
- 确保目标设备支持 BLE,并且设备处于广播状态。
6. 设备不在范围内
- 原因:蓝牙设备的有效范围通常在 10 米左右,如果设备不在范围内,可能无法搜索到。
- 解决方案:
- 确保目标设备在有效范围内,并且没有障碍物干扰。
7. 设备已被其他应用连接
- 原因:如果目标蓝牙设备已被其他应用连接,可能无法被当前应用搜索到。
- 解决方案:
- 确保目标设备未被其他应用占用。
8. 设备名称或 UUID 不匹配
- 原因:如果指定了
services或allowDuplicatesKey参数,可能会导致搜索不到设备。 - 解决方案:
- 确保
services和allowDuplicatesKey参数设置正确,或者尝试不指定这些参数进行搜索。
- 确保
9. 设备兼容性问题
- 原因:某些设备可能存在兼容性问题,导致无法搜索到蓝牙设备。
- 解决方案:
- 尝试在其他设备上进行测试,或者更新设备固件。
10. 代码逻辑问题
-
原因:代码逻辑可能存在错误,导致搜索无法正常进行。
-
解决方案:
- 检查代码逻辑,确保调用
uni.startBluetoothDevicesDiscovery后正确监听onBluetoothDeviceFound事件。
uni.startBluetoothDevicesDiscovery({ success(res) { console.log('开始搜索蓝牙设备'); uni.onBluetoothDeviceFound((res) => { console.log('发现新设备', res.devices); }); }, fail(err) { console.error('启动搜索失败', err); } }); - 检查代码逻辑,确保调用

