gpt4 book ai didi

c# - 在 xamarin 表单中使用 yyy 编写特征时如何修复异常设备 xxx 已断开连接?

转载 作者:行者123 更新时间:2023-12-05 06:57:49 25 4
gpt4 key购买 nike

我正在创建一个 BLE 应用程序,我已成功连接到 BLE 设备。我也能够阅读 GATT 特性。但是当我尝试写操作时出现异常

Device xxx disconnected while writing characteristic with yyy

这是我的代码

private async Task<string> ProcessDeviceInformationService(IService deviceInfoService)
{
try
{
await adapter.ConnectToDeviceAsync(device);
var sb = new StringBuilder("Getting information from Device Information service: \n");
var characteristics = await deviceInfoService.GetCharacteristicsAsync();
var characteristic = await deviceInfoService.GetCharacteristicAsync(Guid.Parse("00002b0f-0000-1000-8000-00805f9b34fb"));

//{
try
{

deviceInfoService.GetCharacteristicAsync(GattCharacteristicIdentifiers.DataExchange);
if (characteristic != null)
{
var sbnew = new StringBuilder("BLE Characteristics\n");
byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "0x21" : SendMessageLabel.Text);



await Task.Delay(300);


var newbytes = await characteristic.WriteAsync(senddata);


byte[] characteristicsvalue = characteristic.Value;
var str = Encoding.UTF8.GetString(characteristicsvalue);



sbnew.AppendLine($"Characteristics found on this device: {string.Join(", ", str.ToString())}");
CharactericsLabel.Text = sbnew.ToString();



}


}
catch (Exception ex)
{
return ex.Message;
}

return CharactericsLabel.Text;

}
catch (Exception ex)
{

return ex.ToString();
}

}

经过一些搜索,我发现我需要传递十六进制值。即使我尝试发送十六进制值,如您在代码中所见,但它不起作用。 对于 android 它工作正常但对于 iOS 它仍然显示此异常。我使用的设备是 Iphone 8 plus 并且 Iphone 的操作系统版本是 13.5.1。写入操作在 Light Blue 中有效 我从 iOS 应用商店下载的应用。我的写入类型设置为默认,即write with response。但它在我的 xamarin 应用程序中仅针对 iOS 部分给出了异常(exception)。我使用的是最新稳定的 Plugin.BLE 2.1.1 版。我不知道如何解决这个任何建议?

最佳答案

我通过从外围端获取不响应特征的写入来修复此异常。

关于c# - 在 xamarin 表单中使用 yyy 编写特征时如何修复异常设备 xxx 已断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64806979/

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