dotintent / FlutterBleLib

Bluetooth Low Energy library for Flutter with support for simulating peripherals
Apache License 2.0
535 stars 197 forks source link

Exception on iOS after relaunch #523

Closed tmorone-rezi closed 3 years ago

tmorone-rezi commented 3 years ago

I am experiencing an exception on iOS that sometimes occurs when I:

  1. Perform a scan
  2. Connect to the device
  3. Shutdown the app
  4. Relaunch the app
  5. Attempt a scan

On the call to startPeripheralScan I get this error: Exception has occurred. BleError (BleError (Error code: 103, ATT error code: null, iOS error code: null, Android error code: null, reason: null, internal message: null, device ID: null, service UUID: null, characteristic UUID: null, descriptor UUID: null))

This will often resolve itself if I shutdown the app and wait a few moments.

I also see this (sometimes) if I perform a scan, back out of the screen, and then perform the scan again.

My environment: iPhone 11 iOS 13.7 xcode 11.7 flutter 1.20.3 flutter_ble_lib 2.3.0

mikolak commented 3 years ago

103 is Bluetooth in unknown state. It might be a race condition in your code. Can you try adding observation of adapter state? bleManager.observeBluetoothState(emitCurrentState: true)

tmorone-rezi commented 3 years ago

Thank you! That made a huge difference. This will wait for POWERED_ON before doing the peripheral scan:

await bleManager.createClient(); await for (BluetoothState state in bleManager.observeBluetoothState()) { if (state == BluetoothState.POWERED_ON) { break; }}

sejunoh commented 2 years ago

Thank you! That made a huge difference. This will wait for POWERED_ON before doing the peripheral scan:

await bleManager.createClient(); await for (BluetoothState state in bleManager.observeBluetoothState()) { if (state == BluetoothState.POWERED_ON) { break; }}

Great! It works for me.