How to fix exception Device xxx disconnected while writing characteristic with yyy in xamarin forms?

Published

I am creating a BLE app where I am successfully connecting to a BLE device. I am able to read the GATT characteristics as well. But when I try to do write operation I get exception

Device xxx disconnected while writing characteristic with yyy

This is my code

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("00002b0f-0000-1000-8000-00805f9b34fb"));

                //{
                    try
                    {
                        
deviceInfoService.GetCharacteristicAsync(GattCharacteristicIdentifiers.DataExchange);
                        if (characteristic != null)
                        {
                        var sbnew = new StringBuilder("BLE Characteristicsn");
                        byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "0x21" : SendMessageLabel.Text);

 

                        await Task.Delay(300);
                        
                         
                        var newbytes = await characteristic.WriteAsync(senddata);
                   

                        byte[] characteristicsvalue = characteristic.Value;
                        var str = Encoding.UTF8.GetString(characteristicsvalue);



                        sbnew.AppendLine($"Characteristics found on this device: {string.Join(", ", str.ToString())}");
                        CharactericsLabel.Text = sbnew.ToString(); 

                        
                   
                    }

 
                }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
          
                return CharactericsLabel.Text;

            }
            catch (Exception ex)
            {
             
                return ex.ToString();
            }
   
        }

After some search I found that I need to pass hexadecimal value.Even I tried sending hexadecimal value as you can see in code but it’s not working. For android its working fine but for iOS it is still showing this exception.I’m using device Iphone 8 plus and OS version of Iphone is 13.5.1.The write operation works in Light Blue app which I downloaded from App store for iOS. My write type is set to default which is write with response. But it is giving exception in my xamarin app only for the iOS part. I’m using latest stable version 2.1.1 of Plugin.BLE. I have no clue how to fix this any suggestions?

Source: Ios Questions

Published
Categorised as bluetooth-lowenergy, c++, ios, utf-8, xamarin.forms Tagged , , , ,

Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq