Udostępnij za pośrednictwem


GATT Scenario: Control a Bluetooth LE Thermometer Device (HTML)

A Windows Store App acts as a controller for a fictitious Bluetooth LE Thermometer device. The device also declares a Format characteristic which would allow a user to retrieve the value reading in either Celsius or Fahrenheit degrees, in addition to the standard characteristics of the HealthThermometer profile.

The Windows Store App uses reliable write transactions to make sure that the format and measurement interval are set as a single value.

// 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 !";
                            }
                        });
                });
        });
};