diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java index f51a9ae53..7900167e1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java @@ -24,7 +24,6 @@ import java.nio.ByteOrder; import java.util.zip.ZipInputStream; import nodomain.freeyourgadget.gadgetbridge.AppManagerActivity; -import nodomain.freeyourgadget.gadgetbridge.GB; import nodomain.freeyourgadget.gadgetbridge.GBCallControlReceiver; import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; @@ -137,11 +136,22 @@ public class PebbleIoThread extends GBDeviceIoThread { gbDevice.setState(GBDevice.State.CONNECTED); gbDevice.sendDeviceUpdateIntent(getContext()); + mIsConnected = true; + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { + Log.i(TAG, "syncing time"); + write(mPebbleProtocol.encodeSetTime(-1)); + } + return true; } @Override public void run() { + gbDevice.setState(GBDevice.State.CONNECTING); + gbDevice.sendDeviceUpdateIntent(getContext()); + mIsConnected = connect(gbDevice.getAddress()); mQuit = !mIsConnected; // quit if not connected @@ -265,28 +275,11 @@ public class PebbleIoThread extends GBDeviceIoThread { bytes += mInStream.read(buffer, bytes + 4, length - bytes); } - if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) { - Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!"); - write(mPebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID)); - write(mPebbleProtocol.encodeFirmwareVersionReq()); - - // this does not really belong here, but since the pebble only asks for our version once it should do the job - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); - if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { - Log.i(TAG, "syncing time"); - write(mPebbleProtocol.encodeSetTime(-1)); - } - } else if (endpoint == PebbleProtocol.ENDPOINT_DATALOG) { - Log.i(TAG, "datalog to endpoint " + endpoint + " (" + length + " bytes)"); - Log.i(TAG, "first two bytes: " + GB.hexdump(buffer, 4, 2)); - write(mPebbleProtocol.encodeDatalog(buffer[5], (byte) 0x85)); + GBDeviceCommand deviceCmd = mPebbleProtocol.decodeResponse(buffer); + if (deviceCmd == null) { + Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + length + " bytes)"); } else { - GBDeviceCommand deviceCmd = mPebbleProtocol.decodeResponse(buffer); - if (deviceCmd == null) { - Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + length + " bytes)"); - } else { - evaluateGBDeviceCommand(deviceCmd); - } + evaluateGBDeviceCommand(deviceCmd); } try { Thread.sleep(100); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java index d72c572da..17408ab47 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java @@ -90,6 +90,8 @@ public class PebbleProtocol extends GBDeviceProtocol { static final byte APPLICATIONMESSAGE_ACK = (byte) 0xff; static final byte APPLICATIONMESSAGE_NACK = (byte) 0x7f; + static final byte DATALOG_TIMEOUT = 7; + static final byte PUTBYTES_INIT = 1; static final byte PUTBYTES_SEND = 2; static final byte PUTBYTES_COMMIT = 3; @@ -108,6 +110,7 @@ public class PebbleProtocol extends GBDeviceProtocol { private final byte SYSTEMMESSAGE_FIRMWARECOMPLETE = 2; private final byte SYSTEMMESSAGE_FIRMWAREFAIL = 3; + static final byte PHONEVERSION_REQUEST = 0; static final byte PHONEVERSION_APPVERSION_MAGIC = 2; // increase this if pebble complains static final byte PHONEVERSION_APPVERSION_MAJOR = 2; static final byte PHONEVERSION_APPVERSION_MINOR = 3; @@ -440,7 +443,7 @@ public class PebbleProtocol extends GBDeviceProtocol { buf.putShort((short) 4); // length of string String temp = "GBT"; buf.put(temp.getBytes()); - buf.put((byte)0x00); + buf.put((byte) 0x00); return buf.array(); } @@ -595,7 +598,7 @@ public class PebbleProtocol extends GBDeviceProtocol { GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); sendBytes.encodedBytes = encodeApplicationMessageTest(); cmd = sendBytes; - } + } break; case APPLICATIONMESSAGE_ACK: Log.i(TAG, "got APPLICATIONMESSAGE ACK"); @@ -610,6 +613,29 @@ public class PebbleProtocol extends GBDeviceProtocol { break; } break; + case ENDPOINT_DATALOG: + if (pebbleCmd != DATALOG_TIMEOUT) { + byte id = buf.get(); + Log.i(TAG, "DATALOG id " + id + " - sending 0x85 (ACK?)"); + GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); + sendBytes.encodedBytes = encodeDatalog(id, (byte) 0x85); + cmd = sendBytes; + } else { + Log.i(TAG, "DATALOG TIMEOUT - ignoring"); + } + break; + case ENDPOINT_PHONEVERSION: + switch (pebbleCmd) { + case PHONEVERSION_REQUEST: + Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!"); + GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); + sendBytes.encodedBytes = encodePhoneVersion(PHONEVERSION_REMOTE_OS_ANDROID); + cmd = sendBytes; + break; + default: + break; + } + break; default: break; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java index 93e0ac9e8..4e621f112 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java @@ -1,7 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge.pebble; import nodomain.freeyourgadget.gadgetbridge.AbstractBTDeviceSupport; -import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol; @@ -9,9 +8,6 @@ public class PebbleSupport extends AbstractBTDeviceSupport { @Override public boolean connect() { - // TODO: state and notification handling should move to IO thread - getDevice().setState(GBDevice.State.CONNECTING); - getDevice().sendDeviceUpdateIntent(getContext()); getDeviceIOThread().start(); return true; }