uni-app 蓝牙扫描 uni.onBluetoothDeviceFound(CALLBACK) 的 devices 中 advertisData 为空

uni-app 蓝牙扫描 uni.onBluetoothDeviceFound(CALLBACK) 的 devices 中 advertisData 为空

开发环境 版本号 项目创建方式
Windows win 11 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Windows

手机系统:Android

手机系统版本号:Android 13

手机厂商:OPPO

手机机型:oppo reno6 pro

页面类型:vue

vue版本:vue2

打包方式:云端

示例代码:

uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success(res) {
uni.onBluetoothDeviceFound((devices) => {
let res = JSON.parse(JSON.stringify(devices));
res.devices.forEach((device) => {
if (device.name !== '') {
console.log(
'res====================================',
device.name, that.ab2hex(
device.advertisData), device);
}
});
},
})

操作步骤:

uni.startBluetoothDevicesDiscovery({  
    allowDuplicatesKey: true,  
    success(res) {  
        uni.onBluetoothDeviceFound((devices) => {  
            let res = JSON.parse(JSON.stringify(devices));  
            res.devices.forEach((device) => {  
                if (device.name !== '') {  
                    console.log(  
                        'res====================================',  
                        device.name, that.ab2hex(  
                            device.advertisData), device);  
                 }  
        });  
    },  
})

预期结果:

advertisData 应有值 

实际结果:

advertisData  为空

bug描述:

App 环境  调用蓝牙 uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success(res) { }
)}
使用 uni.onBluetoothDeviceFound 后 无法在回调函数里 接收到的 advertisData 字段 是空的对象。 但是使用蓝牙助手显示为正常的。

更多关于uni-app 蓝牙扫描 uni.onBluetoothDeviceFound(CALLBACK) 的 devices 中 advertisData 为空的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 蓝牙扫描 uni.onBluetoothDeviceFound(CALLBACK) 的 devices 中 advertisData 为空的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的蓝牙扫描数据获取问题。在uni-app中,advertisData为空通常有以下原因:

  1. 系统权限限制:Android系统对蓝牙扫描数据的获取有严格限制,部分厂商系统(如OPPO)可能默认不返回广播数据。

  2. 设备广播策略:蓝牙设备可能只在特定条件下发送包含完整广播数据包,或者设备本身配置了精简广播模式。

  3. uni-app API限制:底层实现可能未完全获取所有蓝牙数据字段。

解决方案

  1. 检查权限配置

    • 确保在manifest.json中声明了完整的蓝牙权限:
    "permissions": {
      "BLUETOOTH": {},
      "BLUETOOTH_ADMIN": {},
      "ACCESS_FINE_LOCATION": {}
    }
    
  2. 尝试获取deviceId进行详细查询

    uni.onBluetoothDeviceFound((devices) => {
      devices.devices.forEach((device) => {
        if (device.deviceId) {
          // 通过deviceId获取更详细的设备信息
          console.log('发现设备:', device.deviceId, device.name);
        }
      });
    });
    
  3. 验证设备广播数据

    • 使用专业的蓝牙调试工具确认设备确实在发送广播数据。
  4. 考虑使用getBluetoothDevices

    uni.getBluetoothDevices({
      success: (res) => {
        console.log('设备列表:', res.devices);
      }
    });
回到顶部