uni-app的蓝牙api扫描不出蓝牙5.0设备
uni-app的蓝牙api扫描不出蓝牙5.0设备
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | 11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
PC开发环境操作系统版本号:11
HBuilderX类型:正式
HBuilderX版本号:4.57
手机系统:Android
手机系统版本号:Android 15
手机厂商:vivo
手机机型:PJA110
页面类型:nvue
vue版本:vue3
打包方式:云端
项目创建方式:HBuilderX
示例代码:
```javascript
const onBluetoothDeviceFound = (scanResult : UniNamespace.OnBluetoothDeviceFoundResult) => {
console.log(logTag + ' onBluetoothDeviceFound')
scanResult.devices.forEach((item : UniNamespace.BluetoothDeviceInfo) => {
const find = convert(item)
console.log(logTag + ' find=' + find.deviceName) //实测在app上扫描回调不包含蓝牙5.0的设备
})
}
uni.startBluetoothDevicesDiscovery({
services: scanFilter, allowDuplicatesKey: true, success: (_res) => {
//本机蓝牙适配器状态
console.log(logTag + ' startBluetoothDevicesDiscovery ok')
option.success()
//实测发现监听不会随着扫描结束而释放,所以需做好数据过滤和去重
if (isWeixin()) {
console.log(logTag + ' register onBluetoothDeviceFound event')
//微信上支持uni.offBluetoothDeviceFound(),所以需要重新注册
uni.onBluetoothDeviceFound(onBluetoothDeviceFound);
} else {
console.log(logTag + ' register onBluetoothDeviceFound event')
uni.onBluetoothDeviceFound(onBluetoothDeviceFound);
}
}, fail: (err) => {
console.log(logTag + ' startBluetoothDevicesDiscovery fail', err)
option.fail(onError(err))
}
})
操作步骤: 必现
预期结果: 希望解决
实际结果: 扫描回调中不包括蓝牙5.0设备
bug描述: 使用uniapp的蓝牙api,扫描ble设备,在安卓手机和ios手机上,不能扫描出蓝牙5.0的设备(微信小程序上可以扫出来),蓝牙4.0可以。
更多关于uni-app的蓝牙api扫描不出蓝牙5.0设备的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
如果现有的 uni api 不满足要求,可以在插件市场看看蓝牙插件。或者你可以配合原生混编开发 uts 插件。
更多关于uni-app的蓝牙api扫描不出蓝牙5.0设备的实战教程也可以访问 https://www.itying.com/category-93-b0.html
问题分析:
- uni-app蓝牙API在Android平台上对蓝牙5.0设备支持可能存在兼容性问题
- 微信小程序能扫描到而uni-app不能,说明是uni-app底层实现的问题
解决方案建议:
- 检查AndroidManifest.xml是否添加蓝牙5.0相关权限:
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
- 尝试修改扫描参数:
uni.startBluetoothDevicesDiscovery({
interval: 0, // 设置扫描间隔为0
allowDuplicatesKey: true,
success: () => {
uni.onBluetoothDeviceFound(res => {
console.log('发现设备:', res.devices)
})
}
})