don/cordova-plugin-ble-central

Exception after turning off bluetooth while scanning devices

AngeNew opened this issue ยท 6 comments

Hi, I have a problem, I am using ble-central plugin in ionic app, but after turning bluetooth while bluetooth scanning devices, I have this error:
E/AndroidRuntime: FATAL EXCEPTION: JavaBridge
Process: io.ionic.starter, PID: 8409
java.lang.IllegalStateException: BT Adapter is not turned ON
at android.bluetooth.le.BluetoothLeUtils.checkAdapterStateOn(BluetoothLeUtils.java:136)
at android.bluetooth.le.BluetoothLeScanner.stopScan(BluetoothLeScanner.java:256)
at com.megster.cordova.ble.central.BLECentralPlugin$5.run(BLECentralPlugin.java:887)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.os.HandlerThread.run(HandlerThread.java:65)
How can I trouble this problem??
Thanks

I can confirm this is a bug in the BLE plugin itself. It seems the stopScan call ought to be gated behind a check to see if the Bluetooth adapter is still enabled

bluetoothLeScanner.stopScan(leScanCallback);

Thanks for your response, app crash after this error. Do I avoid turning off bluetooth?? is it only solution??

Keeping the Bluetooth on would certainly stop the crash happening. Out of interest, why are you turning off Bluetooth during the scanning?

It would be great to fix the issue in the plugin though. Patches welcome if you have the time to help... otherwise I'll keep it on my radar for a fix when I get a chance ๐Ÿ™‚

I turning off bluetooth for a possible senior user case :), If I have more time in some weekend, I try to trouble this problem.

Ah yes... test cases always cause troubles ๐Ÿ˜†

The right fix here for the plugin would be two parts:

  1. Add a check here to check whether bluetoothAdapter.getState() is still STATE_ON before the stopScan call is made
  2. The second bit would be to surround the stopScan call in a try/catch (no log needed) so that any errors resulting from calling stop scan don't terminate the application.

For me is also right way.
In this weekend, I try to modify, but I don't assure :)