mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-24 17:23:03 +02:00
@@ -405,16 +405,14 @@ public abstract class BluetoothCommunication {
|
|||||||
* @param hwAddress the Bluetooth address to connect to
|
* @param hwAddress the Bluetooth address to connect to
|
||||||
*/
|
*/
|
||||||
public void connect(String hwAddress) {
|
public void connect(String hwAddress) {
|
||||||
disconnect(false);
|
|
||||||
|
|
||||||
logBluetoothStatus();
|
logBluetoothStatus();
|
||||||
|
|
||||||
|
disconnect(false);
|
||||||
|
btAdapter.cancelDiscovery();
|
||||||
|
|
||||||
// Some good tips to improve BLE connections:
|
// Some good tips to improve BLE connections:
|
||||||
// https://android.jlelse.eu/lessons-for-first-time-android-bluetooth-le-developers-i-learned-the-hard-way-fee07646624
|
// https://android.jlelse.eu/lessons-for-first-time-android-bluetooth-le-developers-i-learned-the-hard-way-fee07646624
|
||||||
|
|
||||||
btAdapter.cancelDiscovery();
|
|
||||||
stopLeScan();
|
|
||||||
|
|
||||||
// Don't do any cleanup if disconnected before fully connected
|
// Don't do any cleanup if disconnected before fully connected
|
||||||
btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE;
|
btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE;
|
||||||
|
|
||||||
@@ -462,18 +460,6 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopLeScanAndConnectGatt(final BluetoothDevice device) {
|
|
||||||
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) {
|
||||||
leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -486,23 +472,23 @@ 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
|
// Check that bluetoothGatt == null in case the same device is found multiple times
|
||||||
// and thus multiple calls to connect to it are queued. Only the first will
|
// and thus multiple calls to connect to it are queued. Only the first will
|
||||||
// have callback != null.
|
// trigger the connect.
|
||||||
if (leScanCallback != null) {
|
if (bluetoothGatt == null) {
|
||||||
stopLeScanAndConnectGatt(device);
|
connectGatt(device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Stop scan and try to connect to the device directly if the device isn't found in time
|
// 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");
|
||||||
stopLeScanAndConnectGatt(btAdapter.getRemoteDevice(hwAddress));
|
connectGatt(btAdapter.getRemoteDevice(hwAddress));
|
||||||
}
|
}
|
||||||
}, leScanCallback, SystemClock.uptimeMillis() + LE_SCAN_TIMEOUT_MS);
|
}, leScanCallback, SystemClock.uptimeMillis() + LE_SCAN_TIMEOUT_MS);
|
||||||
|
|
||||||
@@ -533,6 +519,9 @@ public abstract class BluetoothCommunication {
|
|||||||
|
|
||||||
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
|
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
|
||||||
|
|
||||||
|
handler.removeCallbacksAndMessages(null);
|
||||||
|
callbackBtHandler = null;
|
||||||
|
|
||||||
if (doCleanup) {
|
if (doCleanup) {
|
||||||
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) {
|
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) {
|
||||||
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
|
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
|
||||||
@@ -544,9 +533,7 @@ public abstract class BluetoothCommunication {
|
|||||||
if (openRequest) {
|
if (openRequest) {
|
||||||
handler.postDelayed(this, 10);
|
handler.postDelayed(this, 10);
|
||||||
} else {
|
} else {
|
||||||
bluetoothGatt.close();
|
disconnect(false);
|
||||||
bluetoothGatt = null;
|
|
||||||
handler.removeCallbacksAndMessages(null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -555,9 +542,6 @@ public abstract class BluetoothCommunication {
|
|||||||
bluetoothGatt.close();
|
bluetoothGatt.close();
|
||||||
bluetoothGatt = null;
|
bluetoothGatt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.removeCallbacksAndMessages(null);
|
|
||||||
callbackBtHandler = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user