From a086641dff231f2cfeb9816ae416aa9727b0de75 Mon Sep 17 00:00:00 2001 From: raphael Date: Sun, 18 Mar 2018 14:46:38 +0200 Subject: [PATCH] display rssi and signal level, sort by rssi --- btscanselector/build.gradle | 4 ++-- .../btscanselector/BTScanSelectorAdapter.java | 12 ++++++++++-- .../btscanselector/BTScanSelectorDialog.java | 8 ++++++++ .../btscanselector/BTScanSelectorViewModel.java | 14 ++++++++++---- btscanselector/src/main/res/values/strings.xml | 1 + 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/btscanselector/build.gradle b/btscanselector/build.gradle index 50add6e..6302016 100644 --- a/btscanselector/build.gradle +++ b/btscanselector/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 26 - versionCode 8 - versionName "1.0.7" + versionCode 9 + versionName "1.0.8" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorAdapter.java b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorAdapter.java index e140a08..1b05b54 100644 --- a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorAdapter.java +++ b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorAdapter.java @@ -39,8 +39,8 @@ public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { @Override public void onBindViewHolder(BindingViewHolder holder, int position) { final BTScanResultItem device = viewModel.getDevices().get(position); - holder.getViewDataBinding().setVariable(BR.device, device); - holder.getViewDataBinding().setVariable(BR.viewModel, viewModel); + holder.getViewDataBinding().setVariable(com.phearme.btscanselector.BR.device, device); + holder.getViewDataBinding().setVariable(com.phearme.btscanselector.BR.viewModel, viewModel); holder.getViewDataBinding().executePendingBindings(); } @@ -54,4 +54,12 @@ void terminate(Context context) { viewModel.terminate(context); } } + + public boolean isScanning() { + return viewModel.isScanning(); + } + + public void refresh() { + viewModel.refresh(); + } } diff --git a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorDialog.java b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorDialog.java index 363fe64..a578e9d 100644 --- a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorDialog.java +++ b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorDialog.java @@ -49,6 +49,14 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view) .setTitle(R.string.nearbyDevices) + .setNeutralButton(R.string.refresh, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + if (mAdapter != null) { + mAdapter.refresh(); + } + } + }) .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { diff --git a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java index e880832..84fbf3d 100644 --- a/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java +++ b/btscanselector/src/main/java/com/phearme/btscanselector/BTScanSelectorViewModel.java @@ -36,7 +36,7 @@ public void onReceive(Context context, Intent intent) { if (devices != null && !devices.contains(deviceItem) && dataEvents != null && callbacks.onDeviceFound(deviceItem.getBluetoothDevice())) { devices.add(deviceItem); Collections.sort(devices, comparatorByRssi); - notifyPropertyChanged(BR.devices); + notifyPropertyChanged(com.phearme.btscanselector.BR.devices); dataEvents.onDataChange(); } break; @@ -73,7 +73,7 @@ public List getDevices() { public void setDevices(List devices) { this.devices = devices; - notifyPropertyChanged(BR.devices); + notifyPropertyChanged(com.phearme.btscanselector.BR.devices); } @Bindable @@ -83,7 +83,7 @@ public boolean isScanning() { public void setScanning(boolean scanning) { this.scanning = scanning; - notifyPropertyChanged(BR.scanning); + notifyPropertyChanged(com.phearme.btscanselector.BR.scanning); } public void onItemResultClick(BTScanResultItem device) { @@ -106,7 +106,13 @@ void terminate(Context context) { private class ComparatorByRssi implements Comparator { @Override public int compare(BTScanResultItem btScanResultItem, BTScanResultItem t1) { - return btScanResultItem.getRssi() - t1.getRssi(); + return t1.getRssi() - btScanResultItem.getRssi(); + } + } + + public void refresh() { + if (!isScanning()) { + BluetoothAdapter.getDefaultAdapter().startDiscovery(); } } } diff --git a/btscanselector/src/main/res/values/strings.xml b/btscanselector/src/main/res/values/strings.xml index c766cf0..2586a50 100644 --- a/btscanselector/src/main/res/values/strings.xml +++ b/btscanselector/src/main/res/values/strings.xml @@ -2,4 +2,5 @@ btscanselector Nearby Devices Close + Refresh