Serial Port Code Works on Windows, but Throws Error on Android

Yusuf-3141 45 Reputation points
2024-09-25T03:12:46.0633333+00:00

Hi
I'm using this code in my .NET MAUI app to list available serial ports:

using System.IO.Ports;

namespace PortMauiApp1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            ListAvailablePorts();
        }

        private void ListAvailablePorts()
        {
            partsLabel.Text = "";
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                partsLabel.Text += port + "\n";  
            }
        }
    }
}

It works fine in Windows, but on Android it throws this error:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

Any idea how to fix this?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,371 Reputation points Microsoft Vendor
    2024-09-25T06:31:29.87+00:00

    Hello,

    It works fine in Windows, but on Android it throws this error:

    This is because SerialPort.GetPortNames is not support Android platform.

    On Android, you need to use UsbManager to get the serial device.

    
    #if ANDROID
    
                var act = Platform.CurrentActivity;
    
                UsbManager manager = (UsbManager)act.GetSystemService(Context.UsbService);
    
                IDictionary<string, UsbDevice> devicesDictionary = manager.DeviceList;
    
    #endif
    
    

    Please refer to Android.Hardware.Usb Namespace for more details about android native serial APIs.

    In addition, Android has a policy that prevents access to serial ports by default. You could refer to SerialPort is not supported on Android ? #86619 for more details in repletion from akoeplinger.

    I researched a bit and it looks like while in theory the serial port devices are there on Android there's a SELinux policy which prevents you from accessing them: https://stackoverflow.com/questions/30742524/why-cant-i-open-write-from-a-serial-port-on-android That'd really only leave rooted devices or custom OEM versions of Android, I don't think it makes sense for us to support those. I'll add PNSE and the supported OS attributes.


    If you need to access USB-to-serial devices then this library looks like a good alternative choice since it uses the USB Host APIs from Android instead: https://github.com/anotherlab/UsbSerialForAndroid

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.