How can I use Android to control custom hardware? Use JNI. 1. Make preparations. The preparation is nothing more than setting up the environment and downloading something. See some links. Click me! 2. I should not post the led driver. However, considering that some of my colleagues have never learned to drive Android, I will post it for reference only: [cpp] # include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Define DEVICE_NAME "leds" static int led_gpios [] = {S5PV210_GPJ2 (0), S5PV210_GPJ2 (1), S5PV210_GPJ2 (2), S5PV210_GPJ2 (3 ),}; # define LED_NUM ARRAY_SIZE (led_gpios) static long gec210_leds_ioctl (struct file * filp, unsigned int cmd, unsigned long arg) {switch (cmd) {case 0: case 1: if (arg> LED_NUM) {return-EINVAL;} gpio_set_value (led_gpios [arg],! Cmd); // printk (DEVICE_NAME ": % d \ n", arg, cmd); break; default: return-EINVAL;} return 0 ;} static struct file_operations gec210_led_dev_fops = {. owner = THIS_MODULE ,. unlocked_ioctl = gec210_leds_ioctl,}; static struct miscdevice gec210_led_dev = {. minor = MISC_DYNAMIC_MINOR ,. name = DEVICE_NAME ,. fops = & gec210_led_dev_fops,}; static int _ init gec210_led_dev_init (void) {int ret; int I; For (I = 0; I <LED_NUM; I ++) {ret = gpio_request (led_gpios [I], "LED"); if (ret) {printk ("% s: request GPIO % d for LED failed, ret = % d \ n ", DEVICE_NAME, led_gpios [I], ret); return ret;} initi_gpio_cfgpin (led_gpios [I], initi_gpio_output ); gpio_set_value (led_gpios [I], 1);} ret = misc_register (& gec210_led_dev); printk (DEVICE_NAME "\ tinitialized \ n"); return ret ;} static void _ exit gec210_led_dev_exit (Void) {int I; for (I = 0; I <LED_NUM; I ++) {gpio_free (led_gpios [I]);} misc_deregister (& gec210_led_dev );} module_init (gec210_led_dev_init); module_exit (gec210_led_dev_exit); MODULE_LICENSE ("GPL"); MODULE_AUTHOR ("GEC Inc. "); Makefile is also included: [plain] ifneq ($ (KERNELRELEASE),) obj-m: = led_drv.o else module-objs: = led_drv.o KERNELDIR: =/home/gec/linux_kernel/linux-2.6.35.7/PWD: = $ (shell pwd) default: $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules endif clean: $ (RM )*. ko *. mod. c *. mod. o *. o *. order *. symvers *. cmd that linux-2.6.35.7 is the Linux kernel version of the Android system on the GEC210 board, and the Linux under the PC also need to have the source code of this kernel, the path is put in Makefile, you can also modify the Makefile to download it by yourself. Okay. After executing make, you can get a file ending with. ko. Put this. ko file into the file system on the GEC210 board. How can we put it in? You can also use the SD card, network cable nfs, or serial cable. This is not detailed here. 3. Writing Android applications is not much to mention. It is the most basic to read the code. Version 2.2 LedDemoTestActivity. java [java] package com. gec. leddemotest. activity; import android. app. activity; import android. OS. bundle; import android. widget. radioGroup; import android. widget. radioGroup. onCheckedChangeListener; import android. widget. toast; public class LedDemoTestActivity extends Activity {private RadioGroup radioGroupLed01; private RadioGroup radioGroupLed02; private RadioGroup radioGro UpLed03; private RadioGroup radioGroupLed04; static {System. loadLibrary ("leddemotest") ;}@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); openRunLed (); radioGroupLed01 = (RadioGroup) findViewById (R. id. radioGroupLed01); radioGroupLed01.setOnCheckedChangeListener (new OnCheckedChangeListener () {public void onChecked Changed (RadioGroup group, int checkedId) {// TODO Auto-generated method stub if (checkedId = R. id. chooseLed01_1) {startRunLed (0, 1); Toast. makeText (LedDemoTestActivity. this, R. string. ledbench on, Toast. LENGTH_LONG ). show ();} else if (checkedId = R. id. chooseLed01_2) {startRunLed (0, 0); Toast. makeText (LedDemoTestActivity. this, R. string. ledshortoff, Toast. LENGTH_LONG ). show () ;}}); radioGroupLed02 = (RadioGroup) FindViewById (R. id. radioGroupLed02); listener (new OnCheckedChangeListener () {public void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stub if (checkedId = R. id. chooseLed02_1) {startRunLed (1, 1); Toast. makeText (LedDemoTestActivity. this, R. string. led2_on, Toast. LENGTH_LONG ). show ();} else if (checkedId = R. id. chooseLed02_2) {startRu NLed (1, 0); Toast. makeText (LedDemoTestActivity. this, R. string. led2_off, Toast. LENGTH_LONG ). show () ;}}); radioGroupLed03 = (RadioGroup) findViewById (R. id. radioGroupLed03); listener (new OnCheckedChangeListener () {public void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stub if (checkedId = R. id. chooseLed03_1) {startRunLed (2, 1); Ast. makeText (LedDemoTestActivity. this, R. string. led3_on, Toast. LENGTH_LONG ). show ();} else if (checkedId = R. id. chooseLed03_2) {startRunLed (2, 0); Toast. makeText (LedDemoTestActivity. this, R. string. led3_off, Toast. LENGTH_LONG ). show () ;}}); radioGroupLed04 = (RadioGroup) findViewById (R. id. radioGroupLed04); radioGroupLed04.setOnCheckedChangeListener (new OnCheckedChangeListener () {public void onCheckedC Hanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stub if (checkedId = R. id. chooseLed04_1) {startRunLed (3,1); Toast. makeText (LedDemoTestActivity. this, R. string. led4_on, Toast. LENGTH_LONG ). show ();} else if (checkedId = R. id. chooseLed04_2) {startRunLed (3,0); Toast. makeText (LedDemoTestActivity. this, R. string. led4_off, Toast. LENGTH_LONG ). show () ;}}) ;}protected void onDestroy () {CloseRunLed ();} public native void startRunLed (int whichLed, int on); public native void openRunLed (); public native void closeRunLed ();} create a jni file in the project directory and put the following files in it. Appleds. c [cpp] # include# Include # Include # Include # Include # Include # Define LED_ON 1 # define LED_OFF 0 int fd; JNIEXPORT void JNICALL handle (JNIEnv * env, jobject this, jint whichLed, jint on) {fd = open ("/dev/leds", 0);} JNIEXPORT void JNICALL encode (JNIEnv * env, jobject this, jint whichLed, jint on) {if (fd <0) {perror ("open device leds"); exit (1 );} Ioctl (fd, on, whichLed);} JNIEXPORT void JNICALL handle (JNIEnv * env, jobject this, jint whichLed, jint on) {close (fd );} the preceding JNIEXPORT is required, void is the return type, JNICALL is required, and Java is required. Java is followed by com. gec. leddemotest. the name of the function called under LedDemoTestActivity under the activity package. change. This is probably the meaning. In JNI. in the c file, the parameters of the function to be called are at least two, one is JNIEnv * env and the other is jobject this. for more information, check JNI on the Internet. In addition, parameters such as int should be written as jint in JNI. Although int and jint are actually the same, JNI format is suitable for jint, that is, JNI. all data types used in c can be added with j. Of course, the header file must include jni. h. 4. Compiling Android. mkAndroid. mk is an important part of compiling the. so file. The following figure shows Android in the jni folder. mk [plain] LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = libleddemotest a LOCAL_SRC_FILES: = appleds. c B LOCAL_C_INCLUDES + = \ c $ (JNI_H_INCLUDE) LOCAL_PRELINK_MODULE: = false include $ (BUILD_SHARED_LIBRARY) d: generate the C dynamic library name libleddemotest (the JAVA layer loads the library name for mutual debugging) B: Compile the C file c: load the jni library header file d: generate libleddemotest dynamic library. The following is Android under the project directory. mk [plain] LOCAL_PATH: = $ (call my-dir) a include $ (CLEAR_VARS) B LOCAL_SRC_FILES: = $ (call all-subdir-java-files) c LOCAL_PACKAGE_NAME: = LedDemoTest d environment: = libleddemotest e include $ (BUILD_PACKAGE) f include $ (LOCAL_PATH) /jni/Android. mk g # Use the folloing include to make our test apk. include $ (call all-makefiles-under, $ (LOCAL_PATH) h a: an Android. the mk file must first define LOCAL_PATH to obtain the current directory B: used to initialize Android. variable c of "LOCAL_XXX" in mk file: compile Java File D: generate the name of the Android app apk file e: generate the name of the Android app apk file f: generate the Android app g: Compile the Android app in the jni directory. mk file h: compile all Android. mk File 5. Run ndk-build in cygwin. Run the linux Command to enter the project directory and run ndk-build. 6. Set the permission of the leds device file in a string, connect to the GEC210 board, and execute insmod XXXX. ko file: load the device file into the kernel. Then there will be a leds device file under/dev. Use chmod 777 leds to increase the permission of the leds device. You can run the application on the GEC210 board. Enjoy it. Will be attached to the source code.