问题
What I'm trying to do is to brin the dialog to input the PIN for a pairing process.
After I connect to a device, I receive a notification but the pairing dialog does not show up. I have to open it manually.
So far I tried the following methods which are called in the broadcast receiver when I get the PAIRING_REQUEST action:
public void pairDevice(BluetoothDevice device)
{
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
Intent intent = new Intent(ACTION_PAIRING_REQUEST);
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
int PAIRING_VARIANT_PIN = 0;
intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Which shows the dialog properly but after I input it, it does not pair my device.
I also tried this code:
public void pairDevice(BluetoothDevice device)
{
Intent intent = new Intent("android.bluetooth.device.action.PAIRING_REQUEST");
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
int PAIRING_VARIANT_PIN = 272;
intent.putExtra("android.bluetooth.device.extra.PAIRING_VARIANT", PAIRING_VARIANT_PIN);
sendBroadcast(intent);
}
Which crashes my app because it says I don't have permissions to send broadcast for PAIRING_REQUEST (even if I set both permissions BLUETOOTH and BLUETOOTH_ADMIN)
Please, I really need to show this dialog and much better if it is the default one. I am connecting to a BLE device, and after connected it requires a PIN for pairing and be able to modify some characteristics.
Your help would be much appreciated!
Thanks in advance!
回答1:
Try to use new Android BluetoothDevice API: bluetoothdevice.createBond(). After you call this method, the system will invoke the pairing request dialog for you automatically. Then you can enter PIN in that pop up dialog.
Consider adding something like this in your code, where you want to start the pairing process:
private void pairDevice(BluetoothDevice device) {
try {
Log.d(TAG, "Start Pairing... with: " + device.getName());
device.createBond();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
回答2:
I had a similar issue, make sure your bluetooth user permissions are set within the manifest xml tag not under the application tag - in the AndroidManifest (that is). Hope this helps.
回答3:
You can use connect method directly to pair the device.If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure. So when attempting to connect devices, your application does not need to be concerned about whether or not the devices are paired.
回答4:
Try to use this for all API levels:
public static void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
//From API 19.
// device.createBond();
} catch (Exception e) {
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/25201050/bring-bluetooth-pairing-request-notification-dialog-on-front-to-ask-for-pin