cordova-plugin-ble-central
cordova-plugin-ble-central copied to clipboard
stopScan reports a null reference when command executed before Bluetooth is enabled
Noticed on some Android 11 variants, might affect other Android versions.
If Bluetooth is disabled when the first BLE Central command is executed bluetoothAdapter.getBluetoothLeScanner() will be null. This null value is cached https://github.com/don/cordova-plugin-ble-central/blob/966e3a5a783e1d710f0c552edb07c93f6921d1c4/src/android/BLECentralPlugin.java#L152, and won't be re-initialised until startScan is called.
Suggest two fixes here:
stopScanshould return success ifbluetoothLeScanneris nullbluetoothLeScannershould be initialised in it's own guard, so that if BLE does become enabled in the above condition it can immediately work.
This has some relation to #826
Fixed in https://github.com/don/cordova-plugin-ble-central/commit/26a142dcc9a4330591cf2b90a32987bc897b8db8