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(); boolean result = mBluetoothGatt.connect();
if (result) { if (result) {
setDeviceConnectionState(State.WAITING_FOR_RECONNECT); setDeviceConnectionState(State.WAITING_FOR_RECONNECT);
AutoConnectIntervalReceiver.scheduleReconnect();
} }
return result; return result;
} }

View File

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

View File

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