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开发者_JS百科 be done to gain read/write access to the serial ports permanently ?
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();
}
Maybe this can help others.
- Find your init.rc file on your linux repo directory "root"
- Put "chmod 666 /dev/ttyS0" to anywhere
- Compile your Linux Kernel and get uImage
- And you are ready to use your Serial Port
I assume you mean a virtual serial port over USB? There are no physical serial ports on any of the Android devices I know.
About your question: you can't. Applications on normal (non-rooted) devices are sandboxed and do not have privileges to invoke system commands like chmod
.
精彩评论