diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java index e42f1bd85..5358c479d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java @@ -109,8 +109,7 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC if (device != null && device.getAddress().equals(bondingAddress)) { int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); if (bondState == BluetoothDevice.BOND_BONDED) { - GB.toast(DiscoveryActivity.this, "Successfully bonded with: " + bondingAddress, Toast.LENGTH_SHORT, GB.INFO); - finish(); + handleDeviceBonded(); } } } @@ -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() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { @@ -516,9 +520,21 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC } else { try { BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress()); - if (btDevice.createBond()) { - // async, wait for bonding event to finish this activity - bondingAddress = btDevice.getAddress(); + switch (btDevice.getBondState()) { + case BluetoothDevice.BOND_NONE: { + 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) { LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());