1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Discovery: handle the case where a device is already bonded

This commit is contained in:
cpfeiffer 2017-03-04 16:03:36 +01:00
parent 09967b2006
commit 58e2538c4e

View File

@ -109,8 +109,7 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC
if (device != null && device.getAddress().equals(bondingAddress)) { if (device != null && device.getAddress().equals(bondingAddress)) {
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if (bondState == BluetoothDevice.BOND_BONDED) { if (bondState == BluetoothDevice.BOND_BONDED) {
GB.toast(DiscoveryActivity.this, "Successfully bonded with: " + bondingAddress, Toast.LENGTH_SHORT, GB.INFO); handleDeviceBonded();
finish();
} }
} }
} }
@ -118,6 +117,11 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC
} }
}; };
private void handleDeviceBonded() {
GB.toast(DiscoveryActivity.this, "Successfully bonded with: " + bondingAddress, Toast.LENGTH_SHORT, GB.INFO);
finish();
}
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override @Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
@ -516,9 +520,21 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC
} else { } else {
try { try {
BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress()); BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress());
if (btDevice.createBond()) { switch (btDevice.getBondState()) {
// async, wait for bonding event to finish this activity case BluetoothDevice.BOND_NONE: {
bondingAddress = btDevice.getAddress(); if (btDevice.createBond()) {
// async, wait for bonding event to finish this activity
bondingAddress = btDevice.getAddress();
}
break;
}
case BluetoothDevice.BOND_BONDING:
// async, wait for bonding event to finish this activity
bondingAddress = btDevice.getAddress();
break;
case BluetoothDevice.BOND_BONDED:
handleDeviceBonded();
break;
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error pairing device: " + deviceCandidate.getMacAddress()); LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());