gpt4 book ai didi

c# - 如何修复 GattStatus : 3 - WriteNotPermitted exception for BLE Xamarin forms application?

转载 作者:行者123 更新时间:2023-12-03 09:32:36 24 4
gpt4 key购买 nike

我正在开发 BLE 应用程序。我能够连接到 MI 频段并通过我的 Xamarin 表单 BLE 应用程序获取服务。但是当我尝试编写特性时,我遇到了异常。我得到了异常(exception)

Characteristic does not support write.


对于方法 WriteAsync() .这是我写入特性的代码:
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("00002A27-0000-1000-8000-00805F9B34FB"));
// characteristic.CanWrite = true;
//foreach (var characteristic in characteristics)
//{
try
{
// await Task.Delay(300);
var bytes = await characteristic.ReadAsync();
var str = Encoding.UTF8.GetString(bytes, 0, 0);
ManufacturerLabel.Text = str;
//var characteristic = await deviceInfoService.GetCharacteristicAsync(GattCharacteristicIdentifiers.DataExchange);
if (characteristic != null)
{
byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "jenx.si was here" : SendMessageLabel.Text);
await Task.Delay(300);
var newbytes = await characteristic.WriteAsync(senddata);
var strnew = Encoding.UTF8.GetString(senddata, 0, 0);
ManufacturerLabel.Text = newbytes.ToString();
//var strnew = Encoding.UTF8.GetString(newbytes, 0, 0);
}

// ManufacturerLabel.Text = str;
}
catch (Exception ex)
{
return ex.Message;
}
//}

return sb.ToString();
}
我不知道如何解决这个问题有什么建议吗?

最佳答案

我解决了这个问题。首先我们需要检查 Characteristic 是否有写操作 为此,您可以下载一个名为 的应用程序。 BLE 扫描仪 从 Play 商店并连接到该设备。当我们连接到该 BLE 时,我们将能够看到 BLE 外设的可用服务和特性。 然后我们需要检查哪些特征有写操作 .因此,如果您尝试为在外围设备中没有写操作的特性做写特性,它会给出 不允许异常写入 .

关于c# - 如何修复 GattStatus : 3 - WriteNotPermitted exception for BLE Xamarin forms application?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64705692/

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