Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, how is it possible to make serial ports (ttyS0, ttyS1) r/w for everyone?

Tags:

android

I've developed an app that listens to the serial ports and I've been using "chmod 666 /dev/ttyS0" for read/write access to the serial port - however - this method relies on "su" and is only temporary.

What needs to be done to gain read/write access to the serial ports permanently ?

like image 533
Someone Somewhere Avatar asked Sep 13 '25 02:09

Someone Somewhere


1 Answers

Unfortunately you will always require superuser/root access to do this on COTS hardware (but then most off-the-shelf Android devices don't have accessible serial ports).

If you are in control of the device firmware/ROM, you could potentially make other arrangements so that the init.rc file sets ownership of the serial device so that only your app has access, but this seems more difficult than probably necessary.

Your best bet is to require a rooted device, and your app will need to set the permissions on the serial device file every time before it is opened. If your phone has the su app installed (which is typical for rooted Android devices), then your app can easily do this automatically. The first time the app attempts to do this, the user will get a confirmation dialog, but they can check a box so that the decision to allow is remembered.

On a rooted device, you should be able to do something like:

try {
    Runtime.getRuntime().exec("su -c 'chmod 666 /dev/ttyS0'");
} catch (IOException e) {
    e.printStackTrace();
}
like image 179
Joel F Avatar answered Sep 15 '25 16:09

Joel F