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

1.新建eclipse项目

2.在led.java中加入publicstaticnativeintled(inti,intj);

led.java:

[java] view plain copy
  1. packagecom.example.led;
  2. importandroid.os.Bundle;
  3. importandroid.app.Activity;
  4. importandroid.view.Menu;
  5. publicclassLEDextendsActivity{
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.led);
  10. }
  11. @Override
  12. publicbooleanonCreateOptionsMenu(Menumenu){
  13. getMenuInflater().inflate(R.menu.led,menu);
  14. returntrue;
  15. }
  16. <spanstyle="color:#ff6666;">publicstaticnativeintled(inti,intj);</span>
  17. }

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

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

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

javah-classpathbin/classes-djnicom.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. #defineLOG_TAG"LED"//androidlogcat
  9. #defineLOGI(...)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  10. #defineLOGE(...)__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  11. //intmain(intargc,char**argv)
  12. jintJNICALLJava_com_example_led_Led_led(JNIEnv*env,jclassthiz,jintled_nu,jinton)
  13. {
  14. intfd;
  15. fd=open("/dev/leds0",O_RDWR);
  16. if(fd<0)
  17. printf("Can'topen/dev/leds!\n");
  18. ioctl(fd,on,led_nu);
  19. LOGI("led_nu=%d,state=%d\n",led_nu,on);
  20. close(fd);
  21. return0;
  22. }

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

Android.mk:

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

用ndk-build编译生成so库

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

led.java:

[java] view plain copy
  1. packagecom.example.led;
  2. importandroid.os.Bundle;
  3. importandroid.app.Activity;
  4. importandroid.view.Menu;
  5. publicclassLEDextendsActivity{
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.led);
  10. }
  11. @Override
  12. publicbooleanonCreateOptionsMenu(Menumenu){
  13. getMenuInflater().inflate(R.menu.led,menu);
  14. returntrue;
  15. }
  16. publicstaticnativeintled(inti,intj);
  17. static
  18. {
  19. System.loadLibrary("LED");
  20. }
  21. }


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

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

[html] view plain copy
  1. <LinearLayoutxmlns: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. <ToggleButton
  7. android:id="@+id/btn1"
  8. android:layout_width="140dip"
  9. android:layout_height="wrap_content"
  10. android:textOn="led1on"
  11. android:textOff="led1off"
  12. android:layout_gravity="center_horizontal"
  13. />
  14. <ToggleButton
  15. android:id="@+id/btn2"
  16. android:layout_width="140dip"
  17. android:layout_height="wrap_content"
  18. android:textOn="led2on"
  19. android:textOff="led2off"
  20. android:layout_gravity="center_horizontal"
  21. />
  22. <ToggleButton
  23. android:id="@+id/btn3"
  24. android:layout_width="140dip"
  25. android:layout_height="wrap_content"
  26. android:textOn="led3on"
  27. android:textOff="led3off"
  28. android:layout_gravity="center_horizontal"
  29. />
  30. <ToggleButton
  31. android:id="@+id/btn4"
  32. android:layout_width="140dip"
  33. android:layout_height="wrap_content"
  34. android:textOn="led4on"
  35. android:textOff="led4off"
  36. android:layout_gravity="center_horizontal"
  37. />
  38. </LinearLayout>


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

[java] view plain copy
  1. packagecom.example.led;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.net.Uri;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.widget.Button;
  8. importandroid.widget.ToggleButton;
  9. importandroid.util.Log;
  10. importandroid.widget.CompoundButton.OnCheckedChangeListener;
  11. publicclassLedextendsActivity{
  12. privatestaticfinalStringTAG="LED";
  13. privateToggleButtonbutton1;
  14. privateToggleButtonbutton2;
  15. privateToggleButtonbutton3;
  16. privateToggleButtonbutton4;
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.led);
  21. Log.w(TAG,"layout");
  22. button1=(ToggleButton)findViewById(R.id.btn1);
  23. button2=(ToggleButton)findViewById(R.id.btn2);
  24. button3=(ToggleButton)findViewById(R.id.btn3);
  25. button4=(ToggleButton)findViewById(R.id.btn4);
  26. Log.w(TAG,"button");
  27. button1.setOnClickListener(newButton.OnClickListener()
  28. {
  29. publicvoidonClick(Viewv)
  30. {
  31. if(button1.isChecked())
  32. {
  33. Log.w(TAG,"----led1on");
  34. led(0,1);
  35. }
  36. else
  37. {
  38. Log.w(TAG,"----led1off");
  39. led(0,0);
  40. }
  41. }
  42. });
  43. button2.setOnClickListener(newButton.OnClickListener()
  44. {
  45. publicvoidonClick(Viewv)
  46. {
  47. if(button2.isChecked())
  48. {
  49. Log.w(TAG,"----led2on");
  50. led(1,1);
  51. }
  52. else
  53. {
  54. Log.w(TAG,"----led2off");
  55. led(1,0);
  56. }
  57. }
  58. });
  59. button3.setOnClickListener(newButton.OnClickListener()
  60. {
  61. publicvoidonClick(Viewv)
  62. {
  63. if(button3.isChecked())
  64. {
  65. Log.w(TAG,"----led3on");
  66. led(2,1);
  67. }
  68. else
  69. {
  70. Log.w(TAG,"----led3off");
  71. led(2,0);
  72. }
  73. }
  74. });
  75. button4.setOnClickListener(newButton.OnClickListener()
  76. {
  77. publicvoidonClick(Viewv)
  78. {
  79. if(button4.isChecked())
  80. {
  81. Log.w(TAG,"----led4on");
  82. led(3,1);
  83. }
  84. else
  85. {
  86. Log.w(TAG,"-----led4off");
  87. led(3,0);
  88. }
  89. }
  90. });
  91. }
  92. publicstaticnativeintled(inti,intj);
  93. static
  94. {
  95. System.loadLibrary("LED");
  96. }
  97. }


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

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

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  3. 使用Git之后出现android library引用失败
  4. Android(安卓)Push Notification实现信息推送使用及修改使用于项
  5. Android(安卓)局部刷新
  6. ListView怎么处理大量的加载数据;比如有10万条数据,你在ListView怎
  7. [置顶] 《老罗的Android之旅》导读PPT
  8. android中任务栈的处理方式
  9. 2013年Linux周刊读者投票出炉 Ubuntu、Android榜上有名

随机推荐

  1. Button的高宽无故变大了!
  2. 转Android(安卓)安全攻防(三): SEAndroid(安
  3. Android中Handler消息处理机制原理
  4. EventBus使用详解(二)——EventBus使用进
  5. Android(安卓)轮播图---ViewFlipper
  6. android开发之路04(初级android工程师必会
  7. PopupWindow(一)
  8. android 5 设置图案锁,锁屏界面进入相机拍
  9. hunting job
  10. Android(安卓)开发小提示集合