OpenHarmony开发者论坛

标题: BLE 监听功能不定期发生 CppCrash [打印本页]

作者: 余沧海    时间: 2024-7-15 16:33
标题: BLE 监听功能不定期发生 CppCrash
[md]### 【问题描述】

1. 介绍问题现象和发生的背景

业务场景是一个APP扫描BLE蓝牙设备,连接成功后监听BLE设备发来的信息,发送速度为1秒300次左右。

在监听BLE设备信息的过程中,会不定期的发生 cppCrash,也许5分钟,也许60分钟,时间随机。

2. 相关的代码(请勿使用截图)

开启BLE characteristic 监听

```ts
// 使能 特征值监听
let services: Array<bluetooth.GattService> = await this.gattClientDevice.getServices();
let service: bluetooth.GattService = services.find(item => item.serviceUuid === BleUUID.UUID_SERVICE_HEART_RATE);
let characteristics: Array<bluetooth.BLECharacteristic> = service.characteristics;
let characteristic: bluetooth.BLECharacteristic = characteristics.find(item => item.characteristicUuid === BleUUID.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
this.gattClientDevice.setNotifyCharacteristicChanged(characteristic, true);
let descriptors: Array<bluetooth.BLEDescriptor> = characteristic.descriptors;
let descriptor: bluetooth.BLEDescriptor = descriptors.find(item => item.descriptorUuid === BleUUID.UUID_DESCRIPTOR_HEART_RATE);
descriptor.descriptorValue = ArrayBufferUtils.byteArray2ArrayBuffer([0x01, 0x00]);
this.gattClientDevice.writeDescriptorValue(descriptor);
```

监听回调

```ts
    this.gattClientDevice.on("BLECharacteristicChange", (data: bluetooth.BLECharacteristic) => {
      logManager.Info(`bytes = ${ArrayBufferUtils.arrayBuffer2ByteArray(data.characteristicValue)}`);
    });
```

3. 运行结果、错误截图

cppCrash 日志

```ts
Device info:dayu152
Build info:OpenHarmony 3.2.13.5
Module name:com.example.BLETest
Version:1.0.0
Pid:16370
Uid:20010047
Reason:Signal:SIGSEGV(SEGV_MAPERR)@0x0065766965636562
Thread name:IPC_3_17481
#00 pc 000000000001a4d8 /system/lib64/libace_napi.z.so(napi_reference_ref+36) (9157c78f0444e49cc084bc716d6729cf)
#01 pc 0000000000040e28 /system/lib64/module/libbluetooth.z.so (c361fc6528877883dcd89c91a257d7d2)
#02 pc 00000000000464dc /system/lib64/module/libbluetooth.z.so (c361fc6528877883dcd89c91a257d7d2)
#03 pc 00000000000e2b38 /system/lib64/libbtframework.z.so (ea0050e35407321db0b0b88cef77d5b7)
#04 pc 000000000019dcac /system/lib64/libbtframework.z.so (ea0050e35407321db0b0b88cef77d5b7)
#05 pc 000000000019ece0 /system/lib64/libbtframework.z.so (ea0050e35407321db0b0b88cef77d5b7)
#06 pc 000000000003f46c /system/lib64/chipset-pub-sdk/libipc_core.z.so(OHOS::BinderInvoker::OnTransaction(unsigned char const*)+652) (f59c605b8edfa272287d281e4b8707c3)
#07 pc 000000000003f9d8 /system/lib64/chipset-pub-sdk/libipc_core.z.so(OHOS::BinderInvoker::HandleCommandsInner(unsigned int)+388) (f59c605b8edfa272287d281e4b8707c3)
#08 pc 000000000003ed64 /system/lib64/chipset-pub-sdk/libipc_core.z.so(OHOS::BinderInvoker::StartWorkLoop()+140) (f59c605b8edfa272287d281e4b8707c3)
#09 pc 000000000003faa0 /system/lib64/chipset-pub-sdk/libipc_core.z.so(OHOS::BinderInvoker::JoinThread(bool)+48) (f59c605b8edfa272287d281e4b8707c3)
#10 pc 0000000000039c30 /system/lib64/chipset-pub-sdk/libipc_core.z.so(OHOS::IPCWorkThread::ThreadHandler(void*)+420) (f59c605b8edfa272287d281e4b8707c3)
#11 pc 00000000000d20c8 /system/lib/ld-musl-aarch64.so.1
#12 pc 0000000000077674 /system/lib/ld-musl-aarch64.so.1
```

4. 我尝试过的解决方法和结果
   根据cppCrash 日志,是不是底层蓝牙库的问题,与应用层代码无关?

5. 我想要达到的结果

找到发生该 cppCrash 的核心原因并避免其发生

### 【运行环境】

硬件:CPU-Unisoc T770
ROM版本:
DevEvoStudio版本:DevEco Studio 4.1 Release
SDK版本:API 9 3.2.12.5
[/md]
作者: 深开鸿_王石    时间: 2024-7-16 08:59
看栈顶是napi层的bug, /system/lib64/libace_napi.z.so(napi_reference_ref+36);
看错误信息是segv,段错误就是内存问题;
看描述就是你定期ble扫描然后通过framework的napi接口往js层掉回调或者啥时候出的错,你看看你是不是回调时候参数转换不对还是咋了
作者: 余沧海    时间: 2024-7-17 16:28
回复 深开鸿_王石: 大佬您好,这是参数转换的代码,好像也不会导致内存错误呀。
  1. class ArrayBufferUtils {
  2.   /**
  3.    * byte array to ArrayBuffer
  4.    * @param byteArr
  5.    */
  6.   public static byteArray2ArrayBuffer(byteArr: Array<number>): ArrayBufferLike {
  7.     let ret: ArrayBufferLike = new Uint8Array(byteArr).buffer;
  8.     return ret;
  9.   }

  10.   /**
  11.    * ArrayBuffer to byte array
  12.    * @param arrayBuffer
  13.    */
  14.   public static arrayBuffer2ByteArray(arrayBuffer: ArrayBuffer): Array<number> {
  15.     let ret: Array<number> = Array.prototype.slice.call(new Uint8Array(arrayBuffer));
  16.     return ret;
  17.   }
  18. }
复制代码

这个CppCrash的问题困扰我很久了,找了很久都没有头绪。
作者: 深开鸿_王石    时间: 2024-7-17 16:45
回复 余沧海: 用unstriplib可以看看栈:addr2line, 参考文档https://gitee.com/openharmony/do ... crash-guidelines.md




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5