How to use SerialPort sample code in ContentPage Xamarin Forms? RRS feed

  • Question

  • User395909 posted

    For a project, I need to connect a device having Serial Port to an android smartphone.

    I found a sample project here :


    The project uses an Activity so it has very basic UI but it works perfectly.

    However, I need some more UI so I created a new Xamarin Forms project. I have also designed the contentpage.

    But I am unable to use the code from the sample project in my app.

    I keep getting compiler errors at certain lines.

    Can you help me convert the activity to a Content Page?

    Saturday, July 25, 2020 3:35 PM

All replies

  • User365353 posted

    Really, Xamarin Forms controls have representations in each platform. F. e. XF Button is iOS UIButton. But Android Activity is not a ContentPage. ContentPage is presented by ViewGroup in Android. On this page you will see controls implementations. Activity is something more complex then ContentPage. Activity is whole application and it is not presented in portable project. This will help you to understand renderers and project structure. So, if you need to manage controls use renderers.

    But what is USB? This is not UI control, wright? This is a service. Because we can't see this. To implement USB in your portable project you need to use DependencyService DependencyService is using to manage platform specific functions (USB, Bluetooth, location, files, etc). So, use the DependencyService to manage USB.

    Saturday, July 25, 2020 5:46 PM
  • User395909 posted

    @igorkr_10, According to the sample code I have got (I have included the link in the question), I need to use Intent.GetParcelableExtra("PortInfo")....

    However, I am getting a compiler error that an object reference is required for the non static object, field, method or property Intent. Also, in the sample code, there is a line Context....... Since it was an activity, Context was directly used. However when using that in a Content Page, it got changed to Android.Application.App.Context. Maybe this can also return null.

    Finally, if you can spare some time, pls download the sample project and try to find out about its implementation in a ContentPage. I just want to use it in my project and I have no idea about its implementation.

    I also found a Nuget package Xamarin.Android.SerialPort (nothing for Xamarin.Forms). However, the Github link of the project is also not working so I didn't get its implementation either. Is communicating with serial ports so hard in Xamarin that no examples, no video tutorials are available on the web? Also, no one is ready to answer the question on Forums by giving codes. Most of the answers only direct to some links with more information but no exact implementation. If you can, pls post some code to benefit all the novice Xamarin developers like me!

    Thanks :) !

    (I have already made a serial communication program in C# .NET. However, since we can't use System.IO.Ports in Android, I can't use that either!)

    Sunday, July 26, 2020 9:43 AM
  • User365353 posted

    You cannot use references on Android classes in portable project. Portable project is just a library that knows nothing about Android or iOS. If you need to get current Activity in Android project use plugin CurrentActivity in Nuget by James Montemagno. And as I said in previous post use the DependencyService to get platform specific things. ContentPage is just a View from XF library, and it doesn't know about Android.

    Sunday, July 26, 2020 8:47 PM
  • User395909 posted

    @igorkr_10: Is there any easy way like a nuget package or something to communicate with Serial Ports for Xamarin Forms? Something that could be used like a c# .NET SerialPort directly? If not, then maybe I have to go the long way round. :|

    Tuesday, July 28, 2020 12:08 PM