manekinekko / angular-web-bluetooth

The missing Web Bluetooth module for Angular
https://manekinekko.github.io/angular-web-bluetooth/
MIT License
196 stars 59 forks source link

Reading characteristic value failing intermittently after device discovery. #39

Closed manishbansal8843 closed 1 year ago

manishbansal8843 commented 6 years ago

I am trying to build one application for a MI smart band. This application reads the pedometer data. Now, after pairing devices, it fails to read the characteristics value. Post that, once it dispatches notifications of value changed, characteristic values starts to flow in.

Further, this failure is happening only in 90%cases. Once in a while, failure does not happen. It works perfectly fine the very first time.

Now, what i feel is, when we read the characteristics by calling ble.getCharacteristic$(), it returns the promise which generally gets settled before starting the notification on that characteristics. In such case, when we try to read value out of this characteristics, it fails.

Now, in 10 % case, char.startNotifications() gets settled before the above mentioned promise. Hence, the issue do not come while reading the value.

Logs in 10% cases where it works fine.

manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Connecting to GATT Server of BluetoothDevice {id: "gUkAd5nZjn8yRumfGRtIJQ==", name: "Mi Band HRX", gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Getting primary service "0000fee0-0000-1000-8000-00805f9b34fb" (if available) of BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Getting Characteristic "00000007-0000-3512-2118-0009af100700" of BluetoothRemoteGATTService {device: BluetoothDevice, uuid: "0000fee0-0000-1000-8000-00805f9b34fb", isPrimary: true}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Reading Characteristic BluetoothRemoteGATTCharacteristic {service: BluetoothRemoteGATTService, uuid: "00000007-0000-3512-2118-0009af100700", properties: BluetoothCharacteristicProperties, value: null, oncharacteristicvaluechanged: null}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Starting notifications of "00000007-0000-3512-2118-0009af100700"
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Dispatching new characteristic value Event {isTrusted: true, type: "characteristicvaluechanged", target: BluetoothRemoteGATTCharacteristic, currentTarget: BluetoothRemoteGATTCharacteristic, eventPhase: 2, …}
display-mi-band-stats.component.ts:53 Reading pedometer results 

Logs in 90% cases when it fails

manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Connecting to GATT Server of BluetoothDevice {id: "gUkAd5nZjn8yRumfGRtIJQ==", name: "Mi Band HRX", gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Getting primary service "0000fee0-0000-1000-8000-00805f9b34fb" (if available) of BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Getting Characteristic "00000007-0000-3512-2118-0009af100700" of BluetoothRemoteGATTService {device: BluetoothDevice, uuid: "0000fee0-0000-1000-8000-00805f9b34fb", isPrimary: true}
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Reading Characteristic BluetoothRemoteGATTCharacteristic {service: BluetoothRemoteGATTService, uuid: "00000007-0000-3512-2118-0009af100700", properties: BluetoothCharacteristicProperties, value: null, oncharacteristicvaluechanged: null}
core.js:1542 ERROR GATT operation failed for unknown reason.
......
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Starting notifications of "00000007-0000-3512-2118-0009af100700"
manekinekko-angular-web-bluetooth.js:80 [BLE::Info] Dispatching new characteristic value Event {isTrusted: true, type: "characteristicvaluechanged", target: BluetoothRemoteGATTCharacteristic, currentTarget: BluetoothRemoteGATTCharacteristic, eventPhase: 2, …}
display-mi-band-stats.component.ts:53 Reading pedometer results 
manekinekko commented 6 years ago

Hi, thank you for opening this issue. That's indeed a weird behavior! Will merge your PR asap.

manekinekko commented 5 years ago

Hi @manishbansal8843 sorry I couldn't merge your PR as it breaks the demo app when using the Thingy52 device. Can you try again with the new v7.0.0 release of the lib and lmk.

cheers.