1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-30 14:02:56 +01:00

Mi Band 1+2: backend implementation of making BT pairing optional

- cleaned up the DeviceService.connect() variants
- discovery: pass the device candidate around instead of the mac address

Attempts to fix #512, #514, #518
This commit is contained in:
cpfeiffer 2017-01-26 00:11:52 +01:00
parent b8b2d8830f
commit ac68bfe351
9 changed files with 89 additions and 59 deletions

View File

@ -509,7 +509,7 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC
Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
if (pairingActivity != null) {
Intent intent = new Intent(this, pairingActivity);
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS, deviceCandidate.getMacAddress());
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE, deviceCandidate);
startActivity(intent);
} else {
try {

View File

@ -27,7 +27,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
* the given device.
*/
public interface DeviceCoordinator {
String EXTRA_DEVICE_MAC_ADDRESS = "nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate.EXTRA_MAC_ADDRESS";
String EXTRA_DEVICE_CANDIDATE = "nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate.EXTRA_DEVICE_CANDIDATE";
/**
* Checks whether this coordinator handles the given candidate.

View File

@ -23,6 +23,9 @@ import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -34,7 +37,7 @@ public class MiBandPairingActivity extends GBActivity {
private static final long DELAY_AFTER_BONDING = 1000; // 1s
private TextView message;
private boolean isPairing;
private String macAddress;
private GBDeviceCandidate deviceCandidate;
private String bondingMacAddress;
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
@ -43,9 +46,9 @@ public class MiBandPairingActivity extends GBActivity {
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
LOG.debug("pairing activity: device changed: " + device);
if (macAddress.equals(device.getAddress())) {
if (deviceCandidate.getMacAddress().equals(device.getAddress())) {
if (device.isInitialized()) {
pairingFinished(true, macAddress);
pairingFinished(true, deviceCandidate);
} else if (device.isConnecting() || device.isInitializing()) {
LOG.info("still connecting/initializing device...");
}
@ -74,7 +77,7 @@ public class MiBandPairingActivity extends GBActivity {
attemptToConnect();
} else {
LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
pairingFinished(false, bondingMacAddress);
pairingFinished(false, deviceCandidate);
}
}
}
@ -98,11 +101,11 @@ public class MiBandPairingActivity extends GBActivity {
message = (TextView) findViewById(R.id.miband_pair_message);
Intent intent = getIntent();
macAddress = intent.getStringExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS);
if (macAddress == null && savedInstanceState != null) {
macAddress = savedInstanceState.getString(STATE_MIBAND_ADDRESS, null);
deviceCandidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
if (deviceCandidate == null && savedInstanceState != null) {
deviceCandidate = savedInstanceState.getParcelable(STATE_MIBAND_ADDRESS);
}
if (macAddress == null) {
if (deviceCandidate == null) {
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, DiscoveryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
@ -122,13 +125,13 @@ public class MiBandPairingActivity extends GBActivity {
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_MIBAND_ADDRESS, macAddress);
outState.putParcelable(STATE_MIBAND_ADDRESS, deviceCandidate);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
macAddress = savedInstanceState.getString(STATE_MIBAND_ADDRESS, macAddress);
deviceCandidate = savedInstanceState.getParcelable(STATE_MIBAND_ADDRESS);
}
@Override
@ -145,13 +148,9 @@ public class MiBandPairingActivity extends GBActivity {
@Override
protected void onDestroy() {
try {
// just to be sure, remove the receivers -- might actually be already unregistered
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
unregisterReceiver(mBondingReceiver);
} catch (IllegalArgumentException ex) {
// already unregistered, ignore
}
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
if (isPairing) {
stopPairing();
}
@ -159,14 +158,11 @@ public class MiBandPairingActivity extends GBActivity {
}
private void startPairing() {
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
if (device == null) {
GB.toast(this, "No such Bluetooth Device: " + macAddress, Toast.LENGTH_LONG, GB.ERROR);
return;
}
isPairing = true;
message.setText(getString(R.string.pairing, macAddress));
message.setText(getString(R.string.pairing, deviceCandidate));
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
if (!shouldSetupBTLevelPairing()) {
// there are connection problems on certain Galaxy S devices at least;
@ -175,12 +171,10 @@ public class MiBandPairingActivity extends GBActivity {
return;
}
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBondingReceiver, filter);
performBluetoothPair(device);
performBluetoothPair(deviceCandidate);
}
private boolean shouldSetupBTLevelPairing() {
@ -188,7 +182,7 @@ public class MiBandPairingActivity extends GBActivity {
return prefs.getPreferences().getBoolean(MiBandConst.PREF_MIBAND_SETUP_BT_PAIRING, true);
}
private void pairingFinished(boolean pairedSuccessfully, String macAddress) {
private void pairingFinished(boolean pairedSuccessfully, GBDeviceCandidate candidate) {
LOG.debug("pairingFinished: " + pairedSuccessfully);
if (!isPairing) {
// already gone?
@ -196,13 +190,14 @@ public class MiBandPairingActivity extends GBActivity {
}
isPairing = false;
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
unregisterReceiver(mBondingReceiver);
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
if (pairedSuccessfully) {
// remember the device since we do not necessarily pair... temporary -- we probably need
// to query the db for available devices in ControlCenter. But only remember un-bonded
// devices, as bonded devices are displayed anyway.
String macAddress = deviceCandidate.getMacAddress();
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
Prefs prefs = GBApplication.getPrefs();
@ -219,7 +214,9 @@ public class MiBandPairingActivity extends GBActivity {
isPairing = false;
}
protected void performBluetoothPair(BluetoothDevice device) {
protected void performBluetoothPair(GBDeviceCandidate deviceCandidate) {
BluetoothDevice device = deviceCandidate.getDevice();
int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_BONDED) {
GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
@ -241,6 +238,11 @@ public class MiBandPairingActivity extends GBActivity {
private void performApplicationLevelPair() {
GBApplication.deviceService().disconnect(); // just to make sure...
GBApplication.deviceService().connect(macAddress, true);
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
if (device != null) {
GBApplication.deviceService().connect(device, true);
} else {
GB.toast(this, "Unable to connect, can't recognize the device type: " + deviceCandidate, Toast.LENGTH_LONG, GB.ERROR);
}
}
}

View File

@ -7,6 +7,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import android.widget.Toast;
@ -28,6 +29,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -90,7 +92,10 @@ public class PebblePairingActivity extends GBActivity {
message = (TextView) findViewById(R.id.pebble_pair_message);
Intent intent = getIntent();
macAddress = intent.getStringExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS);
GBDeviceCandidate candidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
if (candidate != null) {
macAddress = candidate.getMacAddress();
}
if (macAddress == null) {
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
returnToPairingActivity();

View File

@ -53,21 +53,14 @@ public class GBDeviceService implements DeviceService {
}
@Override
public void connect(GBDevice device) {
Intent intent = createIntent().setAction(ACTION_CONNECT)
.putExtra(GBDevice.EXTRA_DEVICE, device);
invokeService(intent);
public void connect(@Nullable GBDevice device) {
connect(device, false);
}
@Override
public void connect(@Nullable String deviceAddress) {
connect(deviceAddress, false);
}
@Override
public void connect(@Nullable String deviceAddress, boolean performPair) {
public void connect(@Nullable GBDevice device, boolean performPair) {
Intent intent = createIntent().setAction(ACTION_CONNECT)
.putExtra(EXTRA_DEVICE_ADDRESS, deviceAddress)
.putExtra(GBDevice.EXTRA_DEVICE, device)
.putExtra(EXTRA_PERFORM_PAIR, performPair);
invokeService(intent);
}

View File

@ -47,7 +47,6 @@ public interface DeviceService extends EventHandler {
String ACTION_SEND_CONFIGURATION = PREFIX + ".action.send_configuration";
String ACTION_SEND_WEATHER = PREFIX + ".action.send_weather";
String ACTION_TEST_NEW_FUNCTION = PREFIX + ".action.test_new_function";
String EXTRA_DEVICE_ADDRESS = "device_address";
String EXTRA_NOTIFICATION_BODY = "notification_body";
String EXTRA_NOTIFICATION_FLAGS = "notification_flags";
String EXTRA_NOTIFICATION_ID = "notification_id";
@ -112,17 +111,14 @@ public interface DeviceService extends EventHandler {
String EXTRA_CALENDAREVENT_DURATION = "calendarevent_duration";
String EXTRA_CALENDAREVENT_TITLE = "calendarevent_title";
String EXTRA_CALENDAREVENT_DESCRIPTION = "calendarevent_description";
String EXTRA_MIBAND2_AUTH_BYTE = "miband2_auth_byte";
void start();
void connect();
void connect(GBDevice device);
void connect(@Nullable GBDevice device);
void connect(@Nullable String deviceAddress);
void connect(@Nullable String deviceAddress, boolean performPair);
void connect(@Nullable GBDevice device, boolean performPair);
void disconnect();

View File

@ -102,7 +102,6 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CAL
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CONFIG;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_DEVICE_ADDRESS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_FIND_START;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ARTIST;
@ -278,13 +277,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
String btDeviceAddress = null;
if (gbDevice == null) {
btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
if (btDeviceAddress == null && prefs != null) { // may be null in test cases
if (prefs != null) { // may be null in test cases
btDeviceAddress = prefs.getString("last_device_address", null);
}
if (btDeviceAddress != null) {
gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
}
}
} else {
btDeviceAddress = gbDevice.getAddress();
}

View File

@ -1,7 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
public class AndroidUtils {
public static ParcelUuid[] toParcelUUids(Parcelable[] uuids) {
@ -12,4 +15,34 @@ public class AndroidUtils {
System.arraycopy(uuids, 0, uuids2, 0, uuids.length);
return uuids2;
}
/**
* Unregisters the given receiver from the given context.
* @param context the context from which to unregister
* @param receiver the receiver to unregister
* @return true if it was successfully unregistered, or false if the receiver was not registered
*/
public static boolean safeUnregisterBroadcastReceiver(Context context, BroadcastReceiver receiver) {
try {
context.unregisterReceiver(receiver);
return true;
} catch (IllegalArgumentException ex) {
return false;
}
}
/**
* Unregisters the given receiver from the given {@link LocalBroadcastManager}.
* @param manager the manager from which to unregister
* @param receiver the receiver to unregister
* @return true if it was successfully unregistered, or false if the receiver was not registered
*/
public static boolean safeUnregisterBroadcastReceiver(LocalBroadcastManager manager, BroadcastReceiver receiver) {
try {
manager.unregisterReceiver(receiver);
return true;
} catch (IllegalArgumentException ex) {
return false;
}
}
}

View File

@ -123,7 +123,10 @@ public class DeviceHelper {
public GBDevice toSupportedDevice(BluetoothDevice device) {
GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN, device.getUuids());
return toSupportedDevice(candidate);
}
public GBDevice toSupportedDevice(GBDeviceCandidate candidate) {
for (DeviceCoordinator coordinator : getAllCoordinators()) {
if (coordinator.supports(candidate)) {
return coordinator.createDevice(candidate);