Pebble: add experimental reconnect support for standby-mode on firmware 3.x

- You should also set reconnect attempts to 0 in preferences when using this.
- It also works when you set flight mode on the pebble, then wait for about 5 minutes and turn BT back on
- Pebble FW 2.x support ist completely untested.
here
Andreas Shimokawa 2015-12-27 19:11:40 +01:00
parent 9f60bf3561
commit 11e02fbf5f
5 changed files with 45 additions and 11 deletions

View File

@ -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,
}
}

View File

@ -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();

View File

@ -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();
}

View File

@ -44,7 +44,7 @@
<string name="when_screen_off">cuando la pantalla está apagada</string>
<string name="never">nunca</string>
<string name="pref_blacklist">Excluir aplicaciones</string>
<string name="pref_title_canned_replies">Respuestas rápidas</string>
<string name="pref_title_canned_replies">Respuestas enlatadas</string>
<string name="pref_header_development">Opciones de desarrollador</string>
<string name="pref_title_development_miaddr">Dirección de MiBand</string>
<string name="pref_title_pebble_settings">Ajustes de Pebble</string>
@ -198,4 +198,5 @@
<string name="pref_title_keep_data_on_device">Mantener los datos de actividad en el dispositivo</string>
<string name="miband_fwinstaller_incompatible_version">Firmware incompatible</string>
<string name="fwinstaller_firmware_not_compatible_to_device">Este firmware no es compatible con tu dispositivo</string>
<string name="miband_prefs_reserve_alarm_calendar">Reserva de alarmas para próximos eventos</string>
</resources>

View File

@ -215,4 +215,5 @@
<string name="miband_fwinstaller_incompatible_version">Incompatible firmware</string>
<string name="fwinstaller_firmware_not_compatible_to_device">This firmware is not compatible with the device</string>
<string name="miband_prefs_reserve_alarm_calendar">Alarms to reserve for upcoming events</string>
<string name="waiting_for_reconnect">waiting for reconnect</string>
</resources>