如何解决如何修复“Gatt 写入特性失败” xamarin 形式的例外?
我正在尝试在通知后在 BLE 中执行 GATT 写操作,我从服务器获取值。当没有先前的 GATT 操作(如通知)时,写入操作可以正常工作。所以这是我通过通知读取值
的代码public async Task ReadConfigData(ICharacteristic characteristics)
{
if (characteristics != null)
{
try
{
await _adapter.ConnectToDeviceAsync(_device);
characteristics.ValueUpdated += (o,e) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
//var readvalue2 = characteristics.Value;
var bytes = e.Characteristic.Value;
//var readvalue = await characteristics.ReadAsync();
BLEresultnew = System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine(BLEresultnew);
//if(BLEresultnew.Contains("start"))
//{
concat += BLEresultnew;
//}
});
};
await characteristics.StartUpdatesAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
这就是我如何进行写操作
public async task WriteDataAsync(String data)
{
if (_characteristicsBLE != null)
{
try
{
// await _adapter.ConnectToDeviceAsync(_device);
byte[] senddata = Encoding.UTF8.GetBytes(data);
int start = 0;
while (start < senddata.Length)
{
int chunkLength = Math.Min(20,senddata.Length - start);
byte[] chunk = new byte[chunkLength];
Array.Copy(senddata,start,chunk,chunkLength);
Device.BeginInvokeOnMainThread(async () =>
{
await Task.Delay(300);
await _characteristicsBLE.WriteAsync(chunk);
});
start += 20;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
我什至在主线程中调用了写操作以避免任何线程问题。但我仍然收到 GATT 错误。我不知道如何解决这个问题,有什么建议吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。