This project is read-only.

Supported project types

  • Windows Store apps in C# written for Windows 8.1

Project setup

Allow the app to access the bluetooth device

  • Open the xml of the manifest file

  • Add all sensor service ids to the app capabilities

    <m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
      <m2:Device Id="any">
        <m2:Function Type="serviceId:0000180a-0000-1000-8000-00805f9b34fb" /> <!--Device Info Service-->
        <m2:Function Type="serviceId:f000aa00-0451-4000-b000-000000000000" /> <!--Temperature Device Service-->
        <m2:Function Type="serviceId:f000aa10-0451-4000-b000-000000000000" /> <!--Accelerometer Device Service-->
        <m2:Function Type="serviceId:f000aa20-0451-4000-b000-000000000000" /> <!--Humidity Device Service-->
        <m2:Function Type="serviceId:f000aa30-0451-4000-b000-000000000000" /> <!--Magnetometer Device Service-->
        <m2:Function Type="serviceId:f000aa40-0451-4000-b000-000000000000" /> <!--Barometer Device Service-->
        <m2:Function Type="serviceId:f000aa50-0451-4000-b000-000000000000" /> <!--Gyroscope Device Service-->
        <m2:Function Type="serviceId:0000ffe0-0000-1000-8000-00805f9b34fb" /> <!--Key Device Service-->

Add a reference to the sensor tag library

Download the library here or use the NuGet package

Library usage

The easiest way is to look at the example provided in the source code. Look here for further explanation.

Read sensor data

To read sensor data, look for the corresponding sensor class in the namespace X2CodingLab.SensorTag.Sensors.

1. Create
First create an instance of that sensor.
Accelerometer accelerometer = new Accelerometer();

2. Initialize
Then initialize the sensor. Do this by calling Initialize() which will look for any available devices and take the first one.
await accelerometer.Initialize();

Else you can get your own DeviceInformation object and pass it to the initialize method to control which exact device should be used to read data. There's also the GetDevicesOfService method in the X2CodingLab.SensorTag.GattUtils which helps you to retrieve all available devices which provide a Special service.
await accelerometer.Initialize(specificSensorTagDeviceInformation);

  • If there's no sensor paired with your device that can deliver the data, the method throws a DeviceNotFoundException
If the computer could not retrieve the GATT service of the device, the method returns false. This can happen if you didn't allow your app access to the device or the app could not ask for access, because the method wasn't executed on the UI thread. You could also pass the wrong device object, which doesn't have a GATT service.

3. Use the sensor data
After initializing the sensor, you can control every sensor with the following methods:

Enable and disable the sensor to be able to read values and receive notifications. Also, use DisableSensor() to save energy.
await accelerometer.EnableSensor();
await accelerometer.DisableSensor();

To start or stop receiving notifications, use EnableNotifications() and DisableNotification() and subscribe to the SensorValueChanged Event.
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;

If you prefere to read the data manually, use the ReadValue() method.
byte[] sensorData = await accelerometer.ReadValue();

  • If you did not initialize the sensor, the methods throw an DeviceNotInitializedException.
  • If the device is initialized and coupled but is not in range or just not ready to be connected, the methods throw an DeviceUnreachableException

Other methods

For other special methods, take a look at the example application.

Last edited Nov 28, 2013 at 9:42 PM by SebastianL, version 10