Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

c# - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...