diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java index a0292e88..8bb6fddf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java @@ -194,6 +194,8 @@ public class GBDevice implements Parcelable { switch (mState) { case NOT_CONNECTED: return GBApplication.getContext().getString(R.string.not_connected); + case WAITING_FOR_RECONNECT: + return GBApplication.getContext().getString(R.string.waiting_for_reconnect); case CONNECTING: return GBApplication.getContext().getString(R.string.connecting); case CONNECTED: @@ -310,6 +312,7 @@ public class GBDevice implements Parcelable { public enum State { // Note: the order is important! NOT_CONNECTED, + WAITING_FOR_RECONNECT, CONNECTING, CONNECTED, INITIALIZING, @@ -319,6 +322,6 @@ public class GBDevice implements Parcelable { * device name, firmware version, hardware revision (as applicable) is available * in the GBDevice. */ - INITIALIZED + INITIALIZED, } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index 2a0fc220..1d857f14 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; @@ -46,6 +47,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; public class PebbleIoThread extends GBDeviceIoThread { private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); + private static final UUID PEBBLE_UUID_RECONNECT = UUID.fromString("00000000-deca-fade-deca-deafdecacafe"); + private static final UUID PEBBLE_UUID_RECONNECT3X = UUID.fromString("a924496e-cc7c-4dff-8a9f-9a76cc2e9d50"); + public static final String PEBBLEKIT_ACTION_PEBBLE_CONNECTED = "com.getpebble.action.PEBBLE_CONNECTED"; public static final String PEBBLEKIT_ACTION_PEBBLE_DISCONNECTED = "com.getpebble.action.PEBBLE_DISCONNECTED"; public static final String PEBBLEKIT_ACTION_APP_ACK = "com.getpebble.action.app.ACK"; @@ -66,6 +70,7 @@ public class PebbleIoThread extends GBDeviceIoThread { private boolean mIsTCP = false; private BluetoothAdapter mBtAdapter = null; private BluetoothSocket mBtSocket = null; + private BluetoothServerSocket mBtServerSocket = null; private Socket mTCPSocket = null; // for emulator private InputStream mInStream = null; private OutputStream mOutStream = null; @@ -157,7 +162,6 @@ public class PebbleIoThread extends GBDeviceIoThread { mEnablePebblekit = sharedPrefs.getBoolean("pebble_enable_pebblekit", false); } - @Override protected boolean connect(String btDeviceAddress) { GBDevice.State originalState = gbDevice.getState(); @@ -174,6 +178,9 @@ public class PebbleIoThread extends GBDeviceIoThread { mIsTCP = false; BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); ParcelUuid uuids[] = btDevice.getUuids(); + for (ParcelUuid uuid : uuids) { + LOG.info("found service UUID " + uuid); + } mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid()); mBtSocket.connect(); mInStream = mBtSocket.getInputStream(); @@ -347,15 +354,30 @@ public class PebbleIoThread extends GBDeviceIoThread { } catch (IOException e) { if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right LOG.info(e.getMessage()); - gbDevice.setState(GBDevice.State.CONNECTING); - gbDevice.sendDeviceUpdateIntent(getContext()); mIsConnected = false; int reconnectAttempts = Integer.valueOf(sharedPrefs.getString("pebble_reconnect_attempts", "10")); - while (reconnectAttempts-- > 0 && !mQuit) { - LOG.info("Trying to reconnect (attempts left " + reconnectAttempts + ")"); - mIsConnected = connect(gbDevice.getAddress()); - if (mIsConnected) - break; + if (reconnectAttempts > 0) { + gbDevice.setState(GBDevice.State.CONNECTED); + gbDevice.sendDeviceUpdateIntent(getContext()); + while (reconnectAttempts-- > 0 && !mQuit) { + LOG.info("Trying to reconnect (attempts left " + reconnectAttempts + ")"); + mIsConnected = connect(gbDevice.getAddress()); + } + } + if (!mIsConnected) { + try { + gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT); + gbDevice.sendDeviceUpdateIntent(getContext()); + UUID reconnectUUID = mPebbleProtocol.isFw3x ? PEBBLE_UUID_RECONNECT3X : PEBBLE_UUID_RECONNECT; + mBtServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord("PebbleReconnectListener", reconnectUUID); + mBtSocket = mBtServerSocket.accept(); + LOG.info("incoming connection on reconnect uuid (" + reconnectUUID + "), will connect actively"); + mBtSocket.close(); + mIsConnected = connect(gbDevice.getAddress()); + } catch (IOException ex) { + ex.printStackTrace(); + LOG.info("error while reconnecting"); + } } if (!mIsConnected) { mBtSocket = null; @@ -652,6 +674,13 @@ public class PebbleIoThread extends GBDeviceIoThread { e.printStackTrace(); } } + if (mBtServerSocket != null) { + try { + mBtServerSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } if (mTCPSocket != null) { try { mTCPSocket.close(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 18758d39..623e87b1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -1110,7 +1110,7 @@ public class PebbleProtocol extends GBDeviceProtocol { buf.put((byte) 8); // minor buf.put((byte) 1); // patch buf.order(ByteOrder.LITTLE_ENDIAN); - buf.putLong(0x0000000000000003); //flags + buf.putLong(0x00000000000000af); //flags return buf.array(); } diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 77de9cb6..bf5d28be 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -44,7 +44,7 @@ cuando la pantalla está apagada nunca Excluir aplicaciones - Respuestas rápidas + Respuestas enlatadas Opciones de desarrollador Dirección de MiBand Ajustes de Pebble @@ -198,4 +198,5 @@ Mantener los datos de actividad en el dispositivo Firmware incompatible Este firmware no es compatible con tu dispositivo + Reserva de alarmas para próximos eventos diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f1f1c64..f78058a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -215,4 +215,5 @@ Incompatible firmware This firmware is not compatible with the device Alarms to reserve for upcoming events + waiting for reconnect