Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth pairing fails when using two android devices #116

Open
aglogger opened this issue Jul 2, 2019 · 0 comments
Open

Bluetooth pairing fails when using two android devices #116

aglogger opened this issue Jul 2, 2019 · 0 comments

Comments

@aglogger
Copy link

aglogger commented Jul 2, 2019

When using btPairDevice(), the PairingBroadcastReceiver (automatic pairing confirmation) is only registered for the device from which the paring request is started. However, since the corresponding confirmation, for successful pairing must be confirmed on both devices, the PairingBroadcastReceiver must also be registered for the second device.

@Rpc(description = "Pair with a bluetooth device.")
public void btPairDevice(String deviceAddress) throws Throwable {
BluetoothDevice device = mDiscoveryResults.get(deviceAddress);
if (device == null) {
throw new NoSuchElementException(
"No device with address "
+ deviceAddress
+ " has been discovered. Cannot proceed.");
}
mContext.registerReceiver(
new PairingBroadcastReceiver(mContext), PairingBroadcastReceiver.filter);
if (!(boolean) Utils.invokeByReflection(device, "createBond")) {
throw new BluetoothAdapterSnippetException(
"Failed to initiate the pairing process to device: " + deviceAddress);
}
if (!Utils.waitUntil(() -> device.getBondState() == BluetoothDevice.BOND_BONDED, 120)) {
throw new BluetoothAdapterSnippetException(
"Failed to pair with device " + deviceAddress + " after 2min.");
}
}

My solution for this problem was implementing a additional Rpc-function which is only used to register the broadcast receiver for any device as required.

public void btStartPairingBroadcastReceiver () {
    mContext.registerReceiver(new PairingBroadcastReceiver(mContext), PairingBroadcastReceiver.filter);
}
@aglogger aglogger changed the title Bluetooth pairing failing when using two android devices Bluetooth pairing fails when using two android devices Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant