GATT Scenario: Retrieve Bluetooth LE Data
Language: HTML | XAML

GATT Scenario: Retrieve Bluetooth LE Data(XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

A Windows Store App consumes data from a Bluetooth LE device while running in the foreground.

In this example, the Windows Store App consumes temperature measurements from a Bluetooth LE device that implements the Bluetooth LE Health Thermometer Service. In the App's initialization routine, the App specifies that it wants to be notified when a new temperature measurement is available. By registering an event handler for the "Thermometer Characteristic Value Changed" event, the App will receive characteristic value changed event notifications while it is running in the foreground.

Note that when the App is suspended, it must release all device resources and when it resumes, it must perform device enumeration and initialization once again.

double convertTemperatureData(byte[] temperatureData)
    // Read temperature data in IEEE 11703 floating point format
    // temperatureData[0] contains flags about optional data - not used
    UInt32 mantissa = ((UInt32)temperatureData[3] << 16) |
        ((UInt32)temperatureData[2] << 8) |

    Int32 exponent = (Int32)temperatureData[4];

    return mantissa * Math.Pow(10.0, exponent);

async void Initialize()
    var themometerServices = await Windows.Devices.Enumeration

    GattDeviceService firstThermometerService = await

    serviceNameTextBlock.Text = "Using service: " + 

    GattCharacteristic thermometerCharacteristic =

    thermometerCharacteristic.ValueChanged += temperatureMeasurementChanged;

    await thermometerCharacteristic

void temperatureMeasurementChanged(
    GattCharacteristic sender,
    GattValueChangedEventArgs eventArgs)
    byte[] temperatureData = new byte[eventArgs.CharacteristicValue.Length];

    var temperatureValue = convertTemperatureData(temperatureData);

    temperatureTextBlock.Text = temperatureValue.ToString();



© 2017 Microsoft