From 33f0922d871e43fb9749d1acaada0322dc92cedd Mon Sep 17 00:00:00 2001 From: raphael Date: Mon, 2 Apr 2018 10:20:50 +0300 Subject: [PATCH] handle devices with no default bt device --- README.md | 2 +- btscanselector/build.gradle | 4 ++-- .../btscanselector/BTScanSelectorViewModel.java | 12 +++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5dec0e1..40becf0 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Add the dependency to the **Module** gradle file: ```gradle dependencies { ... - compile 'com.github.phearme:bt-scan-selector:1.1.4' + compile 'com.github.phearme:bt-scan-selector:1.1.5' } ``` Enable databinding in the **Module** grade file: diff --git a/btscanselector/build.gradle b/btscanselector/build.gradle index 62c5502..7583688 100644 --- a/btscanselector/build.gradle +++ b/btscanselector/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 26 - versionCode 17 - versionName "1.1.4" + versionCode 18 + versionName "1.1.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java index ca0e9cd..2949446 100644 --- a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java +++ b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java @@ -68,7 +68,9 @@ public void onReceive(Context context, Intent intent) { filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); - BluetoothAdapter.getDefaultAdapter().startDiscovery(); + if (BluetoothAdapter.getDefaultAdapter() != null) { + BluetoothAdapter.getDefaultAdapter().startDiscovery(); + } } @Bindable @@ -99,7 +101,9 @@ public void onItemResultClick(BTScanResultItem device) { void terminate(Context context) { try { - BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); + if (BluetoothAdapter.getDefaultAdapter() != null) { + BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); + } if (mReceiver != null) { context.unregisterReceiver(mReceiver); } @@ -118,7 +122,9 @@ public int compare(BTScanResultItem btScanResultItem, BTScanResultItem t1) { void refresh() { if (!isScanning()) { devices = new ArrayList<>(); - BluetoothAdapter.getDefaultAdapter().startDiscovery(); + if (BluetoothAdapter.getDefaultAdapter() != null) { + BluetoothAdapter.getDefaultAdapter().startDiscovery(); + } } }