From 6125594703ad560120e7948bcf0a61242b8ba13b Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Fri, 7 Aug 2015 13:24:54 +0200 Subject: [PATCH] Various changes related to INITIALZED device state Due to a bug in DeviceCommunicationService.isConnected(), devices using the INITIALIZED state only worked when they had useAutoConnect set to true (Mi Band) Now setting devices to INITIALIZED state to allow any action send to DeviceCommunicationService is mandatory. As an exception only REQUEST_VERSIONINFO will be also be allowed for CONNECTED state. This also fixes a problem when notifications came in on the Pebble with 3.x FW before we actually knew it was FW 3.x (INITZALIZED state on the Pebble now implies that we know the FW version) --- .../service/DeviceCommunicationService.java | 13 ++++++++----- .../service/devices/pebble/PebbleIoThread.java | 14 ++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 8eb2a051..c620f512 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -24,7 +24,6 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State; import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; import nodomain.freeyourgadget.gadgetbridge.util.GB; @@ -82,7 +81,7 @@ public class DeviceCommunicationService extends Service { GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); if (mGBDevice.equals(device)) { mGBDevice = device; - boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isConnected()); + boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized()); GB.setReceiversEnableState(enableReceivers, context); GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), context); } else { @@ -124,7 +123,7 @@ public class DeviceCommunicationService extends Service { return START_NOT_STICKY; } - if (mDeviceSupport == null || (!isConnected() && !mDeviceSupport.useAutoConnect())) { + if ((mDeviceSupport == null) || (!(isConnected() && action.equals(ACTION_REQUEST_VERSIONINFO)) && !isInitialized() && !mDeviceSupport.useAutoConnect())) { // trying to send notification without valid Bluetooth connection if (mGBDevice != null) { // at least send back the current device state @@ -275,11 +274,15 @@ public class DeviceCommunicationService extends Service { } private boolean isConnected() { - return mGBDevice != null && mGBDevice.getState() == State.CONNECTED; + return mGBDevice != null && mGBDevice.isConnected(); } private boolean isConnecting() { - return mGBDevice != null && mGBDevice.getState() == State.CONNECTING; + return mGBDevice != null && mGBDevice.isConnecting(); + } + + private boolean isInitialized() { + return mGBDevice != null && mGBDevice.isInitialized(); } @Override 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 ff920162..0ace462f 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 @@ -19,16 +19,16 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.ZipInputStream; -import nodomain.freeyourgadget.gadgetbridge.util.GB; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader; -import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleInstallable; -import nodomain.freeyourgadget.gadgetbridge.service.bt.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagementResult; +import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader; +import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleInstallable; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.service.bt.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.service.bt.GBDeviceProtocol; +import nodomain.freeyourgadget.gadgetbridge.util.GB; public class PebbleIoThread extends GBDeviceIoThread { private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); @@ -89,7 +89,7 @@ public class PebbleIoThread extends GBDeviceIoThread { gbDevice.sendDeviceUpdateIntent(getContext()); mIsConnected = true; - + write(mPebbleProtocol.encodeFirmwareVersionReq()); return true; } @@ -280,6 +280,8 @@ public class PebbleIoThread extends GBDeviceIoThread { LOG.info("syncing time"); write(mPebbleProtocol.encodeSetTime(-1)); } + gbDevice.setState(GBDevice.State.INITIALIZED); + gbDevice.sendDeviceUpdateIntent(getContext()); return false; case APP_MANAGEMENT_RES: GBDeviceEventAppManagementResult appMgmtRes = (GBDeviceEventAppManagementResult) deviceEvent;