0

so I looked around solutions I came up with this code piece:

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();
            }
        }
    }
};

So this works perfectly fine for Samsung S9, Android 10, however, if I use Huawei phone, bonding always fails at pin code method. Is there some way to solve this?

MaaAn13
  • 264
  • 5
  • 24
  • 54
  • Do you get an error? Where is the stack trace? – Hack5 Jul 19 '20 at 10:05
  • @Hack5 There's no error stack trace. device.setPin just returns false for Huawei and causes popup dialog to be shown for user (which I would love to avoid). – MaaAn13 Jul 19 '20 at 10:25
  • I guess theres nothing related in the logcat? Especially from the Bluetooth service? – Hack5 Jul 19 '20 at 10:41
  • @Hack5 Unfortunetely - no. – MaaAn13 Jul 19 '20 at 14:14
  • Well, your best option is probably disabling Huawei phones from running your app. Huawei's BLE stack is really *really* broken: custom ROMs can't use BLE; custom ROMs can't connect reliably to some devices (until I wrote a patch); and now this. My guess is that they rewrote most of the bluetooth stack, because Huawei coding is utterly broken. Sorry I couldn't help better. – Hack5 Jul 19 '20 at 18:03

1 Answers1

0

You can either contact Huawei and ask them to fix it, or blacklist Huawei devices from your app. AFAIK it's a platform bug and only Huawei can fix it (thanks for locking our bootloaders...)

Hack5
  • 3,244
  • 17
  • 37