GATT Scenario: Control Presentation of Bluetooth LE Device Data(XAML)
A Windows Store App Bluetooth LE devices may expose a battery service that provides the current battery level to the user. The battery service includes an optional PresentationFormat descriptor which allows some flexibility in interpretation of the battery level data. This scenario provides example of an App that works with such a device and uses the PresentationFormats property to format a characteristic value, before presenting it to the user.
void MainPage::Initialize() { create_task(DeviceInformation::FindAllAsync( GattDeviceService::GetDeviceSelectorFromUuid( GattServiceUuids::Battery), nullptr)).then([this] (DeviceInformationCollection^ batteryServices) { create_task(GattDeviceService::FromIdAsync( batteryServices->GetAt(0)->Id)).then([this] ( GattDeviceService^ batteryService) { GattCharacteristic^ batteryLevelCharacteristic = batteryService->GetCharacteristics( GattCharacteristicUuids::BatteryLevel)->GetAt(0); batteryLevelCharacteristic->ValueChanged += ref new TypedEventHandler< GattCharacteristic^, GattValueChangedEventArgs^> (this, &MainPage::BatteryLevelChanged); create_task(batteryLevelCharacteristic ->WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue ::Notify)); }); }); } void MainPage::BatteryLevelChanged( GattCharacteristic^ sender, GattValueChangedEventArgs^ eventArgs) { unsigned char batteryLevelData = DataReader::FromBuffer( eventArgs->CharacteristicValue)->ReadByte(); // if this characteristic has a presentation format // use that information to format the value double batteryLevelValue; if (sender->PresentationFormats->Size > 0) { batteryLevelValue = batteryLevelData * pow(10.0, sender->PresentationFormats->GetAt(0)->Exponent); } else { batteryLevelValue = batteryLevelData; } std::wstringstream str; str << batteryLevelValue; batteryLevelTextBlock->Text = ref new String(str.str().c_str()); }
Show: