Sunday 6 December 2020

BLE Read operation not working in view model for Xamarin forms

I'm trying to do BLE implementation in Xamarin forms. So I am able to connect and do write operation successfully but when I do read operation the app crashes. The read operation works in code behind that is xaml.cs but it crashes in viewmodel. This is exception I'm getting

Assertion at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mono/mini/debugger-agent.c:4660, condition is_ok (error)' not met, function:get_this_async_id, Could not execute the method because the containing type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TReturn_REF]', is not fully instantiated. assembly: type: member:(null) [libc] Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 24845 (twell.touchless), pid 24845 (twell.touchless)

This is my code in viewmodel

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("00002A39-0000-1000-8000-00805F9B34FB"));
               // characteristic.CanWrite = true;
                //foreach (var characteristic in characteristics)
                //{
                    try
                    {
                    if (Device.RuntimePlatform==Device.iOS)
                    {
                        characteresticnativedata = DependencyService.Get<ICharacteristiciOS>();
                        characteresticnativedata.OpeniosBluetooth(device);
                    }
                   
                    
                    if (characteristic != null)
                        {

                       var sbnew = new StringBuilder("BLE Characteristics\n");
                       byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "0ab" : SendMessageLabel.Text);

                        

                        if (MainThread.IsMainThread)
                        {
                            var newbytes = await characteristic.WriteAsync(senddata);
                        }

                        
                           var charvalue = Characteristic.Value;
                        
                        var bytes = await characteristic.ReadAsync();
                            string str = Encoding.UTF8.GetString(charvalue);
                            
                            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();
            }


        }

This same code var bytes = await characteristic.ReadAsync(); works in xaml.cs but it crashes in viewmodel. I even used GetAwaiter().GetResult() instead of await. But still it crashes.

var bytes = characteristic.ReadAsync.GetAwaiter().GetResult();

I have no clue how to fix this any suggestions?



from BLE Read operation not working in view model for Xamarin forms

No comments:

Post a Comment