gpt4 book ai didi

c# - 通过 GATT (UWP) 向 BLE 设备发送写入请求

转载 作者:太空狗 更新时间:2023-10-29 13:09:43 28 4
gpt4 key购买 nike

我有一个 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();
}
}

关于c# - 通过 GATT (UWP) 向 BLE 设备发送写入请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com