Android device as peripheral, Skips pair dialog

140 Views Asked by At

I'm developing an Android app that utilizes Bluetooth Low Energy (BLE) functionality. In my app, my device acts as a periphera. However, I've noticed that the pairing prompt dialog, which the Android system normally displays, is being skipped during the bonding process.

I understand that starting from Android 12, the system behavior for Bluetooth pairing has changed, and the pairing prompt may not always appear. However, I would like to ensure that the pairing prompt dialog always shows up on Android 13 to provide a consistent user experience.

Is there a way to solve this issue and ensure that the pairing prompt dialog is consistently displayed when bonding as a peripheral device on Android 13? I would greatly appreciate any insights, workarounds, or best practices that can help me achieve this goal.

Thank you in advance for your assistance!

Code:

private val broadCastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            if (BluetoothDevice.ACTION_PAIRING_REQUEST == action) {
                val bluetoothDevice = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
                if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                 
                    ActivityCompat.requestPermissions(
                        context as Activity,
                        arrayOf(Manifest.permission.BLUETOOTH_CONNECT),
                        BLUETOOTH_CONNECT_REQUEST_CODE
                    )
                    return
                }
                bluetoothDevice?.setPin(BLE_PIN.encodeToByteArray())
                bluetoothDevice?.createBond()
                
                if (bluetoothDevice != null) {
                    ConnectionManager.connect(bluetoothDevice, context.applicationContext)
                }
            }
        }
    }

I've attempted to use the setPairingConfirmation() method, which has been a valid approach in previous Android versions. However, since I'm targeting Android 13, this method is no longer suitable.

0

There are 0 best solutions below