Power & Source of Big Ideas

Android Studio project with libfriendlyarm-hardware.so

Moderators: chensy, FATechsupport

Hello,

Are there any Android Studio project using libfriendlyarm-hardware.so library?
Using NanoPC-T4 and build the SerialPortDemo with libfriendlyarm-hardware.so.

The steps:
-Create Android Studio project
-Add the library libfriendlyarm-hardware.so under /src/main/jniLibs/armeabi
-Add build.gradle:
android{
...
sourceSets.main {
jniLibs.srcDir 'src/main/libs' }
...
}
- Call in OnCreate() :
devfd = HardwareControler.openSerialPort( devName, speed, dataBits, stopBits );

Error: The app crashes with error:
No implementation found for int com.test.HardwareControler.openSerialPort()

Thank you.
If you know how to build a JNI then there are a few options that will be easer than using the FA .so and don't need you to build with the platform keys.

This one looks easy.

https://github.com/chzhong/serial-android

I also have one but I don't have the time just now to package it up. I will get around to making a library for it soon.
Hello v8dave,
Thank you. That is good. I can use JNI for communication.
Hello v8dave,

There is a problem I write the JNI code. But can not open the serial port / UART.
When call following function
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);

It always returns -1

Do you have any suggestion? Here is the snap code:

===================
SerialImpl::SerialImpl (const string &port,
unsigned long baudrate,
bytesize_t bytesize,
parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: port_ (port), fd_ (-1), is_open_ (false), xonxoff_ (false), rtscts_ (false),
baudrate_ (baudrate), parity_ (parity),
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
{
pthread_mutex_init(&this->read_mutex, NULL);
pthread_mutex_init(&this->write_mutex, NULL);
if (port_.empty () == false)
open ();
}

void SerialImpl::open ()
{
if (port_.empty ()) {
throw invalid_argument ("Empty port is invalid.");
}
if (is_open_ == true) {
throw SerialException ("Serial port already open.");
}
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);

LOGV("OPEN fd value: %d", fd_); // return -1
if (fd_ == -1) {
switch (errno) {
case EINTR:
// Recurse because this is a recoverable error.
open ();
return;
case ENFILE:
case EMFILE:
THROW (IOException, "Too many file handles open.");
default:
THROW (IOException, errno);
}
}
reconfigurePort();
is_open_ = true;
}

====================
Do you know there are any source code about android native for UART and I2C communication from FriendlyElec?

Thank you
Hoang
What device are you passing in for the PORT string?

On the Nano PC T3 Plus I use something like "/dev/ttysac3"
hoang1127 wrote:
Hello v8dave,
Do you know there are any source code about android native for UART and I2C communication from FriendlyElec?


They have a driver but your code needs to be signed with the platform keys. I wrote my own I2C, GPIO and UART drivers with the JNI. There are some open source options out there.
Hello v8dave,

Can you send a link where I can get source code of I2C driver? I could list all 6 I2C ports, but I can not open any of them.

i2c_fd = open("/dev/i2c-3", O_RDWR);

The function open() return i2c_fd = -1
Do you know why.
Thank you
Hoang
hoang1127 wrote:
Hello v8dave,
i2c_fd = open("/dev/i2c-3", O_RDWR);

The function open() return i2c_fd = -1
Do you know why.
Thank you
Hoang


It is likely that the ports are protected. Because I run a custom build of Android my init.rc has the following for each i2c port.

Code: Select all

chmod 0666 /dev/i2c-0
chown system system /dev/i2c-0


If you connect with ADB you should be able to type those commands and then try your code again. You may have to use adb su first and then try them.
Thank you. I will try it. This way I donot have to rebuild and flash the NanoPC.
It works. I can open I2C port.
/////////////
nanopc-t4:/ # chmod 0666 /dev/i2c-0
nanopc-t4:/ # chown system system /dev/i2c-0
11-19 02:52:42.566 2452 2452 I: Open i2c device node files/address.txt
11-19 02:52:42.567 2452 2452 E: i2c_fd : 63
/////////////
Thanks
Good. The only downside is that you will have to do the ADB commands each time.

Better to build your own custom OS and include these in the INIT.RC or maybe you can edit the existing INIT.RC but you will need to mount it read/write but should be able to do this from ADB

Who is online

In total there are 0 users online :: 0 registered, 0 hidden and 0 guests (based on users active over the past 5 minutes)
Most users ever online was 291 on Tue Aug 13, 2019 1:34 am

Users browsing this forum: No registered users and 0 guests