Android应用程序:(jni方式)控制LED/GPIO

前面文章介绍了如何编写 led 驱动模块 , 这里介绍如何编写一个 Android 应用程序去控制 LED/GPIO. 小弟不才 , 不会用 JAVA, 所以这个应用程序是用 C 写的 , 然后用 java 实现了几个按钮 , 代码和外观都比较丑陋 , 大家勿喷 ! 费话少说 , 贴代码 !

1.新建eclipse项目

 

2.在led.java中加入public static native int led(int i, int j);

  led.java:

[java]  view plain copy
  1. package com.example.led;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class LED extends Activity {  
  8.   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.led);  
  13.     }  
  14.   
  15.     @Override  
  16.     public boolean onCreateOptionsMenu(Menu menu) {  
  17.         getMenuInflater().inflate(R.menu.led, menu);  
  18.         return true;  
  19.     }  
  20.       
  21.     <span style="color:#ff6666;">public static native int led(int i, int j);</span>    
  22. }  

3.编译项目文件,bin目录下会生成led.apk.

4.终端进入项目目录,新建jni目录

5.利用javah命令生成头文件,该头文件中包含了符合jni格式的函数名,

    javah -classpath bin/classes -d jni com.example.led.LED

6. jni目录下新建led.c

  此c程序实际上是linux下的LED测试程序,函数入口更换为上面javah生成的函数名,以便java调用.

[cpp]  view plain copy
  1. #include <jni.h>  
  2. #include <stdio.h>     
  3. #include <stdlib.h>     
  4. #include <fcntl.h>  
  5. #include <unistd.h>     
  6. #include <sys/ioctl.h>     
  7. #include <android/log.h>  
  8.   
  9. #define LOG_TAG "LED"       //android logcat  
  10. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__    )  
  11. #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS_    _)  
  12.   
  13. //int main(int argc, char **argv)    
  14. jint JNICALL Java_com_example_led_Led_led(JNIEnv *env, jclass thiz, jint led_nu, jint on)   
  15. {    
  16.     int fd;    
  17.       
  18.     fd = open("/dev/leds0", O_RDWR);  
  19.     if(fd < 0)    
  20.     printf("Can't open /dev/leds!\n");    
  21.       
  22.     ioctl(fd, on, led_nu);    
  23.     LOGI("led_nu=%d,state=%d\n", led_nu, on);  
  24.     close(fd);    
  25.       
  26.     return 0;    
  27. }  

7. 在jni目录下新建Andorid.mk  [实际上是为led.c编写makefile]

    Android.mk:

[plain]  view plain copy
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4. LOCAL_MODULE := LED   
  5. LOCAL_SRC_FILES := led.c  
  6. LOCAL_LDLIBS := -llog  
  7. LOCAL_C_INCLUDES := $(MY_ANDROID_SOURCE)/frameworks/base/core/jni/android/graphics \  
  8. include $(BUILD_SHARED_LIBRARY)  
  9.     

用ndk-build编译生成so库

 

8.回到eclipse中,将生成的so库添加进led.java中

   led.java:

[java]  view plain copy
  1. package com.example.led;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class LED extends Activity {  
  8.   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.led);  
  13.     }  
  14.   
  15.     @Override  
  16.     public boolean onCreateOptionsMenu(Menu menu) {  
  17.         getMenuInflater().inflate(R.menu.led, menu);  
  18.         return true;  
  19.     }  
  20.       
  21.     public static native int led(int i, int j);  
  22.       
  23.     static   
  24.     {  
  25.         System.loadLibrary("LED");  
  26.     }  
  27. }  


9. 修改布局,在led.xml中为界面添加按钮

  res --> layout --> led.xml:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"   
  5.     android:orientation="vertical"  >    
  6.   
  7.   
  8.     <ToggleButton   
  9.               android:id="@+id/btn1"          
  10.               android:layout_width="140dip"          
  11.               android:layout_height="wrap_content"          
  12.               android:textOn="led1 on"          
  13.               android:textOff="led1 off"          
  14.               android:layout_gravity="center_horizontal"   
  15.               />  
  16.     <ToggleButton   
  17.               android:id="@+id/btn2"          
  18.               android:layout_width="140dip"          
  19.               android:layout_height="wrap_content"          
  20.               android:textOn="led2 on"          
  21.               android:textOff="led2 off"          
  22.               android:layout_gravity="center_horizontal"   
  23.               />  
  24.     <ToggleButton   
  25.               android:id="@+id/btn3"          
  26.               android:layout_width="140dip"          
  27.               android:layout_height="wrap_content"          
  28.               android:textOn="led3 on"          
  29.               android:textOff="led3 off"          
  30.               android:layout_gravity="center_horizontal"   
  31.               />  
  32.     <ToggleButton   
  33.               android:id="@+id/btn4"          
  34.               android:layout_width="140dip"          
  35.               android:layout_height="wrap_content"          
  36.               android:textOn="led4 on"          
  37.               android:textOff="led4 off"          
  38.               android:layout_gravity="center_horizontal"   
  39.               />  
  40.   
  41. </LinearLayout>  


