Reputation: 997
I have a BLE device (a fitness tracker), which e.x. shows a message when I send a specific write request to it.
In Android it all works great. The device shows instantly, that it received a message:
BluetoothGattCharacteristic characteristic = ... //connecting to BLE device with specific UUID
byte[] data = new byte[] {(byte)0x01, 0x01};
characteristic.setValue(data);
mBluetoothGatt.writeCharacteristic(characteristic);
But in my UWP app, the BLE device doesn't show any reaction:
var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("00001811-0000-1000-8000-00805f9b34fb")), null);
GattDeviceService service = await GattDeviceService.FromIdAsync(devices[0].Id);
var gattCharacteristic = service.GetCharacteristics(new Guid("00002a46-0000-1000-8000-00805f9b34fb")).First();
//Writing request
var writer = new DataWriter();
writer.WriteBytes(new byte[] { 0x01, 0x01 });
await gattCharacteristic.WriteValueAsync(writer.DetachBuffer());
Does anyone has an idea?
Upvotes: 3
Views: 4712
Reputation: 1029
It can be that you are not connected or you are using the wrong gattCharacteristic service.
Anyway this is how i successfully write to my device:
private async Task SendValues(byte[] toSend)
{
IBuffer writer = toSend.AsBuffer();
try
{
// BT_Code: Writes the value from the buffer to the characteristic.
var result = await gattCharacteristic.WriteValueAsync(writer);
if (result == GattCommunicationStatus.Success)
{
//Use for debug or notyfy
var dialog = new Windows.UI.Popups.MessageDialog("Succes");
await dialog.ShowAsync();
}
else
{
var dialog = new Windows.UI.Popups.MessageDialog("Failed");
await dialog.ShowAsync();
}
}
catch (Exception ex) when ((uint)ex.HResult == 0x80650003 || (uint)ex.HResult == 0x80070005)
{
// E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED or E_ACCESSDENIED
// This usually happens when a device reports that it
//support writing, but it actually doesn't.
var dialog = new Windows.UI.Popups.MessageDialog(ex.Message);
await dialog.ShowAsync();
}
}
Upvotes: 2