Escenario de GATT: Controlar un dispositivo de termómetro Bluetooth LE (HTML)

[ Este artículo está destinado a desarrolladores de Windows 8.x y Windows Phone 8.x que escriben aplicaciones de Windows en tiempo de ejecución. Si estás desarrollando para Windows 10, consulta la documentación más reciente

Una aplicación de la Tienda Windows actúa como controlador del dispositivo ficticio de termómetro Bluetooth LE. El dispositivo también declara una característica de formato que podría permitir a un usuario recuperar la lectura del valor en grados Celsius o Fahrenheit, además de las características estándar del perfil HealthThermometer.

La aplicación de la Tienda Windows usa transacciones de escritura de confianza para garantizar que los intervalos de medida y formato se establecen en un valor único.

// Uuid of the "Format" Characteristic Value
var formatCharacteristicUuid = "{00000000-0000-0000-0000-000000000010}";

// Constant representing a Fahrenheit scale temperature measurement
var fahrenheitReading = 1;

function intialize() {
    var Gatt = Windows.Devices.Bluetooth.GenericAttributeProfile;

    Windows.Devices.Enumeration.DeviceInformation.findAllAsync(
        Gatt.GattDeviceService.getDeviceSelectorFromUuid(
        Gatt.GattServiceUuids.healthThermometer),
        null).done(function (thermometerServices) {
            // App implemented UI to allow the user to select
            // the service they want to work with
            getUserSelectionAsync(thermometerServices).
                done(function (thermometerService) {
                    var intervalCharacteristic = thermometerService
                      .getCharacteristics(
                          Gatt.GattCharacteristicUuids
                              .measurementInterval)[0];

                    var formatCharacteristic = thermometerService
                        .getCharacteristics(formatCharacteristicUuid)[0];

                    var gattTransaction =
                        new Gatt.GattReliableWriteTransaction();

                    var writer = new Windows.Storage.Streams.DataWriter();
                    // Get the temperature every 60 seconds
                    writer.writeUInt16(60);

                    gattTransaction.writeValue(
                        intervalCharacteristic,
                        writer.detachBuffer());

                    // Get the temperature on the Fahrenheit scale
                    writer.writeByte(fahrenheitReading);

                    gattTransaction.writeValue(
                        formatCharacteristic,
                        writer.detachBuffer());

                    gattTransaction.commitAsync()
                        .done(function (gattCommunicationStatus) {
                            if (Gatt.GattCommunicationStatus.unreachable ==
                                gattCommunicationStatus) {
                                
                                outputDiv.innerText =
                                    "Writing to the device failed !";
                            }
                        });
                });
        });
};