From 11c545362ab4597dcf7333fd4d2982a264984e69 Mon Sep 17 00:00:00 2001 From: boun Date: Thu, 12 Jul 2018 18:25:18 +0200 Subject: [PATCH] Do not crash inside the emulator (#1150) As there is no bluetooth support in the emulator --- .../activities/DiscoveryActivity.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java index f1c3688443..b53f5fd985 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java @@ -399,7 +399,7 @@ private void startDiscovery(Scanning what) { } else if (what == Scanning.SCANNING_NEW_BTLE) { if (GB.supportsBluetoothLE()) { startNEWBTLEDiscovery(); - } else { + } else { discoveryFinished(); } } @@ -433,15 +433,20 @@ private void stopDiscovery() { } private void stopBTLEDiscovery() { - adapter.stopLeScan(leScanCallback); + if (adapter != null) + adapter.stopLeScan(leScanCallback); } private void stopBTDiscovery() { - adapter.cancelDiscovery(); + if (adapter != null) + adapter.cancelDiscovery(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void stopNewBTLEDiscovery() { + if (adapter == null) + return; + BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner(); if (bluetoothLeScanner == null) { LOG.warn("could not get BluetoothLeScanner()!"); @@ -520,10 +525,10 @@ private boolean checkBluetoothAvailable() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void startNEWBTLEDiscovery() { // Only use new API when user uses Lollipop+ device - LOG.info("Start New BTLE Discovery"); - handler.removeMessages(0, stopRunnable); - handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION); - adapter.getBluetoothLeScanner().startScan(getScanFilters(), getScanSettings(), getScanCallback()); + LOG.info("Start New BTLE Discovery"); + handler.removeMessages(0, stopRunnable); + handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION); + adapter.getBluetoothLeScanner().startScan(getScanFilters(), getScanSettings(), getScanCallback()); } private List getScanFilters() {