This article explains how to develop a Windows Phone application with support to Bluetooth Low Energy device and use an Arduino board to simulate a Bluetooth device.

Introduction

With the new support to Bluetooth Low Energy (BLE) API in Windows Phone 8.1, the developer is able to developer applications that communicate with a lot of new devices. You can take a look at the BLE technology details and how it works in this articleIntroduction_to_Bluetooth_support_on_Windows_Phone_8.

This article will show how to develop applications that support BLE devices and how to simulate a BLE device using an Arduino board and an Arduino BLE shield.

Warning.pngWarning: The Lumia black firmware version doesn't find any BLE device in the Settings -> Bluetooth view. You must be using the Lumia cyan firmware version to run the code in this article. There is a problem if you have installed the Preview for Developers in order to get the Windows Phone 8.1 earlier, these devices are not updating to Lumia cyan firmware for now.

Preparing the hardware

I have bought this Arduino BLE shield and an Arduino Uno like shown in this article Windows_Phone_8_communicating_with_Arduino_using_Bluetooth. This shield is receiving frequent updates and in the time of writing this article, it is able to simulate a lot of BLE profiles as: HID (Human Interface Device), Heart Rate Monitor, Thermometer, Proximity, and more. This ability to simulate known BLE devices is very useful to developers that want to do applications with support to BLE devices.

First of all, you have to put the BLE shield on top of the Arduino UNO. After that you have to install some additional libraries to work with the BLE shield in Arduino IDE. You have two options to install this additional libraries: You can get it from this guide in the official site of the BLE shield; or you can download this file that I have prepared and put its content in the<ArduinoSDKFolder>/libraries folder. I did this package because the BLE shield is frequently updated and the version will probably be different when you read this article, but if you don't feel comfortable to download my package file, just follow this guide and everything should work fine with a few changes.

The BLE shield has a lot of examples ready to be used. So, we will just select one of these examples and start it. It will be enough to connect and receive notifications from the Windows Phone application. In this article we will simulate a Heart Rate device in our BLE shield. To do that, select the example shown in the below picture.

Selected sample HeartRateBLE.png

After select the example, just compile and upload the code to the Arduino board. It will automatically start the BLE service and you will be able to connect to it from the Windows Phone.

Windows Phone code

In this section, we are going to create a Windows Phone project that is able to connect to the BLE shield and retrieve heart rate measurements as BLE notifications. The entire code is available here, but I recommend you to read the comments below in order to understand the code.

First, let's create a button to connect to the device and configure to receive the heart rate measures as BLE notifications.

private async void Connect_Click(object sender, RoutedEventArgs e)
{
//This code try to find a heart rate device that is doing advertise.
//In our case, this device is a BLE shield
var heartRateServices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.HeartRate), null);
 
//If it finds any device, it gets the device name
if (heartRateServices.Count > 0)
{
GattDeviceService firstHeartRateService = await GattDeviceService.FromIdAsync(heartRateServices[0].Id);
HeartRateServiceName_TextBox.Text = heartRateServices[0].Name;
 
//It also gets the heart rate characteristic and configure a method (heartRateCharacteristic_ValueChanged) to be called when a characteristic value change.
GattCharacteristic heartRateCharacteristic = firstHeartRateService.GetCharacteristics(GattCharacteristicUuids.HeartRateMeasurement)[0];
heartRateCharacteristic.ValueChanged += heartRateCharacteristic_ValueChanged;
 
//The application must configure the characteristic to start to notify the application when the characteristic value change
//If you comment this code your application will compile but not work.
await heartRateCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
}
else
{
HeartRateServiceName_TextBox.Text = "Error connecting...";
}
}

The code below shows the method that is called when a notification is received from the BLE device. It also shows the needed conversion of the received heart rate data.

void heartRateCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
byte[] heartRateData = new byte[args.CharacteristicValue.Length];
Windows.Storage.Streams.DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(heartRateData);
 
Dispatcher.BeginInvoke(() =>
{
//The received value must be converted before.
var hr = convertHearRateData(heartRateData);
HeartRate_TextBox.Text = hr.ToString();
});
}
 
double convertHearRateData(byte[] heartRateData)
{
//The heartRateData received is a byte array with size 2. But the first position of the array is a not used flag
//The heart rate data is in the second position of the heartRateData
return ((double)heartRateData[1]);
}

Running the example

Before run the example, you have to connect to the BLE device from the Settings -> Bluetooth view. To do that, just go there while the BLE shield is on and tap the Nordic HRM device. It must show the string connected below the device name.

After that, run the example application and click on connect. It must show the Nordic HRM string in the first field and the retrieved heart rate in the second field.

Watch the video below to see how to run the example of this article.

Credits:
Originally was contributed on 4 September 2014‎ by mfabiop