BluetoothDevelopmentXamarin

Bluetooth GATT Xamarin.Forms client.

If you read my previous blog post how to create Bluetooth GATT server on top of UWP, then you should be familiar with basics of Bluetooth. In this post I will extend on other side of the “wire” of the Bluetooth communication. I will do this with Xamarin.Forms.

In this post I will create simple (just plain vanilla C# code, no MVVM, plain “code-behind” C# code) mobile app which will communicate with GATT server over Bluetooth LE. Plain code-behind approach is used because I just wanted to focus on the code not the architecture, nor good practices and patterns.

Xamarin.Forms GATT client.

First, I created empty Xamarin.Forms project. Next, I put some NuGet dependencies on all three project: shared library, Android and iOS project. I put these dependencies:

Project and dependencies.

Application flow, pages and corresponding code.

The application flow is simple, straightforward: user opens the app, he or she is presented with scan button, which scans for nearby Bluetooth devices. When clicked on found device, it will be connected to and navigated to next page where user can exchange data with the device.

This Page’s XAML is very simple::

Code-behind C# code:

The page for exchanging data with GATT device is presented in the next section.

XAML:

C# code:

GATT server/device:

Next picture represent GATT server response when data is exchanged via Bluetooth.

Conclusion.

In this blog post you can find all details how to create GATT client with Xamarin.Forms. The example is presented in minimalistic way. In production, this code should be wrapped inside some services and (D)injected into ViewModels and data bonded to Views.

And yes, you can download code for this experiment here: https://github.com/josipx/Jenx.Bluetooth.GattServer

Leave a Reply

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