1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-27 10:07:32 +01:00

Attempting to fix some connection problems after getting disconnected

This commit is contained in:
cpfeiffer 2015-08-14 00:23:01 +02:00
parent eb2332c8be
commit eec7fae288
2 changed files with 23 additions and 8 deletions

View File

@ -133,7 +133,7 @@ public class DeviceCommunicationService extends Service {
} }
} }
// when we get past this, we should have a valid mDeviceSupport and mGBDevice instances // when we get past this, we should have valid mDeviceSupport and mGBDevice instances
switch (action) { switch (action) {
case ACTION_START: case ACTION_START:

View File

@ -80,6 +80,10 @@ public final class BtLEQueue {
mAbortTransaction = false; mAbortTransaction = false;
// Run all actions of the transaction until one doesn't succeed // Run all actions of the transaction until one doesn't succeed
for (BtLEAction action : transaction.getActions()) { for (BtLEAction action : transaction.getActions()) {
if (mAbortTransaction) { // got disconnected
LOG.info("Aborting running transaction");
break;
}
mWaitCharacteristic = action.getCharacteristic(); mWaitCharacteristic = action.getCharacteristic();
mWaitForActionResultLatch = new CountDownLatch(1); mWaitForActionResultLatch = new CountDownLatch(1);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
@ -169,11 +173,12 @@ public final class BtLEQueue {
public void disconnect() { public void disconnect() {
synchronized (mGattMonitor) { synchronized (mGattMonitor) {
if (mBluetoothGatt != null) { BluetoothGatt gatt = mBluetoothGatt;
LOG.info("Disconnecting BtLEQueue from GATT device"); if (gatt != null) {
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
mBluetoothGatt = null; mBluetoothGatt = null;
LOG.info("Disconnecting BtLEQueue from GATT device");
gatt.disconnect();
gatt.close();
setDeviceConnectionState(State.NOT_CONNECTED); setDeviceConnectionState(State.NOT_CONNECTED);
} }
} }
@ -181,6 +186,7 @@ public final class BtLEQueue {
private void handleDisconnected(int status) { private void handleDisconnected(int status) {
mTransactions.clear(); mTransactions.clear();
mAbortTransaction = true;
if (mWaitForActionResultLatch != null) { if (mWaitForActionResultLatch != null) {
mWaitForActionResultLatch.countDown(); mWaitForActionResultLatch.countDown();
} }
@ -191,17 +197,26 @@ public final class BtLEQueue {
// To support automatic reconnection, we keep the mBluetoothGatt instance // To support automatic reconnection, we keep the mBluetoothGatt instance
// alive (we do not close() it). Unfortunately we sometimes have problems // alive (we do not close() it). Unfortunately we sometimes have problems
// reconnecting automatically, so we try to fix this by re-creating mBluetoothGatt. // reconnecting automatically, so we try to fix this by re-creating mBluetoothGatt.
// Not sure if this actually works without re-initializing the device...
if (status != 0) { if (status != 0) {
maybeReconnect(); if (!maybeReconnect()) {
disconnect(); // ensure that we start over cleanly next time
}
} }
} }
private void maybeReconnect() { /**
* Depending on certain criteria, connects to the BluetoothGatt.
* @return true if a reconnection attempt was made, or false otherwise
*/
private boolean maybeReconnect() {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
if (currentTime - lastReconnectTime >= MIN_MILLIS_BEFORE_RECONNECT) { if (currentTime - lastReconnectTime >= MIN_MILLIS_BEFORE_RECONNECT) {
LOG.info("Automatic reconnection attempt...");
lastReconnectTime = currentTime; lastReconnectTime = currentTime;
connect(); return connect();
} }
return false;
} }
public void dispose() { public void dispose() {