From 1b7e3699a73b823e4818da6a88d06bf6d054f0a0 Mon Sep 17 00:00:00 2001 From: blackspherefollower Date: Wed, 2 Oct 2024 15:25:37 +0100 Subject: [PATCH] fix: Correct an Android bug where stopping a scan when there's no longer a Bluetooth adapter/scanner can cause an NPE --- .../com/nonpolynomial/btleplug/android/impl/Adapter.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Adapter.java b/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Adapter.java index 9f04d180..e2a4332b 100644 --- a/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Adapter.java +++ b/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Adapter.java @@ -47,7 +47,13 @@ public void startScan(ScanFilter filter) { @SuppressLint("MissingPermission") public void stopScan() { - BluetoothAdapter.getAdapter().getBluetoothLeScanner().stopScan(this.callback); + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (bluetoothAdapter != null) { + BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); + if (scanner != null) { + scanner.stopScan(this.callback); + } + } } private native void reportScanResult(ScanResult result);