所以我环顾四周我想出了这个代码片段的解决方案:
private void registerBondingBroadcastReceivers() {
final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
context.registerReceiver(pairingRequestBroadcastReceiver, pairingRequestFilter);
}
private final BroadcastReceiver pairingRequestBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final int variant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if (variant == PAIRING_VARIANT_PIN) {
String pin = "141231"
if (pin != null) {
boolean pinSet = device.setPin(pin.getBytes());
abortBroadcast();
}
}
}
};
因此,这对于三星 S9、Android 10 非常有效,但是,如果我使用华为手机,绑定总是在密码方法上失败。有没有办法解决这个问题?