10. 在led.java中添加监听按键代码(最终的led.java)

[java]  view plain copy
  1. package com.example.led;  
  2.   
  3. import android.app.Activity;    
  4. import android.content.Intent;    
  5. import android.net.Uri;    
  6. import android.os.Bundle;    
  7. import android.view.View;    
  8. import android.widget.Button;   
  9. import android.widget.ToggleButton;  
  10. import android.util.Log;  
  11. import android.widget.CompoundButton.OnCheckedChangeListener;  
  12.   
  13.   
  14. public class Led extends Activity {  
  15.     private static final String TAG = "LED";  
  16.     private ToggleButton button1;  
  17.     private ToggleButton button2;  
  18.     private ToggleButton button3;  
  19.     private ToggleButton button4;  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {    
  23.         super.onCreate(savedInstanceState);    
  24.         setContentView(R.layout.led);    
  25.         Log.w(TAG,"layout");  
  26.   
  27.         button1 = (ToggleButton)findViewById(R.id.btn1);    
  28.         button2 = (ToggleButton)findViewById(R.id.btn2);    
  29.         button3 = (ToggleButton)findViewById(R.id.btn3);  
  30.         button4 = (ToggleButton)findViewById(R.id.btn4);   
  31.         Log.w(TAG,"button");        
  32.        button1.setOnClickListener(new Button.OnClickListener()  
  33.         {  
  34.             public void onClick(View v)  
  35.             {  
  36.                 if (button1.isChecked())   
  37.                 {  
  38.                     Log.w(TAG,"----led1 on");   
  39.                     led(01);  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     Log.w(TAG,"----led1 off");  
  44.                     led(00);  
  45.                 }  
  46.             }  
  47.         });   
  48.          
  49.         button2.setOnClickListener(new Button.OnClickListener()  
  50.         {  
  51.             public void onClick(View v)  
  52.             {  
  53.                 if (button2.isChecked())   
  54.                 {  
  55.                     Log.w(TAG,"----led2 on");   
  56.                     led(11);  
  57.                 }  
  58.                 else  
  59.                 {  
  60.                     Log.w(TAG,"----led2 off");  
  61.                     led(10);  
  62.                 }  
  63.             }  
  64.         });     
  65.     
  66.         button3.setOnClickListener(new Button.OnClickListener()  
  67.         {  
  68.             public void onClick(View v)  
  69.             {  
  70.                 if (button3.isChecked())   
  71.                 {  
  72.                     Log.w(TAG,"----led3 on");   
  73.                     led(21);  
  74.                 }  
  75.                 else  
  76.                 {  
  77.                     Log.w(TAG,"----led3 off");  
  78.                     led(20);  
  79.                 }  
  80.             }  
  81.         });     
  82.   
  83.         button4.setOnClickListener(new Button.OnClickListener()  
  84.         {  
  85.             public void onClick(View v)  
  86.             {  
  87.                 if (button4.isChecked())   
  88.                 {  
  89.                     Log.w(TAG,"----led4 on");   
  90.                     led(31);  
  91.                 }  
  92.                 else  
  93.                 {  
  94.                     Log.w(TAG,"-----led4 off");  
  95.                     led(30);  
  96.                 }  
  97.             }  
  98.         });     
  99.             
  100.     }    
  101.      
  102.     public static native int led(int i, int j);  
  103.       
  104.     static   
  105.     {  
  106.         System.loadLibrary("LED");  
  107.     }  
  108.   
  109. }  


11. 编译整个项目,在bin目录下生成led.apk, 拷贝到开发板中就可以安装运行了.

       注意:安装led.apk前,请先加载led.ko模块,并确认编译模块所用的内核版本和android版本一至.另外,还需要通过串口修改 /dev/leds0 的权限为 777.(chmod 777 /dev/leds0),否则led.c中的open("/dev/leds0", o_RDWR)会失败.

转载于:https://my.oschina.net/f839903061/blog/134262

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值