HarmonyOS 蓝牙开启订阅后,收到的特征值变化消息,无设备数据
代码如下:
// 发现服务 
discoverServices(clientDevice: ble.GattClientDevice): void { 
  clientDevice.getServices(async (code, gattServices) => { 
  if (code != null) { 
  console.error('getServices error, errCode: ' + (code as BusinessError).code + ', errMessage: ' + (code as BusinessError).message); 
} else { 
  for (let i = 0; i < gattServices.length; i++) { 
    let serviceUuid = gattServices[i].serviceUuid.toUpperCase(); 
    if (serviceUuid == BleDeviceConstants.FTMS_SERVICE_Uuid || 
      serviceUuid == BleDeviceConstants.FEC_SERVICE_Uuid || 
      serviceUuid == BleDeviceConstants.POWER_SERVICE_Uuid || 
      serviceUuid == BleDeviceConstants.CSC_SERVICE_Uuid || 
      serviceUuid == BleDeviceConstants.UART_SERVICE_Uuid || 
      serviceUuid == BleDeviceConstants.HEART_SERVICE_Uuid) { 
      console.error('serviceUuid is ' + serviceUuid); 
      for (let j = 0; j < gattServices[i].characteristics.length; j++) { 
        let characteristicUuid = gattServices[i].characteristics[j].characteristicUuid.toUpperCase(); 
        if (characteristicUuid == BleDeviceConstants.Fec_Notify_Uuid || 
          characteristicUuid == BleDeviceConstants.FTMS_Indoor_Bike_Data_Notify_Uuid || 
          characteristicUuid == BleDeviceConstants.Power_Notify_Uuid || 
          characteristicUuid == BleDeviceConstants.Heart_Notify_Uuid || 
          characteristicUuid == BleDeviceConstants.Speed_Cadence_Notify_Uuid) { 
          console.error('characteristicUuid is ' + characteristicUuid); 
          await this.setNotifyOn(clientDevice, gattServices[i].characteristics[j]); 
        } 
      } 
    } 
   
  this.subscribeCharacteristicChange(clientDevice); 
} 
}) 
} 
// 开启通知 
async setNotifyOn(clientDevice: ble.GattClientDevice, bleCharacteristic: ble.BLECharacteristic) { 
  try { 
    await clientDevice.setCharacteristicChangeNotification(bleCharacteristic, true); 
    console.error('setCharacteristicChangeNotification success, ' + bleCharacteristic.characteristicUuid); 
  } catch (err) { 
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 
  } 
} 
// 订阅设备的特征值变化事件 
subscribeCharacteristicChange(clientDevice: ble.GattClientDevice) { 
  clientDevice.on('BLECharacteristicChange', (data) => { 
    console.error('BLECharacteristicChange, data: ' + JSON.stringify(data)); 
  }); 
}日志:
nodict][napi_async_work.cpp(CallFunction:147)]enter 
04-28 14:16:52.350   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_timer.cpp(Unregister:68)]timerId: 4 
04-28 14:16:52.352   46388-46388  A03D00/JSAPP                   com.tast....4harmony  E     setCharacteristicChangeNotification success, 00002A5B-0000-1000-8000-00805F9B34FB 
04-28 14:16:52.352   46388-46388  A03D00/JSAPP                   com.tast....4harmony  E     serviceUuid is 0000xxx0D-0000-1xxx0-8000-00805F9B34FB 
04-28 14:16:52.352   46388-46388  A03D00/JSAPP                   com.tast....4harmony  E     characteristicUuid is 000xxxx7-xxx0-1000-8000-00805F9B34FB 
04-28 14:16:52.353   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_gatt_client.cpp(setCharacteristicChangeNotification:813)]enter 
04-28 14:16:52.353   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_utils.cpp(ParseArrayBuffer:130)]arraybuffer size is 0 
04-28 14:16:52.353   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_utils.cpp(ParseArrayBuffer:130)]arraybuffer size is 0 
04-28 14:16:52.354   46388-46388  C00101/Bluetooth               com.tast....4harmony  E     [nodict][bluetooth_gatt_client.cpp(GetServices:453)]isGetServiceYet_ is true 
04-28 14:16:52.355   46388-46388  C00101/Bluetooth               com.tast....4harmony  E     [nodict][bluetooth_gatt_characteristic.cpp(SetValue:186)]values is nullptr, or length is 0 
04-28 14:16:52.355   46388-46388  C00101/Bluetooth               com.tast....4harmony  E     [nodict][napi_bluetooth_utils.cpp(NapiIsFunction:517)]Wrong argument type. Function expected. 
04-28 14:16:52.355   46388-46419  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client.cpp(SetNotifyCharacteristic:821)]handle: 0x0040, enable: 1 
04-28 14:16:52.356   46388-46419  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client_proxy.cpp(RequestNotification:529)]BluetoothGattClientProxy::RequestNotification start 
04-28 14:16:52.358   46388-46419  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client_proxy.cpp(WriteDescriptor:323)]BluetoothGattClientProxy::WriteDescriptor start 
04-28 14:16:52.361   46388-46419  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client.cpp(SetNotifyCharacteristicInner:810)]result: 0 
04-28 14:16:52.362   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_async_work.cpp(Complete:53)]needCallback: 1, errCode: 0 
04-28 14:16:52.363   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_timer.cpp(Register:57)]timerId: 5 
04-28 14:16:52.401   46388-46413  C00101/Bluetooth               com.tast....4harmony  E     [nodict][napi_bluetooth_gatt_client_callback.cpp(OnCharacteristicChanged:31)]napi_acquire_threadsafe_function failed, status: 1 
04-28 14:16:52.452   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client_callback_stub.cpp(OnDescriptorWriteInner:145)]BluetoothGattClientCallbackStub::OnDescriptorWriteInner Triggered! 
04-28 14:16:52.452   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][bluetooth_gatt_client.cpp(OnDescriptorWrite:238)]ret:0, length:0 
04-28 14:16:52.452   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_gatt_client_callback.cpp(OnSetNotifyCharacteristic:114)]UUID: 00002A37-0xxx0-1000-8xxx00-0080xxx9B34FB, status: 0 
04-28 14:16:52.452   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_async_work.cpp(CallFunction:147)]enter 
04-28 14:16:52.452   46388-46413  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_timer.cpp(Unregister:68)]timerId: 5 
04-28 14:16:52.454   46388-46388  A03D00/JSAPP                   com.tast....4harmony  E     setCharacteristicChangeNotification success, 00002A37-0000-1000-8000-0xxF9B34FB 
04-28 14:16:52.454   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_gatt_client.cpp(On:266)]enter 
04-28 14:16:52.507   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_ble_utils.cpp(ConvertBLECharacteristicToJS:128)]uuid: 00002A5B-0000-1xx0-8xxx0-008xxx34FB 
04-28 14:16:52.507   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_ble_utils.cpp(ConvertBLEDescriptorVectorToJS:163)]size: 1 
04-28 14:16:52.507   46388-46388  C00101/Bluetooth               com.tast....4harmony  I     [nodict][napi_bluetooth_ble_utils.cpp(ConvertBLEDescriptorToJS:181)]uuid: 00002902-0000-xx00-8xx0-008xxx9B34FB 
04-28 14:16:52.507   46388-46388  C00101/Bluetooth               com.tast....4harmony  E     [nodict][napi_bluetooth_ble_utils.cpp(ConvertBLEDescriptorToJS:207)]memcpy_s error 
04-28 14:16:52.507   46388-46388  A03D00/JSAPP                   com.tast....4harmony  E     BLECharacteristicChange, data: {"characteristicUuid":"000xx5B-0000-1000-8000-00xxxB34FB","serviceUuid":"00xxx816-0000-1000-8000-008xxx34FB","characteristicValue":{},"properties":{"write":false,"writeNoResponse":false,"read":false,"notify":true,"indicate":false},"descriptors":[{"descriptorUuid":"00xx02-0000-1000-8000-00xxxF9B34FB","characteristicUuid":"0000xxx5B-0000-1000-8000-0080xxB34FB","serviceUuid":"000xx16-0000-1xx00-8000-00xxF9B34FB","descriptorValue":{}}]} 
04-28 14:16:52.508   46388-4
        HarmonyOS
      
        赞
        
 收藏 0
 回答 1
 
        待解决
        
