Windows 8.1 Bluetooth Low Energy Development

Windows 8.1 Bluetooth Low Energy Development

1 Overview

Before the release of Bluetooth 4.0, the intuitive impression given to everyone was the Bluetooth headset, which was used to meet the needs of audio communication with medium bandwidth in short distances. However, after the release of Bluetooth 4.0, the use is very different, especially in the era when the Internet of Things and wearables are popular. Many gadgets use it, such as heart rate meters, bracelets, keychains, and so on. Finally, it It can be connected to the user's mobile phone, Pad, PC and other devices to achieve a variety of functions. Why is Bluetooth 4.0 so versatile? First of all, thanks to low power consumption, devices running Bluetooth Low Energy, a button battery can support it for half a year; secondly, low cost, such as TI's CC2540 Bluetooth SoC, which is priced at $1. Since iOS and Android support Bluetooth 4.0 BLE, at Microsoft’s BUILD 2014 conference in April this year, it was finally officially announced to support Bluetooth 4.0 BLE in Windows 8.1 and Windows Phone 8.1. It is worth noting that so far, Windows 8.1 only supports GATT Client mode does not support GATT Server mode. Let's take a look at how to develop Bluetooth 4.0 BLE applications on the Windows 8.1 platform.

2. Equipment

The first is a PC or tablet with a Windows 8.1 system with Bluetooth 4.0. Take Surface Pro 2 as an example. You can open "Device Manager" -> Bluetooth, check the list below, if there is "Microsoft Bluetooth LE Enumerator" , As shown in Figure 1 below, it means that it supports Bluetooth 4.0 LE. If it does not, it is impossible to search for Bluetooth 4.0 LE devices. At this time, you need to update the system and Bluetooth drivers.

figure 1

Note that before proceeding with this step, it is best to first go to "Settings" -> "Change computer settings" -> "Computers and devices" -> "Bluetooth", turn on Bluetooth. Because when I tested it, I found that when the Bluetooth is turned off, the "Microsoft Bluetooth LE Enumerator" item will not appear on the Surface Pro 2, as shown in Figure 2 below. It will only appear when the Bluetooth is turned on.

figure 2

Secondly, it is the Bluetooth 4.0 BLE device. The most popular one is TI’s CC2541 Sensor Tag, which is sold on Taobao and the price is less than 200. Sensor Tag contains 6 kinds of sensors: IR temperature Sensor, Humidity Sensor, Pressure Sensor, Accelerometer, Gyroscope, Magnetometer.

3. Preparation

If the Sensor Tag is used for the first time on a Windows device, we need to manually pair it. This is also the style that Bluetooth has always been used on the Windows platform, including the previous Windows Mobile, Windows CE, and the current Windows Phone. Of course, after the first pairing is successfully used, there is no need to pair again later. 1. you need to turn on the Bluetooth switch, wait for the TI BLE Sensor Tag to appear, and then click on it. The PIN code for the first pairing is 0000. As shown in Figure 3 below.

image 3

After that, the system will automatically configure the GATT service. After the configuration is completed, you can go to the "Device Manager" -> Bluetooth item and you will find that there are many more GATT Services. As shown in Figure 4 below.

Figure 4

For the service provided by Sensor Tag and the corresponding UUID, you can refer to TI's official document: TI Development KIT .

Typical UUIDs used include:

Thermometer "f000aa00-0451-4000-b000-000000000000" Accelerometer "f000aa10-0451-4000-b000-000000000000" Humidity "f000aa20-0451-4000-b000-000000000000" Magnetometer "f000aa30-0451-4000-b000-000000000000" Barometer " f000aa40-0451-4000-b000-000000000000" Gyroscope "f000aa50-0451-4000-b000-000000000000" Key Service "0000ffe0-0000-1000-8000-00805f9b34fb" Generic Access: 00001800-0000-1000-8000-00805f9b34fb Generic Attribute: 00001801-0000-1000-8000-00805f9b34fb Device Information: 0000180A-0000-1000-8000-00805f9b34fb

4. Create an application

Create a Windows Store application, and then use Notepad or edit the Package.appxmanifest file under View Code and add the following Capabilities:


<Capability Name="internetClient"/>

<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">

<m2:Device Id="any">

<m2:Function Type="serviceId:f000aa00-0451-4000-b000-000000000000"/>

<m2:Function Type="serviceId:F000AA10-0451-4000-B000-000000000000"/>

<m2:Function Type="serviceId:F000AA20-0451-4000-B000-000000000000"/>

