HarmonyOS 鸿蒙Next 调用 bluetooth.BLE.startBLEScan 获取不到设备名称,且返回广播数据异常

HarmonyOS 鸿蒙Next 调用 bluetooth.BLE.startBLEScan 获取不到设备名称,且返回广播数据异常

调用 bluetooth.BLE.startBLEScan 获取不到设备名称,从打印的广播数据来看,似乎拿到的广播数据也是不对的,请专家帮忙看下,以下是代码

```javascript
bluetooth.BLE.on("BLEDeviceFind", (result) => {
    console.info('BLE scan devices, find result number: '+ result.length);
    console.info('find device result = '+ JSON.stringify(result))
    for (let i=0; i<result.length; i++) {
        let deviceName = ''
        let content = new Uint8Array(result[i].data)
        let offset = 0
        let dataLen = content[offset]
        console.info(result[i].deviceId+' '+content[offset].toString(16)+', '+
            content[offset+1].toString(16)+', '+content[offset+2].toString(16)+', '+content[offset+3].toString(16));
        while (dataLen > 0) {
            console.info('content len ' + content.length + ', dataLen ' + dataLen);
            if (content[offset+1] == 0x09) {
                console.info('found complete local name '+result[i].deviceId+', dataLen ' + dataLen)
                for (let i=0; i<dataLen-1; i++) {
                    deviceName += String.fromCharCode(content[offset+2+i])
                }
                break;
            }
            offset += dataLen + 1
            dataLen = content[offset]
        }
        if (deviceName == '') {
            console.log('not get device name')
            deviceName = bluetooth.getRemoteDeviceName(result[i].deviceId)
            console.log('re-get device name ' + deviceName)
            continue;
        }
        console.info('device id: ' + result[i].deviceId + ', device name: ' + deviceName)
    }
});

bluetooth.BLE.startBLEScan(
    [{
        deviceId: ''
    }],
    {
        interval: 500,
        dutyMode: bluetooth.ScanDuty.SCAN_MODE_LOW_POWER,
        matchMode: bluetooth.MatchMode.MATCH_MODE_AGGRESSIVE,
    }
);

以下是打印日志:-------------------------------------

08-05 09:02:34.342 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: BLE scan devices, find result number: 6
08-05 09:02:34.350 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: find device result = [{"deviceId":"50:94:AA:8E:4E:00","rssi":-96,"data":{"0":1,"1":9,"2":32,"3":2,"4":108,"5":243,"6":146,"7":245,"8":156,"9":122,"10":242,"11":127,"12":184,"13":247,"14":41,"15":62,"16":185,"17":4,"18":3,"19":99,"20":124,"21":134,"22":184,"23":162,"24":27,"25":194,"26":15,"27":0,"28":68,"29":65,"30":52}},{"deviceId":"46:68:40:10:14:A9","rssi":-88,"data":{"0":1,"1":9,"2":32,"3":2,"4":68,"5":2,"6":129,"7":205,"8":103,"9":252,"10":192,"11":102,"12":106,"13":164,"14":90,"15":146,"16":92,"17":15,"18":83,"19":15,"20":220,"21":37,"22":173,"23":194,"24":45,"25":54,"26":170,"27":0,"28":156,"29":186,"30":218}},{"deviceId":"30:BC:A6:B4:05:B2","rssi":-70,"data":{"0":2,"1":1,"2":26,"3":2,"4":10,"5":7,"6":10,"7":255,"8":76,"9":0,"10":16,"11":5,"12":60,"13":28,"14":198,"15":87,"16":240,"17":0,"18":49,"19":50,"20":49,"21":53,"22":68,"23":51,"24":33,"25":0,"26":0,"27":0,"28":32,"29":0,"30":0}},{"deviceId":"EA:D8:D1:E5:1B:AB","rssi":-79,"data":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":}}
08-05 09:02:34.351 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: 50:94:AA:8E:4E:00 1, 9, 20, 2
08-05 09:02:34.351 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: content len 31, dataLen 1
08-05 09:02:34.351 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: found complete local name 50:94:AA:8E:4E:00, dataLen 1
08-05 09:02:34.351 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: not get device name
08-05 09:02:34.352 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:155)]GetRemoteDeviceName start
08-05 09:02:34.352 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_utils.cpp(ParseString:47)]param=4.
08-05 09:02:34.352 1908-1918/com.example.getbthb I 06000/Bluetooth: [bluetooth_remote_device.cpp(GetDeviceName:158)]../../foundation/communication/bluetooth/interfaces/innerkits/native_cpp/framework/src/bluetooth_remote_device.cpp: GetDeviceName(): Enter!
08-05 09:02:34.355 1908-1926/com.example.getbthb D 06000/Bluetooth: [bluetooth_ble_central_manager_callback_stub.cpp(OnRemoteRequest:50)]BluetoothBleCentralManagerCallBackStub::OnRemoteRequest, cmd = <private>, flags= <private>
08-05 09:02:34.355 1908-1926/com.example.getbthb I 06000/Bluetooth: [bluetooth_observer_list.h(ForEach:87)]BluetoothObserverList<T>::ForEach called
08-05 09:02:34.355 1908-1926/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_ble_central_manager_callback.cpp(OnStartScanFailed:184)]NapiBluetoothBleCentralManagerCallback::OnStartScanFailed called, resultCode is 0
08-05 09:02:34.355 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:168)]GetRemoteDeviceName end
08-05 09:02:34.356 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: re-get device name
08-05 09:02:34.356 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: 46:68:40:10:14:A9 1, 9, 20, 2
08-05 09:02:34.356 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: content len 31, dataLen 1
08-05 09:02:34.356 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: found complete local name 46:68:40:10:14:A9, dataLen 1
08-05 09:02:34.356 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: not get device name
08-05 09:02:34.356 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:155)]GetRemoteDeviceName start
08-05 09:02:34.356 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_utils.cpp(ParseString:47)]param=4.
08-05 09:02:34.356 1908-1918/com.example.getbthb I 06000/Bluetooth: [bluetooth_remote_device.cpp(GetDeviceName:158)]../../foundation/communication/bluetooth/interfaces/innerkits/native_cpp/framework/src/bluetooth_remote_device.cpp: GetDeviceName(): Enter!
08-05 09:02:34.357 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:168)]GetRemoteDeviceName end
08-05 09:02:34.358 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: re-get device name
08-05 09:02:34.358 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: 30:BC:A6:B4:05:B2 2, 1, 1a, 2
08-05 09:02:34.358 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: content len 31, dataLen 2
08-05 09:02:34.358 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: content len 31, dataLen 2
08-05 09:02:34.358 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: content len 31, dataLen 10
08-05 09:02:34.358 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: not get device name
08-05 09:02:34.358 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:155)]GetRemoteDeviceName start
08-05 09:02:34.358 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_utils.cpp(ParseString:47)]param=4.
08-05 09:02:34.359 1908-1918/com.example.getbthb I 06000/Bluetooth: [bluetooth_remote_device.cpp(GetDeviceName:158)]../../foundation/communication/bluetooth/interfaces/innerkits/native_cpp/framework/src/bluetooth_remote_device.cpp: GetDeviceName(): Enter!
08-05 09:02:34.360 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:168)]GetRemoteDeviceName end
08-05 09:02:34.360 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: re-get device name
08-05 09:02:34.360 1908-1918/com.example.getbthb I 03b00/JSApp: app Log: EA:D8:D1:E5:1B:AB 0, 0, 0, 0
08-05 09:02:34.360 1908-1918/com.example.getbthb D 03b00/JSApp: app Log: not get device name
08-05 09:02:34.360 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_host.cpp(GetRemoteDeviceName:155)]GetRemoteDeviceName start
08-05 09:02:34.360 1908-1918/com.example.getbthb I 05310/bluetooth_napi: [napi_bluetooth_utils.cpp(ParseString:47)]param=4.

更多关于HarmonyOS 鸿蒙Next 调用 bluetooth.BLE.startBLEScan 获取不到设备名称,且返回广播数据异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

楼主您好,建议您参考下这个蓝牙sample:https://gitee.com/openharmony/app_samples/tree/master/device/Bluetooth

更多关于HarmonyOS 鸿蒙Next 调用 bluetooth.BLE.startBLEScan 获取不到设备名称,且返回广播数据异常的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


你好,这个示例是用于经典蓝牙设备,不是LE的设备,而且里面也没有关于获取设备名称的代码。

有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html

楼主你好,麻烦提供下SDK版本和OS设备型号及OS系统版本。

SDK版本是 3.2.2.5,OS版本是 ohos 3.1 release,设备型号是 RK3568,

欢迎开发小伙伴们进来帮帮楼主

针对HarmonyOS鸿蒙Next在调用bluetooth.BLE.startBLEScan时获取不到设备名称且返回广播数据异常的问题,可能的原因及解决方案如下:

  1. 权限问题:确保应用已正确申请并获得了蓝牙相关的权限,包括访问蓝牙设备、扫描蓝牙设备等权限。

  2. 设备兼容性:不同蓝牙设备在广播数据格式上可能存在差异,导致鸿蒙系统在解析时出现问题。确认设备是否支持标准的蓝牙广播格式。

  3. 广播数据格式:检查广播数据中是否包含设备名称字段(通常是0x090x08的AD类型),以及该字段是否被正确填充。

  4. 扫描参数:确认扫描参数(如扫描窗口、扫描间隔等)是否设置合理,过短的扫描窗口或间隔可能导致无法有效捕获广播数据。

  5. 系统Bug:若以上均无误,可能是鸿蒙系统自身的Bug导致的问题,可关注鸿蒙系统的更新日志,看是否有相关修复。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部