我有一个BLE设备(健身追踪器),其中e.x.当我向其发送特定的写请求时显示一条消息。
在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());
有没有人有想法?
答案 0 :(得分:2)
可能是您没有连接或使用错误的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();
}
}