Share via


Escenario de GATT: Controlar la presentación de datos de dispositivos 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

Un dispositivo Bluetooth LE para aplicaciones de la Tienda Windows puede exponer un servicio de batería que indica el nivel de batería actual al usuario. El servicio de batería incluye un descriptor PresentationFormat opcional que permite cierta flexibilidad en la interpretación de los datos del nivel de batería. Este escenario proporciona un ejemplo de aplicación que funciona con este tipo de dispositivo y usa la propiedad PresentationFormats para dar formato a un valor de característica antes de presentarlo al usuario.

function initialize() {
    var Gatt = Windows.Devices.Bluetooth.GenericAttributeProfile;
    Windows.Devices.Enumeration.DeviceInformation.findAllAsync(
        Gatt.GattDeviceService.getDeviceSelectorFromUuid(
            Gatt.GattServiceUuids.battery),
        null).done(function (batteryServices) {
          Gatt.GattDeviceService.fromIdAsync(batteryServices[0].id)
            .done(function (firstBatteryService) {
              var batteryLevelCharacteristic = firstBatteryService
                    .getCharacteristics(
                        Gatt.GattCharacteristicUuids.batteryLevel)[0];

                batteryLevelCharacteristic.onvaluechanged =
                    function (args) {
                        var levelData = Windows.Storage.Streams.DataReader
                            .fromBuffer(args.characteristicValue)
                            .readByte();

                        // Retrieve the GattCharacteristic sender
                        var sender = args.target;

                        // if the sender Characteristic has a presentation
                        // format use that information to format the value
                        var levelValue;
                        if (args.target.presentationFormats.length > 0) {
                            levelValue = levelData *
                                Math.pow(
                                    10.0,
                                    args.target
                                        .presentationFormats[0].exponent);
                        } else {
                            levelValue = levelData;
                        }

                        outputDiv.innerText = levelValue.toString();
                    };

                batteryLevelCharacteristic
                    .writeClientCharacteristicConfigurationDescriptorAsync(
                    Gatt
                      .GattClientCharacteristicConfigurationDescriptorValue
                          .notify).done(function (status) {
                            if (Gatt.GattCommunicationStatus.unreachable ==
                                status) {
                              outputDiv.innerText =
                                  "Writing to the device failed !";
                            }
                          });
            });
        });
}