5

I have written an Android app which runs on a custom kernel on Android 4.4 Kitkat device, which uses the Android Serial Port API (https://code.google.com/p/android-serialport-api/) in order to open the serial port "/dev/ttyACM0" which is the port associated with my serial device. The port has the proper "666" permissions (crw-rw-rw), and the app itself even has the WRITE_EXTERNAL_STORAGE" permission.

However, even though the permissions appear correct, when my app tries to open said serial port using the library, it fails. In my java code, if I try to do a "device.canWrite()" it returns false, whereas a "device.canRead()" returns true. I feel like this is a symptom of a greater permissions problem.

If I use the sample apk bundled with the serial port api/library, I get the same issue, where the app fails at the device.canWrite() and throws a SecurityException.

The exact same code DOES successfully run and open when ran on a custom kernel ICS device, for which the app was originally developed for. However, the kernels for both were written by the same developer, and at least on a surface glance, the permissions on the port seem to be the same.

Is it likely this is an issue that I can resolve on the application code side? I have tried some other serial port libraries (USBSerial3.0, usb-serial-for-android-master) and I seem to encounter the same issue.

Regardless of where the issue may lie, would anyone have any ideas of what the problem may be? Making changes to the kernel, app, and api are all on the table. Thank you.

0

3 Answers 3

4

Gabe, Google changed the SELinux mode from Permissive Mode to Enforce Mode in Android 4.4, by default apps do not have the (SELinux) permissions to access the serial ports. Here are a couple of ways to work around the issue: (1). If your device is rooted you can toggle the SELinux mode from Enforce Mode to Permissive Mode: “su setenforce 0”. You will need to run the command every time you restart your device. see http://www.xda-developers.com/easily-change-your-android-selinux-mode/ (2). If you have access to the kernel framework files for the device you can change them and grant serial port access to your apps.

3

run setenforce 0 from shell to disable SELinux. It worked for me and I was able to read/write on tty/ACM0.

1
  • correction: file name /dev/ttyACM0 and device is rooted.
    – Amit
    Oct 13, 2015 at 7:49
2

The issue turned out to be with SELinux, which most of the Samsung line of devices use as a security enforcement policy. That security policy was set to enforcing, such that no apps could access files outside of their designated app space, such as the serial port I was trying to open.

Given that we could change the kernel, we were able to add a policy exception to SELinux that gave my app elevated priviliges, thereby allowing it to open the port correctly.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.