BluetoothC#DevelopmentXamarin

Xamarin Forms: Automatic pairing with Bluetooth device on Android

Problem: On Android when pairing with Bluetooth device is in progress this popup is shown to enter pin code:

This is fine, except there is an awkward message “Usually 000 or 1234“. Some end users just type 0000 or 1234 as suggested on the pairing form. Of course, pairing failed due to wrong pin entered.

Solution: I wanted to create custom entry form to collect pin and do automatic pairing, if possible. After a quick research I found some useful resources here.

First, I declared simple method TryAutomaticPairing(string pin) inside Xamarin Forms Android project. Crucial code excerpt from my BluetoothConnectionManager class:

where BluetoothDiscoveryReceiver class inherits from BroadcastReceiver:

Voila!

After scanning neighborhood for BLE devices, selecting one of them, request pin from user, application automatically pairs with my BLE device.

On some Android devices SetPin() fails i.e. false if returned. In that case, normal pair popup is displayed because I did not stop pairing process by executing InvokeAbortBroadcast().

If you have similar problems/solutions give me a ping and share your ideas.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.