相关问题
 HarmonyOS 蓝牙读取特征值问题 
1194浏览  • 1回复 待解决
HarmonyOS 关于向低功耗蓝牙设备写入特定的特征值的问题(打印机数据传输) 
1566浏览  • 1回复 待解决
HarmonyOS 应用需采集HarmonyOS设备唯一标识/特征值 
2361浏览  • 1回复 待解决
HarmonyOS BLE蓝牙监听设备服务变化值是characteristicValue这个参数值吗? 
1272浏览  • 1回复 待解决
HarmonyOS  蓝牙取消订阅扫描,仍然能收到扫描结果回调 
1104浏览  • 1回复 待解决
HarmonyOS 蓝牙权限的问题, 未授权蓝牙权限后,蓝牙权限如何开启 
2008浏览  • 1回复 待解决
emitter.off取消订阅某个事件后,是不是所有订阅该事件的地方都不会再收到该事件的消息 
2903浏览  • 1回复 待解决
#鸿蒙通关秘籍#如何在鸿蒙设备间订阅数据库数据变化? 
1200浏览  • 1回复 待解决
HarmonyOS读取蓝牙设备的广播数据,以及回写数据给蓝牙设备的api 
1422浏览  • 1回复 待解决
HarmonyOS 低功耗蓝牙设备DeviceId会变化 
1425浏览  • 1回复 待解决
devecostudio登录后无设备 
5689浏览  • 3回复 已解决
#鸿蒙学习大百科#键值型数据库如何开启订阅分布式数据变化? 
1152浏览  • 1回复 待解决
#鸿蒙学习大百科#关系型数据库如何开启订阅分布式数据变化? 
1254浏览  • 1回复 待解决
HarmonyOS 在websocket收到消息zip压缩数据,怎么高性能的解压数据 
1181浏览  • 1回复 待解决
使用CommonEventManager.createSubscriber创建订阅关系产生订阅后收不到系统消息 
1657浏览  • 1回复 待解决
HarmonyOS toggle点击后,状态变化的回调会跟随开关值变化 
2155浏览  • 1回复 待解决
HarmonyOS  socket Tcp连接connect成功后无法收到tcp.on('message')的消息 
1682浏览  • 1回复 待解决
HarmonyOS 蓝牙对端设备主动断开连接,"BLEConnectionStateChange"监听未收到回调 
1108浏览  • 1回复 待解决
HarmonyOS 推送后台消息,未收到 
2488浏览  • 1回复 待解决
HarmonyOS 仿evenbus全局订阅,所有订阅页面监听消息 
1034浏览  • 1回复 待解决
HarmonyOS push推送消息的testMessage设置为false无法收到消息 
1256浏览  • 1回复 待解决
请问各位大佬有没有数据变化订阅的完整例子? 
3805浏览  • 1回复 待解决
使用EventHub通信接收到消息后更新@States装饰的变量布局刷新 
2251浏览  • 1回复 待解决
HarmonyOS push可以收到,但点击无反应 
1003浏览  • 1回复 待解决





















ArrayBuffer转json字符串,可以尝试使用Uint8Array操作Arraybuffer,调用TextDecoder对Uint8Array解码。