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)
This commit is contained in:
Andreas Shimokawa 2015-08-07 13:24:54 +02:00
parent ce7b42d9d4
commit 6125594703
2 changed files with 16 additions and 11 deletions

View File

@ -24,7 +24,6 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -82,7 +81,7 @@ public class DeviceCommunicationService extends Service {
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
if (mGBDevice.equals(device)) { if (mGBDevice.equals(device)) {
mGBDevice = device; mGBDevice = device;
boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isConnected()); boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized());
GB.setReceiversEnableState(enableReceivers, context); GB.setReceiversEnableState(enableReceivers, context);
GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), context); GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), context);
} else { } else {
@ -124,7 +123,7 @@ public class DeviceCommunicationService extends Service {
return START_NOT_STICKY; 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 // trying to send notification without valid Bluetooth connection
if (mGBDevice != null) { if (mGBDevice != null) {
// at least send back the current device state // at least send back the current device state
@ -275,11 +274,15 @@ public class DeviceCommunicationService extends Service {
} }
private boolean isConnected() { private boolean isConnected() {
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED; return mGBDevice != null && mGBDevice.isConnected();
} }
private boolean isConnecting() { private boolean isConnecting() {
return mGBDevice != null && mGBDevice.getState() == State.CONNECTING; return mGBDevice != null && mGBDevice.isConnecting();
}
private boolean isInitialized() {
return mGBDevice != null && mGBDevice.isInitialized();
} }
@Override @Override

View File

@ -19,16 +19,16 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.zip.ZipInputStream; 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.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagementResult; 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.service.bt.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class PebbleIoThread extends GBDeviceIoThread { public class PebbleIoThread extends GBDeviceIoThread {
private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class);
@ -89,7 +89,7 @@ public class PebbleIoThread extends GBDeviceIoThread {
gbDevice.sendDeviceUpdateIntent(getContext()); gbDevice.sendDeviceUpdateIntent(getContext());
mIsConnected = true; mIsConnected = true;
write(mPebbleProtocol.encodeFirmwareVersionReq());
return true; return true;
} }
@ -280,6 +280,8 @@ public class PebbleIoThread extends GBDeviceIoThread {
LOG.info("syncing time"); LOG.info("syncing time");
write(mPebbleProtocol.encodeSetTime(-1)); write(mPebbleProtocol.encodeSetTime(-1));
} }
gbDevice.setState(GBDevice.State.INITIALIZED);
gbDevice.sendDeviceUpdateIntent(getContext());
return false; return false;
case APP_MANAGEMENT_RES: case APP_MANAGEMENT_RES:
GBDeviceEventAppManagementResult appMgmtRes = (GBDeviceEventAppManagementResult) deviceEvent; GBDeviceEventAppManagementResult appMgmtRes = (GBDeviceEventAppManagementResult) deviceEvent;