1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-18 22:41:44 +02:00

Wait a bit between stopping LE scan and connecting

To see if it helps @ReidarHH in #335
This commit is contained in:
Erik Johansson
2018-10-08 21:36:19 +02:00
parent 2f1b7663b7
commit 960c7e8f30

View File

@@ -427,7 +427,7 @@ public abstract class BluetoothCommunication {
} }
else { else {
Timber.d("No coarse location permission, connecting without LE scan"); Timber.d("No coarse location permission, connecting without LE scan");
connectGatt(hwAddress); connectGatt(btAdapter.getRemoteDevice(hwAddress));
} }
} }
@@ -460,8 +460,16 @@ public abstract class BluetoothCommunication {
} }
} }
private void connectGatt(String hwAddress) { private void stopLeScanAndConnectGatt(final BluetoothDevice device) {
connectGatt(btAdapter.getRemoteDevice(hwAddress)); stopLeScan();
// Delay the call to connectGatt to let things settle a bit
handler.postDelayed(new Runnable() {
@Override
public void run() {
connectGatt(device);
}
}, 500);
} }
private void startLeScanForDevice(final String hwAddress) { private void startLeScanForDevice(final String hwAddress) {
@@ -476,27 +484,28 @@ public abstract class BluetoothCommunication {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
// Check that callback != null in case the same device is found multiple times
// and thus multiple calls to connect to it are queued. Only the first will
// have callback != null.
if (leScanCallback != null) { if (leScanCallback != null) {
stopLeScan(); stopLeScanAndConnectGatt(device);
connectGatt(device);
} }
} }
}); });
} }
}; };
Timber.d("Starting LE scan for device [%s]", hwAddress);
btAdapter.startLeScan(leScanCallback);
// Stop scan and try to connect to the device directly if the device isn't found in time // Stop scan and try to connect to the device directly if the device isn't found in time
handler.postAtTime(new Runnable() { handler.postAtTime(new Runnable() {
@Override @Override
public void run() { public void run() {
Timber.d("Device not found in LE scan, connecting directly"); Timber.d("Device not found in LE scan, connecting directly");
stopLeScan(); stopLeScanAndConnectGatt(btAdapter.getRemoteDevice(hwAddress));
connectGatt(hwAddress);
} }
}, leScanCallback, SystemClock.uptimeMillis() + LE_SCAN_TIMEOUT_MS); }, leScanCallback, SystemClock.uptimeMillis() + LE_SCAN_TIMEOUT_MS);
Timber.d("Starting LE scan for device [%s]", hwAddress);
btAdapter.startLeScan(leScanCallback);
} }
private void stopLeScan() { private void stopLeScan() {
@@ -515,6 +524,8 @@ public abstract class BluetoothCommunication {
stopLeScan(); stopLeScan();
if (bluetoothGatt == null) { if (bluetoothGatt == null) {
// Could be a pending connectGatt waiting
handler.removeCallbacksAndMessages(null);
return; return;
} }