Make reconnect logic more device agnostic and remove pebble reconnect logic

This commit is contained in:
Andreas Shimokawa 2019-11-20 10:53:25 +01:00
parent c7053747cd
commit c2db30274f
3 changed files with 24 additions and 41 deletions

View File

@ -328,7 +328,6 @@ public final class BtLEQueue {
boolean result = mBluetoothGatt.connect();
if (result) {
setDeviceConnectionState(State.WAITING_FOR_RECONNECT);
AutoConnectIntervalReceiver.scheduleReconnect();
}
return result;
}

View File

@ -28,6 +28,8 @@ import android.webkit.ValueCallback;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -44,7 +46,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.ExternalPebbleJSActivity;
@ -110,7 +111,7 @@ class PebbleIoThread extends GBDeviceIoThread {
}
}
public static void sendAppMessage(GBDeviceEventAppMessage message) {
private static void sendAppMessage(GBDeviceEventAppMessage message) {
final String jsEvent;
try {
WebViewSingleton.getInstance().checkAppRunning(message.appUUID);
@ -190,7 +191,7 @@ class PebbleIoThread extends GBDeviceIoThread {
mOutStream = new PipedOutputStream();
mPebbleLESupport = new PebbleLESupport(this.getContext(), btDevice, (PipedInputStream) mInStream, (PipedOutputStream) mOutStream);
} else {
ParcelUuid uuids[] = btDevice.getUuids();
ParcelUuid[] uuids = btDevice.getUuids();
if (uuids == null) {
return false;
}
@ -364,7 +365,7 @@ class PebbleIoThread extends GBDeviceIoThread {
mInStream.skip(2);
}
GBDeviceEvent deviceEvents[] = mPebbleProtocol.decodeResponse(buffer);
GBDeviceEvent[] deviceEvents = mPebbleProtocol.decodeResponse(buffer);
if (deviceEvents == null) {
LOG.info("unhandled message to endpoint " + endpoint + " (" + length + " bytes)");
} else {
@ -386,34 +387,12 @@ class PebbleIoThread extends GBDeviceIoThread {
if (e.getMessage() != null && (e.getMessage().equals("broken pipe") || e.getMessage().contains("socket closed"))) { //FIXME: this does not feel right
LOG.info(e.getMessage());
mIsConnected = false;
int reconnectAttempts = prefs.getInt("pebble_reconnect_attempts", 10);
if (!mQuit && GBApplication.getGBPrefs().getAutoReconnect() && reconnectAttempts > 0) {
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);
gbDevice.sendDeviceUpdateIntent(getContext());
long delaySeconds = 1;
while (reconnectAttempts-- > 0 && !mQuit && !mIsConnected) {
LOG.info("Trying to reconnect (attempts left " + reconnectAttempts + ")");
mIsConnected = connect();
if (!mIsConnected) {
try {
Thread.sleep(delaySeconds * 1000);
} catch (InterruptedException ignored) {
}
if (delaySeconds < 64) {
delaySeconds *= 2;
}
}
}
}
if (!mIsConnected) {
mBtSocket = null;
LOG.info("Bluetooth socket closed, will quit IO Thread");
break;
}
}
}
}
mIsConnected = false;
if (mBtSocket != null) {
try {
@ -426,7 +405,7 @@ class PebbleIoThread extends GBDeviceIoThread {
enablePebbleKitSupport(false);
if (mQuit) {
if (mQuit || !GBApplication.getGBPrefs().getAutoReconnect()) {
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
} else {
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);

View File

@ -57,34 +57,39 @@ public class AutoConnectIntervalReceiver extends BroadcastReceiver {
if (action == null) {
return;
}
GBDevice gbDevice = service.getGBDevice();
if (gbDevice == null) {
return;
}
if (action.equals(DeviceManager.ACTION_DEVICES_CHANGED)) {
if (gbDevice.isInitialized()) {
LOG.info("will reset connection delay, device is initialized!");
mDelay = 4;
}
else if (gbDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
scheduleReconnect();
}
}
else if (action.equals("GB_RECONNECT")) {
if (gbDevice != null) {
if (gbDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
LOG.info("Will re-connect to " + gbDevice.getAddress() + "(" + gbDevice.getName() + ")");
GBApplication.deviceService().connect();
}
}
}
}
public static void scheduleReconnect() {
public void scheduleReconnect() {
mDelay*=2;
if (mDelay > 128) {
mDelay = 128;
if (mDelay > 64) {
mDelay = 64;
}
scheduleReconnect(mDelay);
}
public static void scheduleReconnect(int delay) {
LOG.info("schduling reconnect in " + delay + " seconds");
public void scheduleReconnect(int delay) {
LOG.info("scheduling reconnect in " + delay + " seconds");
AlarmManager am = (AlarmManager) (GBApplication.getContext().getSystemService(Context.ALARM_SERVICE));
Intent intent = new Intent("GB_RECONNECT");
intent.setPackage(BuildConfig.APPLICATION_ID);