What are the basic steps for connecting to BLE peripheral devices using Xamarin Android native APIs?

I'm developing a cross platform app and I'm trying to understand the steps for using the Xamarin Android native APIs in order to connect, discover services, read characteristics, etc. of a Bluetooth LE peripheral device.

I have not found any decent Xamarin examples so far explaining what is/is not required. The BLE example I found provided by Xamarin has been deprecated for a while, and many of the related questions I have found on here are currently unanswered. So far, this is my understanding:

After successfully scanning for devices,
1. Create a BluetoothGattCallback class and use it to override OnConnectionStateChange, OnServicesDiscovered, On CharacteristicRead, etc.
2. 4. Override OnReceive and register your broadcast receiver for the central device using RegisterReceiver ?
3. Use the ConnectGatt method to connect to the BLE peripheral device
4. Use DiscoverServices( ) to see if any services are being advertised by the peripheral
5. Pair to the device using CreateBond( ) ?
6. Read characteristic data, etc. ?
7. Use Disconnect( ) / Close( ) from the BluetoothGatt class & UnregisterReceiver when finished ?

If anyone has come across any updated examples (with no deprecated code and not using third party plugins), I would really appreciate it if you shared below. Or, if you could fill in the blanks for me with the steps I have listed above, I'd appreciate the help. Thanks!

Best Answer

  • eubikeubik ✭✭
    Accepted Answer

    This is by far the best example I have found that does not use a third party BLE plugin. The code is in Java and it's over a year old, but it explains the setup clearly - http://nilhcem.com/android-things/bluetooth-low-energy

    Also just wanted to share in case it helps anyone else that my peripheral was self-disconnecting not due to my code as I had thought (so some kind of hardware/firmware issue). I tested my code with another BLE board, it worked as expected and did not disconnect.

Answers

Sign In or Register to comment.