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 蓝牙权限的问题, 未授权蓝牙权限后,蓝牙权限如何开启
349浏览 • 1回复 待解决
emitter.off取消订阅某个事件后,是不是所有订阅该事件的地方都不会再收到该事件的消息
1583浏览 • 1回复 待解决
devecostudio登录后无设备
4494浏览 • 3回复 已解决
#鸿蒙学习大百科#键值型数据库如何开启订阅分布式数据变化?
132浏览 • 1回复 待解决
#鸿蒙学习大百科#关系型数据库如何开启订阅分布式数据变化?
146浏览 • 1回复 待解决
使用CommonEventManager.createSubscriber创建订阅关系产生订阅后收不到系统消息
616浏览 • 1回复 待解决
HarmonyOS toggle点击后,状态变化的回调会跟随开关值变化
464浏览 • 1回复 待解决
HarmonyOS消息订阅该使用哪个api
238浏览 • 1回复 待解决
怎么监听蓝牙的开启状态?
4709浏览 • 1回复 待解决
物联网产生的数据有什么特征?
4058浏览 • 1回复 待解决
HarmonyOS 推送后台消息,未收到
673浏览 • 1回复 待解决
HarmonyOS 测试应用需要调试订阅推送、实况窗更新消息、通知扩展消息 申请开启此三种场景的推送
153浏览 • 1回复 待解决
请问各位大佬有没有数据变化订阅的完整例子?
2582浏览 • 1回复 待解决
HarmonyOS 用@Watch来订阅数据变化时候,数据中每个属性变化都会导致调用这个@Watch方法
269浏览 • 2回复 待解决
使用EventHub通信接收到消息后更新@States装饰的变量布局刷新
798浏览 • 1回复 待解决
如何订阅系统环境变量的变化
289浏览 • 1回复 待解决
HarmonyOS无感获取设备标识
287浏览 • 1回复 待解决
HarmonyOS 收到 Im 消息如何通知页面刷新?
311浏览 • 1回复 待解决
HarmonyOS无法获取蓝牙设备的真实MAC
346浏览 • 1回复 待解决
HarmonyOS 服务端推送消息,SDK无法收到
284浏览 • 1回复 待解决
如何取消订阅输入法文本内容的变化
275浏览 • 1回复 待解决
HarmonyOS Image组件的图片路径更新为无值的问题
266浏览 • 1回复 待解决
HarmonyOS 音频管理-监听音量变化无反应
226浏览 • 1回复 待解决
HarmonyOS 消息机制emitter一个发送者,多个订阅者,一个取消订阅,造成全部订阅都取消
362浏览 • 1回复 待解决
HarmonyOS 推送通知类消息未收到,已请求通知权限,网络正常
415浏览 • 1回复 待解决
ArrayBuffer转json字符串,可以尝试使用Uint8Array操作Arraybuffer,调用TextDecoder对Uint8Array解码。