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, #518here
parent
b8b2d8830f
commit
ac68bfe351
|
@ -509,7 +509,7 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC
|
||||||
Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
|
Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
|
||||||
if (pairingActivity != null) {
|
if (pairingActivity != null) {
|
||||||
Intent intent = new Intent(this, pairingActivity);
|
Intent intent = new Intent(this, pairingActivity);
|
||||||
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS, deviceCandidate.getMacAddress());
|
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE, deviceCandidate);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -27,7 +27,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||||
* the given device.
|
* the given device.
|
||||||
*/
|
*/
|
||||||
public interface DeviceCoordinator {
|
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.
|
* Checks whether this coordinator handles the given candidate.
|
||||||
|
|
|
@ -23,6 +23,9 @@ import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity;
|
import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
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.GB;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
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 static final long DELAY_AFTER_BONDING = 1000; // 1s
|
||||||
private TextView message;
|
private TextView message;
|
||||||
private boolean isPairing;
|
private boolean isPairing;
|
||||||
private String macAddress;
|
private GBDeviceCandidate deviceCandidate;
|
||||||
private String bondingMacAddress;
|
private String bondingMacAddress;
|
||||||
|
|
||||||
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
|
||||||
|
@ -43,9 +46,9 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
|
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
|
||||||
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||||
LOG.debug("pairing activity: device changed: " + device);
|
LOG.debug("pairing activity: device changed: " + device);
|
||||||
if (macAddress.equals(device.getAddress())) {
|
if (deviceCandidate.getMacAddress().equals(device.getAddress())) {
|
||||||
if (device.isInitialized()) {
|
if (device.isInitialized()) {
|
||||||
pairingFinished(true, macAddress);
|
pairingFinished(true, deviceCandidate);
|
||||||
} else if (device.isConnecting() || device.isInitializing()) {
|
} else if (device.isConnecting() || device.isInitializing()) {
|
||||||
LOG.info("still connecting/initializing device...");
|
LOG.info("still connecting/initializing device...");
|
||||||
}
|
}
|
||||||
|
@ -74,7 +77,7 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
attemptToConnect();
|
attemptToConnect();
|
||||||
} else {
|
} else {
|
||||||
LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
|
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);
|
message = (TextView) findViewById(R.id.miband_pair_message);
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
macAddress = intent.getStringExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS);
|
deviceCandidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
|
||||||
if (macAddress == null && savedInstanceState != null) {
|
if (deviceCandidate == null && savedInstanceState != null) {
|
||||||
macAddress = savedInstanceState.getString(STATE_MIBAND_ADDRESS, 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();
|
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));
|
startActivity(new Intent(this, DiscoveryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
|
||||||
finish();
|
finish();
|
||||||
|
@ -122,13 +125,13 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onSaveInstanceState(Bundle outState) {
|
protected void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
outState.putString(STATE_MIBAND_ADDRESS, macAddress);
|
outState.putParcelable(STATE_MIBAND_ADDRESS, deviceCandidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||||
super.onRestoreInstanceState(savedInstanceState);
|
super.onRestoreInstanceState(savedInstanceState);
|
||||||
macAddress = savedInstanceState.getString(STATE_MIBAND_ADDRESS, macAddress);
|
deviceCandidate = savedInstanceState.getParcelable(STATE_MIBAND_ADDRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -145,13 +148,9 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
try {
|
// just to be sure, remove the receivers -- might actually be already unregistered
|
||||||
// just to be sure, remove the receivers -- might actually be already unregistered
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
||||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
|
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
|
||||||
unregisterReceiver(mBondingReceiver);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// already unregistered, ignore
|
|
||||||
}
|
|
||||||
if (isPairing) {
|
if (isPairing) {
|
||||||
stopPairing();
|
stopPairing();
|
||||||
}
|
}
|
||||||
|
@ -159,14 +158,11 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startPairing() {
|
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;
|
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()) {
|
if (!shouldSetupBTLevelPairing()) {
|
||||||
// there are connection problems on certain Galaxy S devices at least;
|
// there are connection problems on certain Galaxy S devices at least;
|
||||||
|
@ -175,12 +171,10 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
|
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
|
|
||||||
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
||||||
registerReceiver(mBondingReceiver, filter);
|
registerReceiver(mBondingReceiver, filter);
|
||||||
|
|
||||||
performBluetoothPair(device);
|
performBluetoothPair(deviceCandidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldSetupBTLevelPairing() {
|
private boolean shouldSetupBTLevelPairing() {
|
||||||
|
@ -188,7 +182,7 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
return prefs.getPreferences().getBoolean(MiBandConst.PREF_MIBAND_SETUP_BT_PAIRING, true);
|
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);
|
LOG.debug("pairingFinished: " + pairedSuccessfully);
|
||||||
if (!isPairing) {
|
if (!isPairing) {
|
||||||
// already gone?
|
// already gone?
|
||||||
|
@ -196,13 +190,14 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
isPairing = false;
|
isPairing = false;
|
||||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
||||||
unregisterReceiver(mBondingReceiver);
|
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
|
||||||
|
|
||||||
if (pairedSuccessfully) {
|
if (pairedSuccessfully) {
|
||||||
// remember the device since we do not necessarily pair... temporary -- we probably need
|
// 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
|
// to query the db for available devices in ControlCenter. But only remember un-bonded
|
||||||
// devices, as bonded devices are displayed anyway.
|
// devices, as bonded devices are displayed anyway.
|
||||||
|
String macAddress = deviceCandidate.getMacAddress();
|
||||||
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
|
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
|
||||||
if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
|
if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
|
||||||
Prefs prefs = GBApplication.getPrefs();
|
Prefs prefs = GBApplication.getPrefs();
|
||||||
|
@ -219,7 +214,9 @@ public class MiBandPairingActivity extends GBActivity {
|
||||||
isPairing = false;
|
isPairing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void performBluetoothPair(BluetoothDevice device) {
|
protected void performBluetoothPair(GBDeviceCandidate deviceCandidate) {
|
||||||
|
BluetoothDevice device = deviceCandidate.getDevice();
|
||||||
|
|
||||||
int bondState = device.getBondState();
|
int bondState = device.getBondState();
|
||||||
if (bondState == BluetoothDevice.BOND_BONDED) {
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
||||||
GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
|
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() {
|
private void performApplicationLevelPair() {
|
||||||
GBApplication.deviceService().disconnect(); // just to make sure...
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
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.Device;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao;
|
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
@ -90,7 +92,10 @@ public class PebblePairingActivity extends GBActivity {
|
||||||
|
|
||||||
message = (TextView) findViewById(R.id.pebble_pair_message);
|
message = (TextView) findViewById(R.id.pebble_pair_message);
|
||||||
Intent intent = getIntent();
|
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) {
|
if (macAddress == null) {
|
||||||
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
|
||||||
returnToPairingActivity();
|
returnToPairingActivity();
|
||||||
|
|
|
@ -53,21 +53,14 @@ public class GBDeviceService implements DeviceService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(GBDevice device) {
|
public void connect(@Nullable GBDevice device) {
|
||||||
Intent intent = createIntent().setAction(ACTION_CONNECT)
|
connect(device, false);
|
||||||
.putExtra(GBDevice.EXTRA_DEVICE, device);
|
|
||||||
invokeService(intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(@Nullable String deviceAddress) {
|
public void connect(@Nullable GBDevice device, boolean performPair) {
|
||||||
connect(deviceAddress, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void connect(@Nullable String deviceAddress, boolean performPair) {
|
|
||||||
Intent intent = createIntent().setAction(ACTION_CONNECT)
|
Intent intent = createIntent().setAction(ACTION_CONNECT)
|
||||||
.putExtra(EXTRA_DEVICE_ADDRESS, deviceAddress)
|
.putExtra(GBDevice.EXTRA_DEVICE, device)
|
||||||
.putExtra(EXTRA_PERFORM_PAIR, performPair);
|
.putExtra(EXTRA_PERFORM_PAIR, performPair);
|
||||||
invokeService(intent);
|
invokeService(intent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,6 @@ public interface DeviceService extends EventHandler {
|
||||||
String ACTION_SEND_CONFIGURATION = PREFIX + ".action.send_configuration";
|
String ACTION_SEND_CONFIGURATION = PREFIX + ".action.send_configuration";
|
||||||
String ACTION_SEND_WEATHER = PREFIX + ".action.send_weather";
|
String ACTION_SEND_WEATHER = PREFIX + ".action.send_weather";
|
||||||
String ACTION_TEST_NEW_FUNCTION = PREFIX + ".action.test_new_function";
|
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_BODY = "notification_body";
|
||||||
String EXTRA_NOTIFICATION_FLAGS = "notification_flags";
|
String EXTRA_NOTIFICATION_FLAGS = "notification_flags";
|
||||||
String EXTRA_NOTIFICATION_ID = "notification_id";
|
String EXTRA_NOTIFICATION_ID = "notification_id";
|
||||||
|
@ -112,17 +111,14 @@ public interface DeviceService extends EventHandler {
|
||||||
String EXTRA_CALENDAREVENT_DURATION = "calendarevent_duration";
|
String EXTRA_CALENDAREVENT_DURATION = "calendarevent_duration";
|
||||||
String EXTRA_CALENDAREVENT_TITLE = "calendarevent_title";
|
String EXTRA_CALENDAREVENT_TITLE = "calendarevent_title";
|
||||||
String EXTRA_CALENDAREVENT_DESCRIPTION = "calendarevent_description";
|
String EXTRA_CALENDAREVENT_DESCRIPTION = "calendarevent_description";
|
||||||
String EXTRA_MIBAND2_AUTH_BYTE = "miband2_auth_byte";
|
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
void connect();
|
void connect();
|
||||||
|
|
||||||
void connect(GBDevice device);
|
void connect(@Nullable GBDevice device);
|
||||||
|
|
||||||
void connect(@Nullable String deviceAddress);
|
void connect(@Nullable GBDevice device, boolean performPair);
|
||||||
|
|
||||||
void connect(@Nullable String deviceAddress, boolean performPair);
|
|
||||||
|
|
||||||
void disconnect();
|
void disconnect();
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE;
|
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_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_FIND_START;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM;
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ARTIST;
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ARTIST;
|
||||||
|
@ -278,12 +277,11 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||||
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||||
String btDeviceAddress = null;
|
String btDeviceAddress = null;
|
||||||
if (gbDevice == null) {
|
if (gbDevice == null) {
|
||||||
btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
|
if (prefs != null) { // may be null in test cases
|
||||||
if (btDeviceAddress == null && prefs != null) { // may be null in test cases
|
|
||||||
btDeviceAddress = prefs.getString("last_device_address", null);
|
btDeviceAddress = prefs.getString("last_device_address", null);
|
||||||
}
|
if (btDeviceAddress != null) {
|
||||||
if (btDeviceAddress != null) {
|
gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
||||||
gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
btDeviceAddress = gbDevice.getAddress();
|
btDeviceAddress = gbDevice.getAddress();
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
import android.os.ParcelUuid;
|
import android.os.ParcelUuid;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
public class AndroidUtils {
|
public class AndroidUtils {
|
||||||
public static ParcelUuid[] toParcelUUids(Parcelable[] uuids) {
|
public static ParcelUuid[] toParcelUUids(Parcelable[] uuids) {
|
||||||
|
@ -12,4 +15,34 @@ public class AndroidUtils {
|
||||||
System.arraycopy(uuids, 0, uuids2, 0, uuids.length);
|
System.arraycopy(uuids, 0, uuids2, 0, uuids.length);
|
||||||
return uuids2;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,7 +123,10 @@ public class DeviceHelper {
|
||||||
|
|
||||||
public GBDevice toSupportedDevice(BluetoothDevice device) {
|
public GBDevice toSupportedDevice(BluetoothDevice device) {
|
||||||
GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN, device.getUuids());
|
GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN, device.getUuids());
|
||||||
|
return toSupportedDevice(candidate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GBDevice toSupportedDevice(GBDeviceCandidate candidate) {
|
||||||
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
||||||
if (coordinator.supports(candidate)) {
|
if (coordinator.supports(candidate)) {
|
||||||
return coordinator.createDevice(candidate);
|
return coordinator.createDevice(candidate);
|
||||||
|
|
Loading…
Reference in New Issue