获取GattService[C#UWP]时任务结束
使用:Windows 10、C#NET 2015社区、UWP 我正在尝试构建一个windows通用应用程序,将我的PC与一个可扩展设备配对 已经开始工作的是枚举附近的设备,与选定的设备配对,并获取电池电量和固件版本等信息 现在的问题是,当我尝试获取自定义服务时,由于.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()” 这是不起作用的代码:获取GattService[C#UWP]时任务结束,c#,win-universal-app,bluetooth-lowenergy,C#,Win Universal App,Bluetooth Lowenergy,使用:Windows 10、C#NET 2015社区、UWP 我正在尝试构建一个windows通用应用程序,将我的PC与一个可扩展设备配对 已经开始工作的是枚举附近的设备,与选定的设备配对,并获取电池电量和固件版本等信息 现在的问题是,当我尝试获取自定义服务时,由于.GetGattService处出现“System.Exception”,任务结束 System.Exception.Message:“未找到元素。(来自HRESULT的异常:0x80070490)” System.Exception
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;
}
}
private async Task writeSettingTransition(蓝牙设备,字节[]字节设置)
{
//检查设备是否可用
如果(设备!=null)
{
Guid服务——自定义=新Guid(“7e0bc6be-8271-4f5a-a126-c24220e6250c”);
GattDeviceService=device.GetGattService(服务\自定义);
//检查服务是否可用
if(服务==null)
{
返回设置return.INIT_错误;
}
GattCharacteristic characteristic=service.GetCharacteristics(BLETestApp.CHAR_设置)[0];
//检查特征是否可用
如果(特征==null)
{
返回设置return.INIT_错误;
}
var writer=newdatawriter();
writer.WriteBytes(字节设置);
var buffer=writer.DetachBuffer();
等待特征。WriteValueAsync(缓冲区)//********
bool success=characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write);
if(success==true)
{
//注意计数器的8位字节(最大值=255(无符号))
如果(事务ID>250)
{
事务_ID=0;
}
其他的
{
//数一数TANSACTION\u ID up
事务_ID++;
}
返回设置return.OK;
}
其他的
{
返回设置return.WRITE_错误;
}
}
其他的
{
返回设置return.INIT_错误;
}
}
我希望没有人能帮助我,或者告诉我我做错了什么。我无法确定您所遇到的错误,请使用调试器检查您的特征是否具有写入权限 我已以成功写入设备的方式更改了您的代码。 还添加了一个try-catch块。 这是:
private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings)
{
bool success = false;
//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;
}
IBuffer writeBuffer = byteSettings.AsBuffer();// using Windows.Storage.Streams
try
{
// BT_Code: Writes the value from the buffer to the characteristic.
var result = await characteristic.WriteValueAsync(writeBuffer);
if (result == GattCommunicationStatus.Success)
{
// NotifyUser("Successfully wrote value to device" );
success = true;
}
else
{
// NotifyUser($"Write failed: {result}");
success = false;
}
}
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.
// NotifyUser(ex.Message, NotifyType.ErrorMessage);
}
if (success)
{
// 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;
}
}
private async Task writeSettingTransition(蓝牙设备,字节[]字节设置)
{
布尔成功=假;
//检查设备是否可用
如果(设备!=null)
{
Guid服务——自定义=新Guid(“7e0bc6be-8271-4f5a-a126-c24220e6250c”);
GattDeviceService=device.GetGattService(服务\自定义);
//检查服务是否可用
if(服务==null)
{
返回设置return.INIT_错误;
}
GattCharacteristic characteristic=service.GetCharacteristics(BLETestApp.CHAR_设置)[0];
//检查特征是否可用
如果(特征==null)
{
返回设置return.INIT_错误;
}
IBuffer writeBuffer=byteSettings.AsBuffer();//使用Windows.Storage.Streams
尝试
{
//BT_代码:将值从缓冲区写入特征。
var result=wait characteristic.WriteValueAsync(writeBuffer);
if(结果==GattCommunicationStatus.Success)
{
//NotifyUser(“已成功将值写入设备”);
成功=真实;
}
其他的
{
//NotifyUser($“写入失败:{result}”);
成功=错误;
}
}
当((uint)ex.HResult==0x80650003 | | |(uint)ex.HResult==0x80070005)时捕获(异常ex)
{
//E_蓝牙_附件_写入_不允许或E_访问被拒绝
//这通常发生在设备报告它支持写入,但实际上不支持写入时。
//NotifyUser(例如Message,NotifyType.ErrorMessage);
}
如果(成功)
{
//注意计数器的8位字节(最大值=255(无符号))
如果(事务ID>250)
{
事务_ID=0;
}
其他的
{
//数一数TANSACTION\u ID up
事务_ID++;
}
返回设置return.OK;
}
其他的
{
返回设置return.WRITE_错误;
}
}
其他的
{
返回设置return.INIT_错误;
}
}
可能会有拼写错误和其他不幸,希望能有所帮助。您能编辑您的帖子并添加异常详细信息(消息和堆栈跟踪)吗?@PedroLamas抱歉,我忘了。现在添加了详细信息。是否设置了必要的功能?您可能需要先查看@PedroLamas,谢谢您的帮助。在我的代码中有
谢谢你的帮助。遗憾的是,我仍然在gattdeviceservice中遇到同样的异常