'How to fix 'Gatt write characteristic FAILED.' exception in xamarin forms?

I am trying to do a GATT write operation in a BLE after Notification where I am getting the value from server. The write operation works fine when there is no prior GATT operation like Notification. So this is the code where I am reading the value through notification

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

This is how I am doing the write operation

 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, 0, chunkLength);
                        Device.BeginInvokeOnMainThread(async () =>
                        {
                            await Task.Delay(300);
                            await _characteristicsBLE.WriteAsync(chunk);
                        });
                        start += 20;
                        
                    }

                   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

I have even called the write operation in main thread to avoid any threading issue. But still I get GATT error. I have no clue how to fix this any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source