BluetoothDevelopmentUWP

Bluetooth GATT server.

Developing applications which use Bluetooth can be tricky. Commonly, on one side of the communication you have some hardware or maybe some IoT device, while on other, there is some mobile app or similar. When doing geenfield projects, normally you don’t have everything in place on the device side, but you need to develop Bluetooth application stack in parallel with device development/production.

In this blog post I will present one way how to handle this situation. I am going to present Bluetooth communication between two Bluetooth endpoints.

I will show “server-pheripheral side” of Bluetooth communication, side which is waiting for connection. In the next blog post I will present also client side. This will be presented by Xamarin.Forms mobile app (Android and iOS).

Before digging into code, I will present some basic stuff.

Bluetooth.

Bluetooth is a wireless technology standard for exchanging data between fixed and mobile devices over short distances. It’s widely used technology, especially for connecting telephones, speakers, tablets, media players, robotics systems, laptops, and console gaming equipment as well as some high definition headsets, modems and even watches. Bluetooth is really broad topic, but If someone wants to go deeper here is the start point : www.bluetooth.com.

Universal Windows Platform – UWP

I will use Universal Windows Platform (UWP) for server side app development. UWP has solid support for Bluetooth. There are two different Bluetooth technologies that you can choose to implement in your app.

In my case I will use Bluetooth Low Energy (LE). This specification defines protocols for discovery and communication between power-efficient devices. Discovery of devices is done through the Generic Access Profile (GAP) protocol. After discovery, device-to-device communication is done through the Generic Attribute (GATT) protocol. For more about Bluetooth LE, you can check Bluetooth Core Specification version 4.0, where Bluetooth LE was introduced.

Application development stack for developing Bluetooth application are under:

What is GATT?

Generic Attribute Profile (GATT) is built on top of the Attribute Protocol (ATT) and establishes common operations and a framework for the data transported and stored by the Attribute Protocol.

GATT is an acronym for the Generic Attribute Profile, and it defines the way that two Bluetooth Low Energy devices exchange data. Basic concepts here are called Services and Characteristics. Attribute Protocol (ATT) store Services, Characteristics and related data in a simple lookup table using 16-bit IDs (UUIDs or to Windows developers more familiar (and very similar) entities called GUIDs).

GATT comes into play when devices are connected, meaning that you have already gone through the advertising process called GAP.

Very important thing here is that GATT connection is exclusive. On short, a BLE peripheral can only be connected to one central device (a mobile phone, etc.) at a time! As soon as a peripheral is connected to a central device, it will stop advertising and other devices will no longer see it or connect to this peripheral device.

Structure of GATT protocol: Profile, Service, Characteristics, Characteristic descriptors.

GATT is (again) very broad topic, but you can find a lot of material about this topic on the internet or at official Bluetooth site.

Creating GATT server on top of UWP platform.

Universal Windows Platform (UWP) has good support for Bluetooth, so I decided to try with this framework.

Show me the code!

I will host my GATT server inside (UWP) Console application and UWP GUI app.

For the purpose of this example I introduced my GATT server interface. It’s simple, minimalistic definition. I will basically just exchange simple data over Bluetooth connection, so for that purpose this interface definition will provide all I need:

My GATT server will be able to initialize read, write and read-write characteristics inside GATT service. Not a rocket science, but enough to get it going. Simple cases will be covered, which can be quickly extended on more complex scenarios.

Just to summarize what is going on in code below:

  • “Root” service is defined via GattServiceProvider.CreateAsync(_serviceId).
  • ServiceProvider is then extracted from service.
  • Characteristics can be added on top of root Service.
  • On Start() service is advertised (available to external devices).

To host my GATT server I created two examples: console and UWP GUI version. Let’s check the first one:

UWP Console host.

In general, UWP programs are desktop GUI applications, but with small tricks, console application can be developed also. You can find all the information how to do it here: https://blog.pieeatingninjas.be/2018/04/05/creating-a-uwp-console-app-in-c/.

To summarize, In my console host I created new instance of my Gatt server, make some initialization and start broadcasting my service on my BLE Windows 10 Bluetooth infrastructure. The code is here:

When I compiled everything and run the application, my output was:

The most important (but easy to forget) thing is that you must enable Bluetooth Permission in UWP Package manifest. If this is not enabled, write characteristics will not work as expected.

UWP GUI host.

I like “big button” projects for my proof-of-concept experiments: In the next section, I will quickly present UWP GUI (big button) application which hosts my GATT server. Nothing fancy, just two buttons and one text output control, e.g.:

You can spot similarity with console version. The core application flow is identical.

Testing my GATT server.

In order to test my Bluetooth GATT server I used one of many mobile BLE client applications, in my case nRF Connect.

I scanned my network, I got my device list and connect to my “device”:

And then I play around with my application by changing my GATT characteristics.

And my GATT server just logger my data transferred via BLE.

Conclusion.

To conclude: in this blog post you can find all details howto create GATT server in order to test or experiment with Bluetooth LE connection. Example presented here is just basic scenario, but this example can easily be extended in more realistic (complex) cases.

In the next blog post I will talk about how to connect and to exchange data with BLE with Xamarin.Forms Android and iOS mobile apps.

Hope you enjoyed & stay tuned.

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 *