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!

3 thoughts on “Xamarin Forms: Automatic pairing with Bluetooth device on Android

  1. @Joze Thanks Joze it’s very helpful for me.

    same implementation needed for iOS or UWP platforms or it’s handled by the Plugin.BLE itself?

    1. This example here is only for Android. On iOS there is no such solution (at least I dont know of). On iOS, in general, when you app needs BLE device with pair functionality, iOS will automatically popup pairing window so you can enter pin and pair with device…

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.