PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
665 stars 326 forks source link

Proper workflow for performing device bonding #531

Open jhewitt opened 2 years ago

jhewitt commented 2 years ago

I originally added this to an existing issue but believe it warrants more visibility. In addition to the existing Android 12 and security patch problems reported in other issues, we need to determine when a device bonding process has completed based on our observations below. This is not a problem on all iOS versions tested to date. We are encountering this problem on a Pixel 5 running Android 12.2 with all security patches applied.

The device we are attempting to bond with requires a PIN code which Android and iOS manage themselves. We have permissions handled but need a way to determine when bonding has completed. How do we determine using this PUB when to request services and set up characteristic notifications? We are encountering the following error when attempting this following the connected event, this same workflow works properly on all iOS versions tested to date. Note the error response to discovering services:

D/BluetoothAdapter(32345): isLeEnabled(): ON I/flutter (32345): D BluetoothServiceImpl._connectDevice: connecting 68A I/flutter (32345): D SmartLinkRepositoryImpl.connect: connect to device D/BluetoothGatt(32345): connect() - device: 00:A0:50:3E:A6:8A, auto: false D/BluetoothGatt(32345): registerApp() D/BluetoothGatt(32345): registerApp() - UUID=affa0af0-9efb-4c48-bec6-c8036b673079 I/flutter (32345): D SmartLinkRepositoryImpl._connectAndSubscribeToConnectionState.: ble connect state: ConnectionStateUpdate(deviceId: 00:A0:50:3E:A6:8A, connectionState: DeviceConnectionState.connecting, failure: null) D/BluetoothGatt(32345): onClientRegistered() - status=0 clientIf=9 D/BluetoothGatt(32345): onClientConnectionState() - status=0 clientIf=9 device=00:A0:50:3E:A6:8A I/flutter (32345): D SmartLinkRepositoryImpl._connectAndSubscribeToConnectionState.: ble connect state: ConnectionStateUpdate(deviceId: 00:A0:50:3E:A6:8A, connectionState: DeviceConnectionState.connected, failure: null) I/flutter (32345): D SmartLinkRepositoryImpl._subscribeToCharacteristics: ble Discovering Services I/flutter (32345): D SmartLinkRepositoryImpl._subscribeToCharacteristics: ble Error: PlatformException(service_discovery_failure, Bonding is in progress wait for bonding to be finished before executing more operations on the device, null, null) D/BluetoothGatt(32345): onConnectionUpdated() - Device=00:A0:50:3E:A6:8A interval=6 latency=0 timeout=500 status=0 D/BluetoothGatt(32345): onConnectionUpdated() - Device=00:A0:50:3E:A6:8A interval=36 latency=0 timeout=500 status=0 D/BluetoothGatt(32345): onClientConnectionState() - status=0 clientIf=9 device=00:A0:50:3E:A6:8A D/BluetoothGatt(32345): close() D/BluetoothGatt(32345): unregisterApp() - mClientIf=9

_Originally posted by @jhewitt in https://github.com/PhilipsHue/flutter_reactive_ble/issues/530#issuecomment-1062937242_

jhewitt commented 2 years ago

I should mention that we have used this PUB successfully on other projects for both iOS and Android with devices that utilize the 'Just Works' pairing workflow.

remonh87 commented 2 years ago

Understandable request and I agree this difficult without a method that make sure bonding is there. iOS doesn't have a mechanism to determine the bonding state but android does have it. Maybe we should consider exposing an extra check specifically for android where you can request the bonding state of the device. @Taym95 it goes a bit against our philosophy of not introducing platform specific methods. What are your thoughts? This is not the first issue in this area and bonding on Android works very differently

jhewitt commented 2 years ago

iOS and Android handle the bonding process differently. Typically Just-Works bonding takes place when the central device accesses a secure characteristic, at which time the OS takes over and performs BMS handshaking with the peripheral. Both Android and iOS delay the read or write response until BMS completes. When a device has all characteristics defined as secure, or when comparison, passkey or out of band workflows are used; this occurs during the connection process and iOS delays reporting the connect event until BMS is complete. The Android Bluetooth stack does not delay reporting of the connect event.

petemillermultimedia commented 1 year ago

Encountering this exact problem. Older phone and Android version, but the description is the same. Adding this to bump the issue.

invented-pro commented 1 year ago

Exact same issue on Android.

pzehle commented 11 months ago

I have this problem on Android as well. I can't find a workaround or anything I can do to get through this error.

chris-otani commented 10 months ago

I'm running into this issue as well.

AurimassKl commented 9 months ago

Same issue, maybe someone found how to solve it?

enseitankad0 commented 6 months ago

Bump. Pixel 3XL with Android 12 on board, the same issue.

marioalemn commented 6 months ago

I got the same issue with Android.

rowdy15 commented 5 months ago

I've got the same issue with android. I'm trying to use a work around with flutter MethodChannel function calls, but I still haven't got it working exactly how I would like.