Replace method pair() with connectFirstTime()

Should help with #642 for hplus which did not implement pair()
master
cpfeiffer 2017-04-12 21:33:19 +02:00
parent 589945f234
commit 16af0724dd
12 changed files with 47 additions and 50 deletions

View File

@ -105,10 +105,10 @@ public class GBDeviceService implements DeviceService {
}
@Override
public void connect(@Nullable GBDevice device, boolean performPair) {
public void connect(@Nullable GBDevice device, boolean firstTime) {
Intent intent = createIntent().setAction(ACTION_CONNECT)
.putExtra(GBDevice.EXTRA_DEVICE, device)
.putExtra(EXTRA_PERFORM_PAIR, performPair);
.putExtra(EXTRA_CONNECT_FIRST_TIME, firstTime);
invokeService(intent);
}

View File

@ -97,7 +97,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_URI = "uri";
String EXTRA_CONFIG = "config";
String EXTRA_ALARMS = "alarms";
String EXTRA_PERFORM_PAIR = "perform_pair";
String EXTRA_CONNECT_FIRST_TIME = "connect_first_time";
String EXTRA_BOOLEAN_ENABLE = "enable_realtime_steps";
String EXTRA_WEATHER_TIMESTAMP = "weather_timestamp";

View File

@ -88,6 +88,14 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
this.context = context;
}
/**
* Default implementation just calls #connect()
*/
@Override
public boolean connectFirstTime() {
return connect();
}
@Override
public boolean isConnected() {
return gbDevice.isConnected();

View File

@ -134,7 +134,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOT
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SUBJECT;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TITLE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_PERFORM_PAIR;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CONNECT_FIRST_TIME;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_VIBRATION_INTENSITY;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITION;
@ -239,7 +239,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
}
String action = intent.getAction();
boolean pair = intent.getBooleanExtra(EXTRA_PERFORM_PAIR, false);
boolean firstTime = intent.getBooleanExtra(EXTRA_CONNECT_FIRST_TIME, false);
if (action == null) {
LOG.info("no action");
@ -299,8 +299,8 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
if (deviceSupport != null) {
setDeviceSupport(deviceSupport);
if (pair) {
deviceSupport.pair();
if (firstTime) {
deviceSupport.connectFirstTime();
} else {
deviceSupport.setAutoReconnect(autoReconnect);
deviceSupport.connect();

View File

@ -52,6 +52,27 @@ public interface DeviceSupport extends EventHandler {
*/
boolean isConnected();
/**
* Attempts an initial connection to the device, typically after the user "discovered"
* and connects to it for the first time. Some implementations may perform an additional
* initialization or application-level pairing compared to the regular {@link #connect()}.
* <p/>
* Implementations may perform the connection in a synchronous or asynchronous way.
* Returns true if a connection attempt was made. If the implementation is synchronous
* it may also return true if the connection was successfully established, however
* callers shall not rely on that.
* <p/>
* The actual connection state change (successful or not) will be reported via the
* #getDevice device as a device change Intent.
*
* Note: the default implementation {@link AbstractDeviceSupport#connectFirstTime()} just
* calls {@link #connect()}
*
* @see #connect()
* @see GBDevice#ACTION_DEVICE_CHANGED
*/
boolean connectFirstTime();
/**
* Attempts to establish a connection to the device. Implementations may perform
* the connection in a synchronous or asynchronous way.
@ -62,6 +83,7 @@ public interface DeviceSupport extends EventHandler {
* The actual connection state change (successful or not) will be reported via the
* #getDevice device as a device change Intent.
*
* @see #connectFirstTime()
* @see GBDevice#ACTION_DEVICE_CHANGED
*/
boolean connect();
@ -92,14 +114,6 @@ public interface DeviceSupport extends EventHandler {
*/
boolean getAutoReconnect();
/**
* Attempts to pair and connect this device with the gadget device. Success
* will be reported via a device change Intent.
*
* @see GBDevice#ACTION_DEVICE_CHANGED
*/
void pair();
/**
* Returns the associated device this instance communicates with.
*/

View File

@ -72,6 +72,11 @@ public class ServiceDeviceSupport implements DeviceSupport {
return delegate.isConnected();
}
@Override
public boolean connectFirstTime() {
return delegate.connectFirstTime();
}
@Override
public boolean connect() {
return delegate.connect();
@ -112,11 +117,6 @@ public class ServiceDeviceSupport implements DeviceSupport {
return delegate.useAutoConnect();
}
@Override
public void pair() {
delegate.pair();
}
private boolean checkBusy(String notificationKind) {
if (!flags.contains(Flags.BUSY_CHECKING)) {
return false;

View File

@ -417,12 +417,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
return true;
}
@Override
public void pair() {
LOG.debug("Pair");
}
private void handleDeviceInfo(DeviceInfo info) {
LOG.warn("Device info: " + info);
}

View File

@ -217,12 +217,13 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void pair() {
public boolean connectFirstTime() {
for (int i = 0; i < 5; i++) {
if (connect()) {
return;
return true;
}
}
return false;
}
public DeviceInfo getDeviceInfo() {

View File

@ -267,13 +267,9 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
@Override
public void pair() {
public boolean connectFirstTime() {
needsAuth = true;
for (int i = 0; i < 5; i++) {
if (connect()) {
return;
}
}
return super.connect();
}
private MiBand2Support sendDefaultNotification(TransactionBuilder builder, SimpleNotification simpleNotification, short repeat, BtLEAction extraAction) {

View File

@ -124,11 +124,6 @@ public class VibratissimoSupport extends AbstractBTLEDeviceSupport {
return true;
}
@Override
public void pair() {
}
private void handleDeviceInfo(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo info) {
LOG.warn("Device info: " + info);
versionCmd.hwVersion = info.getHardwareRevision();

View File

@ -68,12 +68,6 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
}
}
@Override
public void pair() {
// Default implementation does no manual pairing, use the Android
// pairing dialog instead.
}
/**
* Lazily creates and returns the GBDeviceProtocol instance to be used.
*/

View File

@ -44,11 +44,6 @@ class TestDeviceSupport extends AbstractDeviceSupport {
return false;
}
@Override
public void pair() {
}
@Override
public void onNotification(NotificationSpec notificationSpec) {