Page 1 of 1

Android Studio project with libfriendlyarm-hardware.so

Posted: Sat Apr 13, 2019 1:24 am
by hoang1127
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.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Sun Apr 14, 2019 7:09 am
by v8dave
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.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Tue Apr 23, 2019 7:16 am
by hoang1127
Hello v8dave,
Thank you. That is good. I can use JNI for communication.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Sun Apr 28, 2019 12:04 pm
by hoang1127
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

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Mon Apr 29, 2019 2:06 am
by v8dave
What device are you passing in for the PORT string?

On the Nano PC T3 Plus I use something like "/dev/ttysac3"

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Mon Apr 29, 2019 2:09 am
by v8dave
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.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Fri May 03, 2019 1:34 pm
by hoang1127
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

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Sat May 04, 2019 8:40 am
by v8dave
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.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Thu May 09, 2019 9:38 pm
by hoang1127
Thank you. I will try it. This way I donot have to rebuild and flash the NanoPC.

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Thu May 09, 2019 10:18 pm
by hoang1127
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

Re: Android Studio project with libfriendlyarm-hardware.so

Posted: Fri May 10, 2019 10:45 am
by v8dave
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