<m2:Function Type="serviceId:F000AA30-0451-4000-B000-000000000000"/>

<m2:Function Type="serviceId:F000AA40-0451-4000-B000-000000000000"/>

<m2:Function Type="serviceId:F000AA50-0451-4000-B000-000000000000"/>

<m2:Function Type="serviceId:0000ffe0-0000-1000-8000-00805f9b34fb"/>

<m2:Function Type="serviceId:00001800-0000-1000-8000-00805f9b34fb"/>

<m2:Function Type="serviceId:00001801-0000-1000-8000-00805f9b34fb"/>

<m2:Function Type="serviceId:0000180A-0000-1000-8000-00805f9b34fb"/>




Then, use Generic Access Service to read the Device Name of the Sensor Tag, the code is as follows:

//Find the devices that expose the service

var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.GenericAccess));

if (devices.Count==0)


//Connect to the service

var service = await GattDeviceService.FromIdAsync(devices[0].Id);

if (service == null)


//Obtain the characteristic we want to interact with

var characteristic = service.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0x2A00))[0];

//Read the value

var deviceNameBytes=(await characteristic.ReadValueAsync()).Value.ToArray();

//Convert to string

var deviceName=Encoding.UTF8.GetString(deviceNameBytes,0,deviceNameBytes.Length);

Next, let's take a look at how to read the accelerometer and other data:

//Find the devices that expose the service

var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("F000AA10-0451-4000-B000-000000000000")));

if (devices.Count==0)


//Connect to the service

var accService = await GattDeviceService.FromIdAsync(devices[0].Id);

if (accService == null)


//Get the accelerometer data characteristic

var accData = accService.GetCharacteristics(new Guid("F000AA11-0451-4000-B000-000000000000"))[0];

//Subcribe value changed

accData.ValueChanged += accData_ValueChanged;

//Set configuration to notify

await accData.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);

//Get the accelerometer configuration characteristic

var accConfig = accService.GetCharacteristics(new Guid("F000AA12-0451-4000-B000-000000000000"))[0];

//Write 1 to start accelerometer sensor

await accConfig.WriteValueAsync((new byte[]{1}).AsBuffer());

Subscribe to notifications of data changes. The default data update frequency of the accelerometer is 1 second. We can also modify this value.

async void accData_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)


var values ​​= (await sender.ReadValueAsync()).Value.ToArray();

var x = values[0];

var y = values[1];

var z = values[2];


5. Use third-party libraries

As you all know, if you follow the above steps, you need to write a lot of data interaction code. If you want to be lazy, you can refer to the third-party library. SebastianL provides the TI Sensor Tag open source library on codeplex, the link address is: Link . The current version is 1.2, which supports Windows 8.1. According to the author, a version that supports Windows Phone 8.1 will come soon. Using this library is very simple:

The first step is to add application Capabilities to the newly created Store application. The steps are the same as in section 4 above.

The second step is to download the X2CodingLab.SensorTag library and add a reference to X2CodingLab.SensorTag in the project, as shown in Figure 5 below.

Figure 5

The third step is to instantiate the corresponding sensor in the background file of the page that needs to use the Sensor Tag. Take the acceleration sensor as an example:

Accelerometer accelerometer = new Accelerometer();

The fourth step is to initialize the sensor and call the packaged Initialize() method:

await accelerometer.Initialize();

If there is no matching Sensor Tag, DeviceNotFoundException will be thrown here.

The fifth step is to obtain sensor data. Use the EnableSensor and DisableSensor methods to turn on the data acquisition switch. In addition, if the data acquisition is over, it is best to call DisableSensor to save the power consumption of the Sensor Tag.

await accelerometer.EnableSensor();

await accelerometer.DisableSensor();

Use EnableNotifications() and DisableNotification() to allow or prohibit data notification:

private async void Notify()


await accelerometer.DisableNotifications();

await accelerometer.EnableNotifications();

accelerometer.SensorValueChanged += accelerometer_SensorValueChanged;


void accelerometer_SensorValueChanged(object sender, X2CodingLab.SensorTag.SensorValueChangedEventArgs e)


byte[] sensorData = e.RawData;


Below is a diagram of the application running interface.

Figure 6

Give the picture of the real object again, the red one is the Sensor Tag.

Finally, give the download link of the source code project: Link . OneDrive cannot be used recently. Use a microdisk instead.

Reference: Windows 8.1 Bluetooth Low Energy Development-Cloud + Community-Tencent Cloud