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
2024-09-09 09:21:19
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

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

let view: Uint8Array = new Uint8Array(arrayBuffer); // 使用Uint8Array读取arrayBuffer的数据 
let textDecoder: util.TextDecoder = util.TextDecoder.create(); // 调用util模块的TextDecoder类 
let res: string = textDecoder.decodeWithStream(view); // 对view解码 
let jsonRes:string = JSON.stringify(res);
分享
微博
QQ
微信
回复
2024-09-09 15:44:25
相关问题
devecostudio登录设备
4711浏览 • 3回复 已解决
怎么监听蓝牙开启状态?
4845浏览 • 1回复 待解决
HarmonyOS 推送后台消息,未收到
1292浏览 • 1回复 待解决
HarmonyOS消息订阅该使用哪个api
398浏览 • 1回复 待解决
物联网产生数据有什么特征
4229浏览 • 1回复 待解决
HarmonyOS 蓝牙API startBLEScan有变化吗?
125浏览 • 1回复 待解决
HarmonyOS感获取设备标识
463浏览 • 1回复 待解决