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

标签 c# xamarin.forms bluetooth-lowenergy bluetooth-gatt

我正在开发 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/

相关文章:

c# - 在 Parallel.For 完成作业后引发事件

c# - 确保 Controller 在 Unity 中具有无参数公共(public)构造函数

c# - 使用继承自 List<T> 的类进行 JSON 序列化

c# - Postgres 连接上的 System.Net.Sockets.SocketException

c# - 为什么 Xamarin 不允许 Android 上的某些文件名?

ios - IOS 7中如何将警报级别写入警报级别的特征?

java - 蓝牙 SDP 在某些设备中失败

swift - 无法搜索自定义 CBUUID

c# - 我应该在服务中的哪个位置创建DbContext?

xamarin.forms - 更新 xamarin.forms 后 Android 构建失败