Pebble: auto connect on incoming notification or phone call if connection was lost unxpectedly before

here
Andreas Shimokawa 2016-04-03 18:30:20 +02:00
parent a4919789ca
commit 7ddfd35c35
3 changed files with 53 additions and 6 deletions

View File

@ -200,11 +200,16 @@ public class PebbleIoThread extends GBDeviceIoThread {
}
mPebbleProtocol.setForceProtocol(sharedPrefs.getBoolean("pebble_force_protocol", false));
gbDevice.setState(GBDevice.State.CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
mIsConnected = true;
write(mPebbleProtocol.encodeFirmwareVersionReq());
if (originalState == GBDevice.State.WAITING_FOR_RECONNECT) {
gbDevice.setState(GBDevice.State.INITIALIZED);
} else {
gbDevice.setState(GBDevice.State.CONNECTED);
write(mPebbleProtocol.encodeFirmwareVersionReq());
}
gbDevice.sendDeviceUpdateIntent(getContext());
return true;
}

View File

@ -10,7 +10,10 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
import nodomain.freeyourgadget.gadgetbridge.service.serial.AbstractSerialDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
@ -35,7 +38,7 @@ public class PebbleSupport extends AbstractSerialDeviceSupport {
@Override
public boolean useAutoConnect() {
return false;
return true;
}
@Override
@ -71,6 +74,45 @@ public class PebbleSupport extends AbstractSerialDeviceSupport {
return (PebbleIoThread) super.getDeviceIOThread();
}
private boolean reconnect() {
if (!isConnected() && useAutoConnect()) {
if (getDevice().getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
gbDeviceIOThread.interrupt();
gbDeviceIOThread = null;
if (!connect()) {
return false;
}
try {
Thread.sleep(2000); // this is about the time the connect takes, so the notification can come though
} catch (InterruptedException ignored) {
}
}
}
return true;
}
@Override
public void onNotification(NotificationSpec notificationSpec) {
if (reconnect()) {
super.onNotification(notificationSpec);
}
}
@Override
public void onSetCallState(String number, String name, ServiceCommand command) {
if (reconnect()) {
super.onSetCallState(number, name, command);
}
}
@Override
public void onSetMusicInfo(String artist, String album, String track, int duration, int trackCount, int trackNr) {
if (reconnect()) {
super.onSetMusicInfo(artist, album, track, duration, trackCount, trackNr);
}
}
@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
//nothing to do ATM

View File

@ -29,8 +29,8 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceSupport.class);
private GBDeviceProtocol gbDeviceProtocol;
private GBDeviceIoThread gbDeviceIOThread;
protected GBDeviceProtocol gbDeviceProtocol;
protected GBDeviceIoThread gbDeviceIOThread;
/**
* Factory method to create the device specific GBDeviceProtocol instance to be used.