Useful tips when building own Khadas android images

Hi all,

I decided to start a topic about some useful tips and tricks found during building image of Android from Khadas sources. Some of the tips are already can be found in the other topics, but it maybe can be good idea to put all things together here.

So here is a first trick. I’m developing an application which will definitely run in privileged mode and I would need to work with serial ports. There is Khadas API which allow to work with serial ports but actually everything is already in the android core. It has serial system service which can be obtained with getSystemService(“serial”) function call.

   SerialManager serialManager = getSystemService("serial");

Note: your app need to have right permission in it’s manifest and run at privileged level

uses-permission android:name=“android.permission.SERIAL_PORT”

Then you can get a list of serial ports in the system with


and open it with


and use read(), write() calls to get/send data to it.

Problem is that serialManager will not allow you to see a list of ports or open any port not in the list. In usual build it’s empty. Fortunately it can be fixed when you build your own android image.

You just need to change a file at ./frameworks/base/core/res/res/values/config.xml (by default it’s empty) under the following lines

    <!-- List of paths to serial ports that are available to the serial manager.for example, /dev/ttyUSB0 -->
<string-array translatable="false" name="config_serialPorts">

and build your system image. After that you will be able to get an access to those ports (only to that which physically exists in the system - if port defined but not in the system - it won’t be returned in getSerialPorts() call - we are safe here)

To get it working in you app - you’ll need to import a couple of Java classes from AOSP ( at /frameworks/base/core/java/android/hardware ) and one aidl file
And it will work - here is I’m getting GPS data from external USB GPS attached to my Vim3L

An only limitation of using system serial port - it works only with standard baud rates… :frowning: If you need something unusual - you have to do that in your own way.

1 Like