【发布时间】:2017-04-13 04:24:57
【问题描述】:
使用:Windows 10、C# .NET 2015 社区、UWP
我正在尝试构建一个 windows-universal-app 以将我的 PC 与 BLE 设备配对。
目前正在运行的是枚举附近的设备,与选定的设备配对并获取电池电量和固件版本等信息。
现在的问题是,当我尝试获取自定义服务时,我的任务因 .GetGattService 处的“System.Exception”而结束
System.Exception.Message:“未找到元素。(来自 HRESULT 的异常:0x80070490)”
System.Exception.Stack : "在 Windows.Devices.Bluetooth.BluetoothLEDevice.GetGattService(Guid serviceUuid)\r\n 在 SettingsCs.Settings.d__23.MoveNext()"
这是不工作的代码:
private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings)
{
//Check if device is available
if (device != null)
{
Guid SERVICE_CUSTOM = new Guid("7e0bc6be-8271-4f5a-a126-c24220e6250c");
GattDeviceService service = device.GetGattService(SERVICE_CUSTOM);
//Check if service is available
if (service == null)
{
return SettingsReturn.INIT_ERROR;
}
GattCharacteristic characteristic = service.GetCharacteristics(BLETestApp.CHAR_SETTINGS)[0];
//Check if characteristic is available
if (characteristic == null)
{
return SettingsReturn.INIT_ERROR;
}
var writer = new DataWriter();
writer.WriteBytes(byteSettings);
var buffer = writer.DetachBuffer();
await characteristic.WriteValueAsync(buffer);//********
bool success = characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write);
if (success == true)
{
// Take care of the 8 bit byte for the counter (max = 255 (unsigned))
if (TRANSACTION_ID > 250)
{
TRANSACTION_ID = 0;
}
else
{
// Count TANSACTION_ID one up
TRANSACTION_ID++;
}
return SettingsReturn.OK;
}
else
{
return SettingsReturn.WRITE_ERROR;
}
}
else
{
return SettingsReturn.INIT_ERROR;
}
}
我希望有人可以帮助我或告诉我我做错了什么。
【问题讨论】:
-
您可以编辑您的帖子并添加异常详细信息(消息和堆栈跟踪)吗?
-
@PedroLamas 抱歉,我忘记了。现在添加详细信息。
-
您是否设置了必要的功能?您可能需要检查this
-
@PedroLamas 首先感谢您的帮助。在我的代码中,我有
<DeviceCapability Name="bluetooth.genericAttributeProfile"> <Device Id="any"> <Function Type="serviceId:7e0bc6be-8271-4f5a-a126-c24220e6250c" /> </Device> </DeviceCapability>
标签: c# win-universal-app bluetooth-lowenergy