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

1

7

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?

Ledesma answered 5/11, 2020 at 21:38 Comment(8)
You read/write are backwards. You want to read the characteristics from the BLE and write results to a byte array. You are writing the characteristics from a byte array.Acidulent
@Acidulent can u explain it through code. Sorry I'm new to this.Ledesma
Following is writing to controlCharacteristic. You want to read.: controlCharacteristic.WriteAsync(new byte[] {COMMAND_BYTE, COMMAND_HEART_RATE_CONTINUOUS, COMMAND_ON});Acidulent
@Acidulent I'm reading the characterics aswell before writing it to byte array. I'm editing my post u can look into it.Ledesma
See code on following page. The Write parameter is an enumeration (not a byte array) : github.com/jeroendesloovere/examples-windows/blob/master/…Acidulent
@Acidulent That project is for windows will it work for android?Ledesma
@Acidulent do u know any xamarin project where it is used?Ledesma
Visual Studio 4.7.2 and later you can write the same c# code for both Windows (Net) and Android/Xamarin (Core) and you can target the code for either Net or Core. The source code doesn't change. In some cases Core is a compact version of Net and doesn't include all the libraries. When a library doesn't exist you may need to add a Github library if one exists. The source doesn't usually have to change if the library is available in both Core and Net.Acidulent
L
3

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.

Ledesma answered 12/11, 2020 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.