AnswerBun.com

BLE Read operation not working in view model for Xamarin forms

Stack Overflow Asked by Judson Abraham on December 24, 2020

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 Characteristicsn");
                       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?

Add your own answers!

Related Questions

Replace duplicates items in array with different values

3  Asked on November 7, 2021 by mohamed-eshaftri

         

How to fix object “x” not found

2  Asked on November 7, 2021 by ronie-febriansah

 

link error when using random_device from boost

2  Asked on November 7, 2021 by dikiidog

     

Error: invalid input syntax for type integer

0  Asked on November 7, 2021 by hammad-ali

     

android studio : My app crashes immediately after start

5  Asked on November 7, 2021 by user13930404

   

Int + Str type python

1  Asked on November 7, 2021

     

Javascript Output is giving empty response

0  Asked on November 7, 2021 by manish-jha

     

Create many CSV Files based on Pandas df column value

2  Asked on November 7, 2021 by eithar

       

How to change radio button checked attribute

2  Asked on November 7, 2021 by white-death

       

Vue js/Javascript Objects.assign() not working

0  Asked on November 7, 2021 by madsongr

     

Problems with HTTP/2 on nginx for Windows?

0  Asked on November 7, 2021 by camaross

     

Error to start application after insert Entity Framework

1  Asked on November 7, 2021 by bircastri

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP