mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 08:05:55 +01:00
Small improvements to discovery, only scan for 1min #33
This commit is contained in:
parent
dbffd5a42f
commit
0629d6aa5d
@ -9,6 +9,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Parcelable;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
@ -31,6 +32,9 @@ import nodomain.freeyourgadget.gadgetbridge.adapter.DeviceCandidateAdapter;
|
||||
|
||||
public class DiscoveryActivity extends Activity implements AdapterView.OnItemClickListener {
|
||||
private static final String TAG = "DiscoveryAct";
|
||||
private static final long SCAN_DURATION = 60000; // 60s
|
||||
|
||||
private Handler handler = new Handler();
|
||||
|
||||
private BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
@ -52,6 +56,12 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
|
||||
}
|
||||
}
|
||||
};
|
||||
private Runnable stopRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
stopDiscovery();
|
||||
}
|
||||
};
|
||||
|
||||
private void bluetoothStateChanged(int oldState, int newState) {
|
||||
discoveryFinished();
|
||||
@ -164,6 +174,11 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
|
||||
* Pre: bluetooth is available, enabled and scanning is off
|
||||
*/
|
||||
private void startDiscovery() {
|
||||
if (isScanning) {
|
||||
Log.w(TAG, "Not starting BLE discovery, because already scanning.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log.i(TAG, "Starting discovery...");
|
||||
discoveryStarted(); // just to make sure
|
||||
if (ensureBluetoothReady()) {
|
||||
@ -175,8 +190,10 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
|
||||
}
|
||||
|
||||
private void stopDiscovery() {
|
||||
Log.i(TAG, "Stopping discovery");
|
||||
if (isScanning) {
|
||||
adapter.stopLeScan(leScanCallback);
|
||||
handler.removeMessages(0, stopRunnable);
|
||||
// unfortunately, we never get a call back when stopping the scan, so
|
||||
// we do it manually:
|
||||
discoveryFinished();
|
||||
@ -213,6 +230,8 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli
|
||||
}
|
||||
|
||||
private void startBLEDiscovery() {
|
||||
handler.removeMessages(0, stopRunnable);
|
||||
handler.postDelayed(stopRunnable, SCAN_DURATION);
|
||||
adapter.startLeScan(leScanCallback);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user