Android uses JNI to implement serial communication with C language Operation Bluetooth hardware module

Source: Internet
Author: User

Always wanted to write a technical document, but because the ability to self-inductance is limited and can not be written, recently made an Android platform to achieve and C language communication to operate the Bluetooth module project, the middle encountered a lot of problems, but also in the online search a lot of information, after the main function. There are some people who ask me some questions on the Internet. Writing this document here is a periodic summary.

Assuming a good response. You may be sent an android stub with the new Android HAL Learning document.

Because the Bluetooth module is a serial communication mechanism. Use the C language to access, while the Android application layer uses Java. The hardware cannot be directly manipulated. Therefore, the use of JNI technology to achieve the main functions. The jni of Android should be part of the Android Hal, because the Android HAL mechanism is not fully explored at the moment, so the program architecture here is the HAL mechanism of the old version number. Program schemas such as the.

First, what is JNI?

JNI is the abbreviation for Java Native interface, and Chinese is a native call to Java. From Java1.1, the Java Native Interface (JNI) standard became part of the Java platform, agreeing that Java code interacts with code written in other languages.

The JNI begins with a locally compiled language. Specifically, C and C + + are designed, but it is best to interfere with other languages, just call the Convention supported. (The above content is from Baidu Encyclopedia)

Second, how to use JNI?

1. Before using JNI, you should make sure that the following software environment is installed on your PC:

Android SDK: Version number and your hardware test environment version number same, download on Google's Android website

Android NDK: Used to compile a shared library of C files for. So. Assuming you are developing a Linux-only SDK with Android, you do not need to

Cygwin: for compiling C files

Android Development environment: The configuration of the Android development environment online a lot, here no longer repeat

2, the program because only use Java C, and do not need C Java, so in the application layer C function is very easy, just need to add Nativekeyword before a method (native method can not have method body), and then the compiled shared library can be imported.

For example, the following:

Public class Bluetoothjni {

Static {

System.loadlibrary ("JNI");

}

Public native Static int Bluetoothon (String StrAT);

Public native Static void Bluetoothoff (String StrAT);

Public native Static String readserial ();

Public native Static void Write (String StrAT);

}

3. after saving project, Eclipse will take the initiative to compile the Java file into a class file. We use the Javah command to compile the class file into a C header file. For example, the following:

Running this command will generate a. h C header file under Project's Bin folder.

A function declaration for the corresponding Java native method is generated in the header file.

Create a new C file with the same name as the header file and implement all functions.

4, because the bottom of Android is based on the Linux kernel. So some system functions under Windows are not available. Linux under the serial communication article online there are a lot of code here, it is not posted out. We introduce jni.h and the newly generated header file in the C file to ensure that JNI can be called. Note: The core part of the whole program is here, the C language and the Bluetooth module serial Port Communications Service, I have encountered the most problems here.

Third, compile C file

1, create a new folder under Android-ndk-r5b\samples\ Bluetooth\jni, copy the C file and the header file, create a new makefile.

For example, the following:

Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_c_includes: = $ (Jni_h_include)

Local_ldlibs: =-lm-llog

Local_module: = JNI

Local_src_files: = Com_android_semisky_bluetooth_util_bluetoothjni.c

Include $ (build_shared_library)

2. Open the Cygwin bash shell and go to the Bluetooth folder. Run Ndk-build to compile.

3. Copy the libjni.so file generated under Libs/armeabi into the Libs/armeabi folder of Androidproject. Install APK to the Development Board. Run the program.

Iv. problems that may arise

1. The problem of serial read and write permission

The online method is to obtain the SU account in Java to change the permissions or add a command in the init.rc to modify the permissions and finally compiled into an IMG to burn into the development Board. Both of these methods I did not succeed, and finally no way to run the shell command under DOS to change

2. c File serial reading and writing problems

I do not know whether my program has a problem or the Bluetooth module, the Bluetooth module when the data returned slightly larger. Data is sometimes returned in cases where it is not returned.

3, garbled problem

The data returned by Bluetooth sometimes garbled and unresolved.

4, the problem of serial number

Suppose you return an FD less than 1 when open, make sure your development board string is correct. The protocol is correct and the permissions are changed.

5, the biggest big problem

Read the serial Code snippet algorithm problem, optimize the problem, otherwise the data will return to abnormal.

Android uses JNI to implement serial communication with C language Operation Bluetooth hardware module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.