[英]Send write request to BLE device via GATT (UWP)
我有一个BLE设备(健身追踪器),当我向它发送特定的写请求时,它会显示一条消息。
在Android中它一切都很棒。 设备立即显示,它收到一条消息:
BluetoothGattCharacteristic characteristic = ... //connecting to BLE device with specific UUID
byte[] data = new byte[] {(byte)0x01, 0x01};
characteristic.setValue(data);
mBluetoothGatt.writeCharacteristic(characteristic);
但在我的UWP应用程序中,BLE设备没有显示任何反应:
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());
有没有人有想法?
可能是您没有连接或您正在使用错误的gattCharacteristic服务。
无论如何,这是我成功写入我的设备的方式:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.