diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index bfcd0c5f..591ab410 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -16,9 +16,8 @@
-
+
+
deviceCandidates = new ArrayList<>();
private ListView deviceCandidatesView;
private DeviceCandidateAdapter cadidateListAdapter;
private Button startButton;
- private boolean isScanning;
- private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
- @Override
- public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
- DeviceCandidate candidate = new DeviceCandidate(device, (short) rssi);
- if (DeviceHelper.getInstance().isSupported(candidate)) {
- int index = deviceCandidates.indexOf(candidate);
- if (index >= 0) {
- deviceCandidates.set(index, candidate); // replace
- } else {
- deviceCandidates.add(candidate);
- }
- cadidateListAdapter.notifyDataSetChanged();
- }
- }
- };
+ private Scanning isScanning = Scanning.SCANNING_OFF;
+
+ private enum Scanning {
+ SCANNING_BT,
+ SCANNING_BTLE,
+ SCANNING_OFF
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -157,7 +145,7 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
public void onStartButtonClick(View button) {
Log.d(TAG, "Start Button clicked");
- if (isScanning) {
+ if (isScanning()) {
stopDiscovery();
} else {
startDiscovery();
@@ -170,29 +158,51 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
super.onDestroy();
}
+ private void handleDeviceFound(BluetoothDevice device, short rssi) {
+ DeviceCandidate candidate = new DeviceCandidate(device, (short) rssi);
+ if (DeviceHelper.getInstance().isSupported(candidate)) {
+ int index = deviceCandidates.indexOf(candidate);
+ if (index >= 0) {
+ deviceCandidates.set(index, candidate); // replace
+ } else {
+ deviceCandidates.add(candidate);
+ }
+ cadidateListAdapter.notifyDataSetChanged();
+ }
+ }
+
/**
- * Pre: bluetooth is available, enabled and scanning is off
+ * Pre: bluetooth is available, enabled and scanning is off.
+ * Post: BT is discovering
*/
private void startDiscovery() {
- if (isScanning) {
+ if (isScanning()) {
Log.w(TAG, "Not starting BLE discovery, because already scanning.");
return;
}
Log.i(TAG, "Starting discovery...");
- discoveryStarted(); // just to make sure
+ discoveryStarted(Scanning.SCANNING_BT); // just to make sure
if (ensureBluetoothReady()) {
- startBLEDiscovery();
+ startBTDiscovery();
} else {
discoveryFinished();
Toast.makeText(this, "Enable Bluetooth to discover devices.", Toast.LENGTH_LONG).show();
}
}
+ private boolean isScanning() {
+ return isScanning != Scanning.SCANNING_OFF;
+ }
+
private void stopDiscovery() {
Log.i(TAG, "Stopping discovery");
- if (isScanning) {
- adapter.stopLeScan(leScanCallback);
+ if (isScanning()) {
+ if (isScanning == Scanning.SCANNING_BT) {
+ stopBTDiscovery();
+ } else if (isScanning == Scanning.SCANNING_BTLE) {
+ stopBTLEDiscovery();
+ }
handler.removeMessages(0, stopRunnable);
// unfortunately, we never get a call back when stopping the scan, so
// we do it manually:
@@ -200,6 +210,31 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
}
}
+ private void stopBTLEDiscovery() {
+ adapter.stopLeScan(leScanCallback);
+ }
+
+ private void stopBTDiscovery() {
+ adapter.cancelDiscovery();
+ }
+
+ private void bluetoothStateChanged(int oldState, int newState) {
+ discoveryFinished();
+ startButton.setEnabled(newState == BluetoothAdapter.STATE_ON);
+ }
+
+ private void discoveryFinished() {
+ isScanning = Scanning.SCANNING_OFF;
+ progressView.setVisibility(View.GONE);
+ startButton.setText(getString(R.string.discovery_start_scanning));
+ }
+
+ private void discoveryStarted(Scanning what) {
+ isScanning = what;
+ progressView.setVisibility(View.VISIBLE);
+ startButton.setText(getString(R.string.discovery_stop_scanning));
+ }
+
private boolean ensureBluetoothReady() {
boolean available = checkBluetoothAvailable();
startButton.setEnabled(available);
@@ -229,12 +264,18 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
return true;
}
- private void startBLEDiscovery() {
+ private void startBTLEDiscovery() {
handler.removeMessages(0, stopRunnable);
handler.postDelayed(stopRunnable, SCAN_DURATION);
adapter.startLeScan(leScanCallback);
}
+ private void startBTDiscovery() {
+ handler.removeMessages(0, stopRunnable);
+ handler.postDelayed(stopRunnable, SCAN_DURATION);
+ adapter.startDiscovery();
+ }
+
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
DeviceCandidate deviceCandidate = deviceCandidates.get(position);
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 5240fb73..cbc841dc 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -89,7 +89,7 @@
No valid user data given, using dummy user data for now.
When your Mi Band vibrates and blinks, tap it a few times in a row.
Install
- Currently connected devices will likely not be discovered.
+ Make your device discoverable. Currently connected devices will likely not be discovered.
Note:
Device Image
About You