Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a BLE application. I am able to connect to MI band and get the services through my Xamarin forms BLE app. But when I am trying to write characteristics I am getting exception. I'm getting exception

Characteristic does not support write.

for the method WriteAsync(). This is my code where I am writing to characteristics:

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();
        }

I have no clue how to fix this any suggestions?

like image 861
Judson Abraham Avatar asked Nov 05 '20 21:11

Judson Abraham


1 Answers

I resolved this. First of all we need to check if the Characteristic has the write operation in it for that you can download an app called BLE scanner from play store and connect to that device. When we connect to that BLE we will be able to see the available services and characteristics of the BLE Peripheral. And there we need to check which characteristics has write operation. So if you try to do write characteristics for characteristics which doesn't have a write operation in peripheral it will give exception write not permitted.

like image 165
Judson Abraham Avatar answered Oct 11 '22 21:10

Judson Abraham