GATT Scenario: Control a Bluetooth LE Thermometer Device(XAML)
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 Guid formatCharacteristicUuid(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10); // Constant representing a Fahrenheit scale temperature measurement const unsigned char FAHRENHEIT_READING = 1; void MainPage::Initialize() { create_task(DeviceInformation::FindAllAsync( GattDeviceService::GetDeviceSelectorFromUuid( GattServiceUuids::HealthThermometer), nullptr)).then( [this] (DeviceInformationCollection^ thermometerServices) { create_task(GattDeviceService::FromIdAsync( thermometerServices->GetAt(0)->Id)).then([this] ( GattDeviceService^ thermometerService) { GattCharacteristic^ intervalCharacteristic = thermometerService->GetCharacteristics( GattCharacteristicUuids::MeasurementInterval) ->GetAt(0); GattCharacteristic^ formatCharacteristic = thermometerService->GetCharacteristics( formatCharacteristicUuid)->GetAt(0); GattReliableWriteTransaction^ gattTransaction = ref new GattReliableWriteTransaction(); DataWriter^ writer = ref new DataWriter(); // Get a temperature reading every 60 seconds writer->WriteUInt16(60); gattTransaction->WriteValue( intervalCharacteristic, writer->DetachBuffer()); writer->WriteByte(FAHRENHEIT_READING); gattTransaction->WriteValue( formatCharacteristic, writer->DetachBuffer()); create_task(gattTransaction->CommitAsync()) .then([this] (GattCommunicationStatus status) { if (GattCommunicationStatus::Unreachable == status) { statusTextBlock->Text = ref new String(L"Writing to your device failed !"); } }); }); }); }
Show: