mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-18 22:41:44 +02:00
Try connectGatt with autoConnect = true if it fails with autoConnect = false
See #335
This commit is contained in:
@@ -56,6 +56,7 @@ public abstract class BluetoothCommunication {
|
|||||||
|
|
||||||
private Handler callbackBtHandler;
|
private Handler callbackBtHandler;
|
||||||
private Handler handler;
|
private Handler handler;
|
||||||
|
private static boolean autoConnect = false;
|
||||||
private BluetoothGatt bluetoothGatt;
|
private BluetoothGatt bluetoothGatt;
|
||||||
private boolean connectionEstablished;
|
private boolean connectionEstablished;
|
||||||
private BluetoothGattCallback gattCallback;
|
private BluetoothGattCallback gattCallback;
|
||||||
@@ -449,14 +450,15 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectGatt(BluetoothDevice device) {
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
bluetoothGatt = device.connectGatt(
|
bluetoothGatt = device.connectGatt(
|
||||||
context, false, gattCallback, BluetoothDevice.TRANSPORT_LE);
|
context, autoConnect, gattCallback, BluetoothDevice.TRANSPORT_LE);
|
||||||
}
|
}
|
||||||
else {
|
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 {
|
protected class GattCallback extends BluetoothGattCallback {
|
||||||
@Override
|
@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);
|
Timber.d("onConnectionStateChange: status=%d, newState=%d", status, newState);
|
||||||
|
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
@@ -668,6 +670,9 @@ public abstract class BluetoothCommunication {
|
|||||||
handler.post(new Runnable() {
|
handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (connectionEstablished && status != 0) {
|
||||||
|
autoConnect = !autoConnect;
|
||||||
|
}
|
||||||
setBtStatus(connectionEstablished
|
setBtStatus(connectionEstablished
|
||||||
? BT_STATUS_CODE.BT_CONNECTION_LOST
|
? BT_STATUS_CODE.BT_CONNECTION_LOST
|
||||||
: BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
: BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
|
Reference in New Issue
Block a user