我想做的是打开对话框以输入配对过程的 PIN。
连接到设备后,我收到通知,但没有显示配对对话框。我必须手动打开它。
到目前为止,我尝试了以下在收到 PAIRING_REQUEST 操作时在广播接收器中调用的方法:
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);
}
它正确显示了对话框,但在我输入它之后,它没有与我的设备配对。
我也试过这段代码:
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);
}
这导致我的应用崩溃,因为它说我没有发送 PAIRING_REQUEST 广播的权限(即使我同时设置了 BLUETOOTH 和 BLUETOOTH_ADMIN 权限)
拜托,我真的需要显示这个对话框,如果它是默认对话框就更好了。我正在连接到一个 BLE 设备,连接后它需要一个 PIN 码来配对并能够修改一些特性。
非常感谢您的帮助!
提前致谢!
最佳答案
尝试使用新的 Android BluetoothDevice API:bluetoothdevice.createBond()。调用该方法后,系统会自动为您调用配对请求对话框。然后您可以在弹出的对话框中输入 PIN。
考虑在您要开始配对过程的代码中添加类似这样的内容:
private void pairDevice(BluetoothDevice device) {
try {
Log.d(TAG, "Start Pairing... with: " + device.getName());
device.createBond();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
关于android - 将蓝牙配对请求通知对话框放在前面以询问 PIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201050/