AndroidappleBluetoothC#DevelopmentXamarin

Bluetooth Low Energy UART Service with Xamarin Forms

uart-ble-xamairn-forms-logo

A little bit of background.

Bluetooth is a wireless technology standard used for exchanging data between fixed and mobile devices over short distances using short-wavelength UHF radio waves in the industrial, scientific and medical radio bands, from 2.402 GHz to 2.480 GHz, and building personal area networks. It was originally conceived as a wireless alternative to RS-232 data cables (reference: https://en.wikipedia.org/wiki/Bluetooth).

In Bluetooth Classic, a Serial Port Profile (SPP) is one of the traditional Bluetooth profiles (https://www.bluetooth.com/specifications/profiles-overview/) and it is an adopted profile defined by the Bluetooth Special Interest Group (SIG) used to emulate a serial port connection over a Bluetooth wireless.

For Bluetooth Low Energy (BLE), an adopted profile for implementing Serial Port Profile (SPP) over BLE is not defined, thus emulation of a serial port must be implemented as a vendor-specific custom profile.

At this point, I will not go into details of BLE, but you can find a lot of documentation on the web and a quick-start information on my blog post https://www.jenx.si/2019/08/09/bluetooth-gatt-server/.

For now, I will only emphasis that when you start building apps on top of BLE, you will need to know what Generic Attribute Profile (GATT) is and how to deal with this protocol.

How to start?

When I start with any Bluetooth LE project, I always observe which GATT Profiles/Service/Characteristics are exposed by GATT Server. For that I use super-awesome Android tool nRF Connect for Mobile (or for iOS here) from Nordic Semiconductor.

Putting my case-study in the context.

When we talk about Bluetooth communication there is almost always some hardware included in the development loop.

In my case, on one of my projects I worked with OBD2 Bluetooth dongle where UART/Serial communication was enabled over BLE.

First of all, I paired with my BLE device and observe GATT entities. I had four GATT services available on my BLE dongle, i.e.:

gatt-uart-services

For me, the interesting GATT services were the two marked in upper image.

First two are Generic Access and Device Information GATT services, providing general information about the device. You can check about these two services here: https://www.bluetooth.com/specifications/gatt/services/.

At first, I thought that first one (out of interesting ones) was used for UART communication. I knew that my BLE dongle used MicroChip hardware – and I found good documentation how Microchip use UART over BLE: Accessing the Transparent UART Service using Android BLE.

After a bit of investigation, I found out that GATT service with UUID 0xFFF0 (e.g. GUID 0000FFF0-0000-1000-8000-00805f9b34fb) is the right GATT service for me, and it’s used for UART communication with my BLE dongle.

Now, I have all the information needed, let’s start coding!

Let’s do some code.

In this blog post, I will present how to implement BLE UART with Xamarin Forms. I will show Android and iOS version by sharing almost 100% of the source code.

I used my https://github.com/josipx/Jenx.Bluetooth.GattServer source repository as a starting point. This Github repository is accompanying source code for my blog post https://www.jenx.si/2019/08/09/bluetooth-gatt-server.

After checkout, I removed all server side (GATT Server) projects. Next, I replaced outdated Plugin.Permissions NuGet with permission module from Xamarin.Essentials 1.5.3.2. Furthermore, I updated projects to the latest (as of this writing) NuGets, namely: Xamarin Forms 4.8.0.1269, BLE.Plugin 2.1.1 and for basic application cosmetics Xamarin.Forms.Visual.Material 4.8.0.1269.

Finally, I slightly changed namespaces and polished some code to reflect and to address my current domain.

App flow is as simple as possible:

  1. User starts the application.
  2. User hits “Scan” button where BLE permissions are checked and scan for BLE devices is performed.
  3. List of found BLE devices is displayed.
  4. User selects BLE device to connect to.
  5. On successful connection, apps redirects on UART data-exchange page.

You can check complete source code here https://github.com/josipx/Jenx.Bluetooth.UartOverGatt, here I will only present important and interesting parts of my application.

My mobile app consists basically from two pages, all code is approx. 100 lines of C# code-behind per page, and some basic XAML for view-page. Therefore, I will simply dump my code here, and comment out interesting parts.

My GATT Identifiers/constants are defined as:

Initial – main page page is where I can check permissions and perform BLE scanning. Xamarin.Essentials permissions module is used for handling permissions. Adding permissions to Android and iOS can be tricky, but for more details you can check application Android Manifest or iOS counterpart info.plist.

MainPage

XAML
Code behind

BluetoothDataPage

XAML
Code behind

At this point, I must express my disclaimer :): in this blog post – minimal proof-of-concept example is presented – in real production-ready application all this functionality would be organized in more structural way (e.g. as Views, Model, View-Models, Services, etc…).

My mobile apps in action

Android
iOS

OK, look & feel of the app is not exactly the same between Android and iOS, but UART functionality is working perfectly on both platforms – and for this example this is all that matters.

Conclusion

In this blog post I wanted to show how easy is to use Xamarin Forms and make Android and iOS app to talk to Bluetooth LE device. Furthermore, I presented simple Bluetooth UART example over BLE.

Many thanks to Plugin.BLE plugin which provides awesome Xamarin Forms Bluetooth layer.

In one of the next blog posts, I will use this investigation and show how to talk to cars over OBD2 protocol.

Until then, happy coding.

2 thoughts on “Bluetooth Low Energy UART Service with Xamarin Forms

  1. Hi, while using sendCharacteristics.WriteAsync in BluetoothDataPage.cs , its directly going to the catch part and showing msg as “Error sending comand to UART” , Can you help me out with this.
    Thanks and Regards,
    Abhimanyu Panda
    Developer, TCS
    India

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.