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.