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 due to the limited self-inductance ability is not able to pen, recently made a on the Android platform to achieve and C language communication to operate the Bluetooth module project, the middle encountered a lot of problems, but also on the internet a lot of information, in the completion of the main functions, there are some people on the internet asked me some questions Writing this document here is a periodic summary. If the response is good, follow-up will be sent on the Android stub with the new version of the Android Hal Learning document.

Because the Bluetooth module is a serial communication mechanism, using the C language to access, and Android application layer in Java, can not directly manipulate the hardware. 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, so the program architecture here is the old version of the HAL mechanism. program architecture such as.

First, what is JNI?

JNI is the abbreviation for Java Native interface, and Chinese is a native call to Java. Starting with Java1.1, the Java Native Interface (JNI) standard becomes part of the Java platform, which allows Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is 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 is the same as your hardware test environment version, download on Google's Android website

Android NDK: Used to compile a shared library of C files for. So, if you are developing under Linux and have an Android full SDK, 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-tune Java, so the application layer C function is very simple, just need to add the native keyword before a method (native method cannot have method body), and then the compiled shared library can be imported.

As follows:

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 the project, Eclipse will automatically compile the Java file into class file, we use The Javah command compiles the class file into a C header file. as follows:

Executing this command will generate a. h C header file in the project's Bin directory. A function declaration of 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 Windows system functions are not available, Linux serial communication articles on the internet there are many, here the code is numerous, it is not posted out. , we introduce jni.h with 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 directory under Android-ndk-r5b\samples\ bluetooth\jni, copy the C file and the header file, and create a new makefile. As follows:

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 Cygwin Bash shell, enter the Bluetooth directory, execute ndk-build to compile.

3. Copy the libjni.so file generated under Libs/armeabi into the Libs/armeabi directory of Android Project. Install APK into the Development Board and execute the program.

Iv. problems that may arise

1. The problem of serial read and write permission

The online method is to get the SU account in Java to change the permissions or add commands in the init.rc to modify the permissions finally compiled into the IMG into the Development Board, and these two methods I did not succeed, and finally no way to execute the shell command under DOS to modify

2. c File serial reading and writing problems

I don't know if it's my problem with the program or the Bluetooth module, when the Bluetooth module returns a slightly larger data, the data sometimes returns sometimes does not return.

3, garbled problem

The data returned by Bluetooth sometimes garbled and unresolved.

4, the problem of serial number

If you return an FD less than 1 when open, make sure that your Development Board string is correct, that the protocol is correct, and that the permissions are modified.

5, the biggest big problem

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

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.