1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-18 14:31:23 +02:00

Try connectGatt with autoConnect = true if it fails with autoConnect = false

See #335
This commit is contained in:
Erik Johansson
2018-10-09 21:41:02 +02:00
parent bbbb91ecdd
commit 2cc98f4416

View File

@@ -56,6 +56,7 @@ public abstract class BluetoothCommunication {
private Handler callbackBtHandler;
private Handler handler;
private static boolean autoConnect = false;
private BluetoothGatt bluetoothGatt;
private boolean connectionEstablished;
private BluetoothGattCallback gattCallback;
@@ -449,14 +450,15 @@ public abstract class BluetoothCommunication {
}
private void connectGatt(BluetoothDevice device) {
Timber.i("Connecting to [%s] (driver: %s)", device.getAddress(), driverName());
Timber.i("Connecting to [%s] (%sdriver: %s)",
device.getAddress(), autoConnect ? "auto connect, " : "", driverName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
bluetoothGatt = device.connectGatt(
context, false, gattCallback, BluetoothDevice.TRANSPORT_LE);
context, autoConnect, gattCallback, BluetoothDevice.TRANSPORT_LE);
}
else {
bluetoothGatt = device.connectGatt(context, false, gattCallback);
bluetoothGatt = device.connectGatt(context, autoConnect, gattCallback);
}
}
@@ -634,7 +636,7 @@ public abstract class BluetoothCommunication {
*/
protected class GattCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) {
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, int newState) {
Timber.d("onConnectionStateChange: status=%d, newState=%d", status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
@@ -668,6 +670,9 @@ public abstract class BluetoothCommunication {
handler.post(new Runnable() {
@Override
public void run() {
if (connectionEstablished && status != 0) {
autoConnect = !autoConnect;
}
setBtStatus(connectionEstablished
? BT_STATUS_CODE.BT_CONNECTION_LOST
: BT_STATUS_CODE.BT_NO_DEVICE_FOUND);