question
Adding a serial port application to an Android 11 device encounters an error when opening the serial port:
System.err: Caused by: java.io.IOException: error=13, Permission denied
Solution steps
- Add permission in APP to apply for
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /
- System-level application
AndroidManifest.xml adds android.uid.system
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
>
Only the system application configuration is introduced here. If it is an ordinary application, the modification method is different.
- Configure selinux
to modify according to the prompt of avc
type=1400 audit(0.0:1182): avc: denied { read write } for name="ttyS2" dev="tmpfs" ino=12037 context=u:r:system_app:s0 tcontext =u:object_r:ttyS_device:s0 tclass=chr_file permissive=0
Modify according to the avc print prompt, different platforms have different modification places, here is the mtk platform
device/mediatek/sepolicy/basic/non_plat/system_app.te
# serial port
allow system_app ttyS_device:chr_file { open read write ioctl };
- Device permissions
Since the board corresponds to mt6771, modify
device/mediatek/mt6771/init.mt6771.rc
and add
chmod 0666 /dev/ttyS2
Different types of CPUs correspond to different directories, and this step needs to be changed, otherwise the following error will appear
open() fd = -1
Cannot open port
native open returns null
openSerialPort: Open serial port exception: java.io.IOException
Author: too handsome to go out