如何修复 GattStatus: 3 - BLE Xamarin 表单应用程序的 WriteNotPermitted 异常?

How to fix GattStatus: 3 - WriteNotPermitted exception for BLE Xamarin forms application?

我正在开发 BLE 应用程序。我能够连接到 MI band 并通过我的 Xamarin forms BLE 应用程序获得服务。但是当我尝试编写特征时,我遇到了异常。我遇到异常

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是否有写操作,因为你可以从Play商店下载一个名为BLE scanner的应用,然后连接到该设备。当我们连接到那个 BLE 时,我们将能够看到 BLE 外设的可用服务和特征。 然后我们需要检查哪些特征有写操作。因此,如果您尝试为在外围设备中没有写操作的特性执行写特性,它将给出 不允许写入异常 .