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。
1589772710(1)

https://wiki.radxa.com/Rockpi4/android-mraa

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 https://github.com/radxa/Android-MraaDemo 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
1590546779(1)
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
java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe
java_vm_ext.cc:542] string: '���eeee
java_vm_ext.cc:542] ’
java_vm_ext.cc:542] input: ‘<0xfe> 0xef 0xef 0x65 0x65 0x65 0x65 0x0a’
java_vm_ext.cc:542] in call to NewStringUTF
java_vm_ext.cc:542] from java.lang.String mraa.mraaJNI.Uart_readStr(long, mraa.Uart, int)
java_vm_ext.cc:542] “main” prio=5 tid=1 Runnable
java_vm_ext.cc:542] | group=“main” sCount=0 dsCount=0 flags=0 obj=0x745a3770 self=0x723c614c00
java_vm_ext.cc:542] | sysTid=6589 nice=-10 cgrp=default sched=0/0 handle=0x72c222c548
java_vm_ext.cc:542] | state=R schedstat=( 798584411 44647914 363 ) utm=70 stm=8 core=5 HZ=100
java_vm_ext.cc:542] | stack=0x7fdabfd000-0x7fdabff000 stackSize=8MB
java_vm_ext.cc:542] | held mutexes= “mutator lock”(shared held)