Android 9 how to open the serial port

Android 9 system is installed on rock PI 4,i create an app to connect by the serial port,but is there something wrong with the code below:
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/xbin/su");
there is no access on the file,must i root the system or there are other solutions。I have tried
several methods to root the system,but filed。

This is because you do not have permission to operate on /dev/ttys0 or /dev/ttys4.
You can use the “adb shell” in command line, enter Android’s shell, and execute:
su; chmod 777 /dev/ttys4
In this way, you can get the permission to read and write uart4.

Or you can try to use it in your app Code:
su = Runtime. getRuntime(). exec (“su”); // switch to root account
The root account itself has read and write permissions to /dev/ttyS4 or /dev/ttyS0.

Hi,first of all, thank you for your answer。In your way,the question still exist。When i used the adb shell commod ,my usrname is rk3399 not root。So i suspect root is needed。

I hope this helped you.

Wow,i think you solved my problem, i will hava a try。

By mraa,i could send data,but i could not receive the data。I dont konw why。

mraa_uart can be used with adb. Try this tool.

Excuse me,I don’t understand what you mean

OK, Maybe the demo can help you.

Hi,I used the demo,there is the same question,could not receive the data,the funcition send is normal。I wonder what the problem is!!!!!!!

  1. What is your test environment?
  2. What is your serial port configuration?
  3. Which serial port are you using?

Please be more specific when you ask questions. How can I help you solve the problem???

Use of the UART function of mraademo: after you input the serial port, you need to add the Enter key, and then click “recv” to receive the serial port data.

Ok,the android img version is rockpi-4b-rk3399-android9-20200522_1559_2f865d4-gpt。
The MraaDemo setting is below
The Serial tool configur is below

Send data “hello” in app ,you could see the data in Serial tool,but send data in the serial tool,there is no data in app by click the REC button。

Hi,I didnot see where to add the enter key,could you cut a picture?

When sending string, enter the Enter key at the end; when sending hexadecimal data, add 0A at the end; this is the flag bit when receiving.

1 Like

Thank you very much。
A problem was found during the test] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe] string: '���eeee] ’] input: ‘<0xfe> 0xef 0xef 0x65 0x65 0x65 0x65 0x0a’] in call to NewStringUTF] from java.lang.String mraa.mraaJNI.Uart_readStr(long, mraa.Uart, int)] “main” prio=5 tid=1 Runnable] | group=“main” sCount=0 dsCount=0 flags=0 obj=0x745a3770 self=0x723c614c00] | sysTid=6589 nice=-10 cgrp=default sched=0/0 handle=0x72c222c548] | state=R schedstat=( 798584411 44647914 363 ) utm=70 stm=8 core=5 HZ=100] | stack=0x7fdabfd000-0x7fdabff000 stackSize=8MB] | held mutexes= “mutator lock”(shared held)

Hi,now I could communicate with mraa through the ROCK_PI_4_UART4,and I also want to communicate by /dev/ttyS4。
I execute the following commands on my computer
C:\Users\63401>adb shell
rk3399:/ $ su
rk3399:/ # chmod 777 /dev/ttyS4
rk3399:/ #
then I could nomal communiacte by the /dev/ttyS4。And when restart the Android system,open the app unable to communicate。I must execute the commands again on computer,the communicate become normal。

second:I modefiy program code “su = Runtime.getRuntime().exec(”/system/xbin/su");" to “su = Runtime.getRuntime().exec(“su”);” then cannot communicate through serial port 4。
My system img is rockpi-4b-rk3399-android9-20200522_1559_2f865d4-gpt and setting TunerSettings -> System -> Root Access -enable-> APP SU Access.

This is very simple. We just need to change the way of entering commands manually to automatically execute this operation at startup.
You can think of this method as being modified in Linux “/etc/rc.local” Documents.
This method requires a file to be modified, because there is no tool in adb shell to modify the file, so you need to pull the file to the PC to make the modification, and then upload it to Android.

  1. Get root permission:
     $ adb root
  1. Set /system to can read and write:
     $ adb remount
  1. Copy the ueventd.rc file to PC:
     $ adb pull /ueventd.rc .
  1. Modification ueventd.rc file and add at the end of the file:

# Uart device node
/dev/ttyS4 0777 root root

  1. Copy the ueventd.rc file to android:
     $ adb push ueventd.rc /
  1. Reboot Android:
     $ reboot
  1. done
1 Like

Thank you very much!:smiley: