Skip to content

Commit

Permalink
display rssi and signal level, sort by rssi
Browse files Browse the repository at this point in the history
  • Loading branch information
r-cohen committed Mar 18, 2018
1 parent f75a803 commit a086641
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions btscanselector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -54,4 +54,12 @@ void terminate(Context context) {
viewModel.terminate(context);
}
}

public boolean isScanning() {
return viewModel.isScanning();
}

public void refresh() {
viewModel.refresh();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ public List<BTScanResultItem> getDevices() {

public void setDevices(List<BTScanResultItem> devices) {
this.devices = devices;
notifyPropertyChanged(BR.devices);
notifyPropertyChanged(com.phearme.btscanselector.BR.devices);
}

@Bindable
Expand All @@ -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) {
Expand All @@ -106,7 +106,13 @@ void terminate(Context context) {
private class ComparatorByRssi implements Comparator<BTScanResultItem> {
@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();
}
}
}
1 change: 1 addition & 0 deletions btscanselector/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">btscanselector</string>
<string name="nearbyDevices">Nearby Devices</string>
<string name="close">Close</string>
<string name="refresh">Refresh</string>
</resources>

0 comments on commit a086641

Please sign in to comment.