Recently received a project,Demand is to do a special project Andrews board serial read and write in order to achieve some specific external devices to interact with - yes you read right,Andrews is also a board can have serial port!

Many friends do mobile development may not come into contact with - in fact, before I did not come into contact with。He stepped on seven or eight hours a pit,Finally climb out,Here to do a summary,Each board may project different circumstances,We must improvise。

First of all you need to know

Jumper,In order to save the project board USB interface,The default external USB interfaces are not connected to the computer debugging,According to this board for me to get started,Next to that is that close to the RJ45 port USB cable will have a jumper,It can be connected to a dedicated port of the computer。--Of course,Do not forget to turn on debug mode。

Find your serial number,Usually there will be on board the specification,Or view the sample program is also in line。

Java Can notDirectly to the serial port of Android to read and write,Use C or C ++ job,Therefore, to use the more advanced JNI to bridge,The part of the serial read and write the made-link library。

Read the serial portDo not need root,At least I did not encounter,Before the process step on the pit there are a few tips without permission,But in the end I was proved wrong path。

Multiple processes (threads) or the app iscanSimultaneous access to a serial port of the same,The result is likely to serial dataWill be lost,Shows that the "loss"。

mission target

My goal is simple,You do not even need to send information,Just change a signal from an external device needed to read,Specific to the project,It is understood to be 0 and 1 like。

The specific process

My board system 4.4.4,I do use Android Studio,First is to create an empty project。

Add NDK ,This is a C library to use Java:

Add NDK

Automatically download and install the,as will configure everything for you,Next in the upper left corner of your project to selected project "Project":

Switching the display mode

Then app/main Right-click on,New JNI directory:

New JNI directory

Then put into the following five files in the directory:

JNI in five documents required

jni ← Click to download

This side contains the files needed to bridge,Will be used later,Next, create a new directory called in java android_serialport_api Package,Note that this name must be a。A new java class within this package,Named SerialPort ,Follows:

After the addition of Gradle to link to the C ++,This time as you would choose to build the system,Choose NDK-build ,Then follow the prompts to find five file we just downloaded in Android.mk

Gradle link to C ++

After linking,You can choose Make Project in the Build menu ,After the make,Check the catalog to see if successfully generated link library:

Generate dynamic link library

After confirming the success of,To clean up and rebuild the entire project,Oh, right,As a test case, MainActivity You can write:

Noteworthy

This project looks simple,But in fact most of the pit where the C library is written in question,It can be chosen online are almost always in this file ...... SerialPort.c First 116 Row,And also 128 Row,You will see the version I commented out the two detection,In fact, this problem fucked me three hours!

I get our hands on this board,It is to return -1 of! He returned -1 Nothing wrong! So until I suspect that life is a desperate fight ...... ...... only to find the true life of the pedicle。

In short,If you find that there was a strange problem in the implementation process,Then put a block of statements Anti comment if the two just fine。

If the first pass,Then you have toI thank。 :)

Reference Reading

Android Studio serial jni development

Android Application Development [] - (19) Android serial programming principles and implementation(With source code)

Android Studio : Serial communication using jni

Android serial port (SerialPort) a brief summary of the development issues

Published by R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Join the Conversation

7 Comments

Your email address will not be published. Required fields are marked *

  1. Hello there,I also use this jni you use the serial port to read and write,At present no problem to send,With serial tool FBI,But the reception is very strange。I ask you if you had encountered device to receive data,It receives only part of the data,Another portion of the data has been sent to the peer equipment where? such as,Computer equipment to send asdfgh,By mInputStream.read(buffer)Read,Read-only to sdfgh,Then received a computer terminal

  2. Recently doing the mainland class electronic cards
    Bloggers just found this article
    Helped me a great
    Thank