Skip to content

Instantly share code, notes, and snippets.

@neelratanguria
Created April 19, 2024 12:09
Show Gist options
  • Save neelratanguria/cfe1552f14c8e742ce410bd580281379 to your computer and use it in GitHub Desktop.
Save neelratanguria/cfe1552f14c8e742ce410bd580281379 to your computer and use it in GitHub Desktop.
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(id);
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
if (result.Status == GattCommunicationStatus.Success) {
LogEvent("DEBUG: Services discovered successfully");
foreach (GattDeviceService service in result.Services) {
// Discover characteristics for each service
GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsAsync();
if (characteristicsResult.Status == GattCommunicationStatus.Success) {
LogEvent("DEBUG: Characteristics discovered for service " + service.Uuid);
foreach (GattCharacteristic characteristic in characteristicsResult.Characteristics) {
// Process each characteristic as needed
// characteristic.Uuid will give you the UUID of the characteristic
// Example: Writing to a characteristic
var writer = new Windows.Storage.Streams.DataWriter();
writer.WriteByte(0x01); // Example data to write
GattCommunicationStatus writeStatus = await characteristic.WriteValueAsync(writer.DetachBuffer());
if (writeStatus == GattCommunicationStatus.Success) {
LogEvent("DEBUG: Write to characteristic " + characteristic.Uuid + " succeeded");
} else {
LogEvent("ERROR: Write to characteristic " + characteristic.Uuid + " failed");
}
}
} else {
LogEvent("ERROR: Failed to discover characteristics for service " + service.Uuid);
}
}
} else {
LogEvent("ERROR: Failed to discover services");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment