Android应用程序:(jni方式)控制LED/GPIO
16lz
2021-01-25
前面文章介绍了如何编写 led 驱动模块 , 这里介绍如何编写一个 Android 应用程序去控制 LED/GPIO. 小弟不才 , 不会用 JAVA, 所以这个应用程序是用 C 写的 , 然后用 java 实现了几个按钮 , 代码和外观都比较丑陋 , 大家勿喷 ! 费话少说 , 贴代码 !
1.新建eclipse项目
2.在led.java中加入publicstaticnativeintled(inti,intj);
led.java:
[java] view plain copy- packagecom.example.led;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.view.Menu;
- publicclassLEDextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.led);
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- getMenuInflater().inflate(R.menu.led,menu);
- returntrue;
- }
- <spanstyle="color:#ff6666;">publicstaticnativeintled(inti,intj);</span>
- }
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- #include<jni.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<sys/ioctl.h>
- #include<android/log.h>
- #defineLOG_TAG"LED"//androidlogcat
- #defineLOGI(...)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
- #defineLOGE(...)__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
- //intmain(intargc,char**argv)
- jintJNICALLJava_com_example_led_Led_led(JNIEnv*env,jclassthiz,jintled_nu,jinton)
- {
- intfd;
- fd=open("/dev/leds0",O_RDWR);
- if(fd<0)
- printf("Can'topen/dev/leds!\n");
- ioctl(fd,on,led_nu);
- LOGI("led_nu=%d,state=%d\n",led_nu,on);
- close(fd);
- return0;
- }
7. 在jni目录下新建Andorid.mk [实际上是为led.c编写makefile]
Android.mk:
[plain] view plain copy- LOCAL_PATH:=$(callmy-dir)
- include$(CLEAR_VARS)
- LOCAL_MODULE:=LED
- LOCAL_SRC_FILES:=led.c
- LOCAL_LDLIBS:=-llog
- LOCAL_C_INCLUDES:=$(MY_ANDROID_SOURCE)/frameworks/base/core/jni/android/graphics\
- include$(BUILD_SHARED_LIBRARY)
用ndk-build编译生成so库
8.回到eclipse中,将生成的so库添加进led.java中
led.java:
[java] view plain copy- packagecom.example.led;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.view.Menu;
- publicclassLEDextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.led);
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- getMenuInflater().inflate(R.menu.led,menu);
- returntrue;
- }
- publicstaticnativeintled(inti,intj);
- static
- {
- System.loadLibrary("LED");
- }
- }
9. 修改布局,在led.xml中为界面添加按钮
res --> layout --> led.xml:
[html] view plain copy- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ToggleButton
- android:id="@+id/btn1"
- android:layout_width="140dip"
- android:layout_height="wrap_content"
- android:textOn="led1on"
- android:textOff="led1off"
- android:layout_gravity="center_horizontal"
- />
- <ToggleButton
- android:id="@+id/btn2"
- android:layout_width="140dip"
- android:layout_height="wrap_content"
- android:textOn="led2on"
- android:textOff="led2off"
- android:layout_gravity="center_horizontal"
- />
- <ToggleButton
- android:id="@+id/btn3"
- android:layout_width="140dip"
- android:layout_height="wrap_content"
- android:textOn="led3on"
- android:textOff="led3off"
- android:layout_gravity="center_horizontal"
- />
- <ToggleButton
- android:id="@+id/btn4"
- android:layout_width="140dip"
- android:layout_height="wrap_content"
- android:textOn="led4on"
- android:textOff="led4off"
- android:layout_gravity="center_horizontal"
- />
- </LinearLayout>
10. 在led.java中添加监听按键代码(最终的led.java)
- packagecom.example.led;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.net.Uri;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.ToggleButton;
- importandroid.util.Log;
- importandroid.widget.CompoundButton.OnCheckedChangeListener;
- publicclassLedextendsActivity{
- privatestaticfinalStringTAG="LED";
- privateToggleButtonbutton1;
- privateToggleButtonbutton2;
- privateToggleButtonbutton3;
- privateToggleButtonbutton4;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.led);
- Log.w(TAG,"layout");
- button1=(ToggleButton)findViewById(R.id.btn1);
- button2=(ToggleButton)findViewById(R.id.btn2);
- button3=(ToggleButton)findViewById(R.id.btn3);
- button4=(ToggleButton)findViewById(R.id.btn4);
- Log.w(TAG,"button");
- button1.setOnClickListener(newButton.OnClickListener()
- {
- publicvoidonClick(Viewv)
- {
- if(button1.isChecked())
- {
- Log.w(TAG,"----led1on");
- led(0,1);
- }
- else
- {
- Log.w(TAG,"----led1off");
- led(0,0);
- }
- }
- });
- button2.setOnClickListener(newButton.OnClickListener()
- {
- publicvoidonClick(Viewv)
- {
- if(button2.isChecked())
- {
- Log.w(TAG,"----led2on");
- led(1,1);
- }
- else
- {
- Log.w(TAG,"----led2off");
- led(1,0);
- }
- }
- });
- button3.setOnClickListener(newButton.OnClickListener()
- {
- publicvoidonClick(Viewv)
- {
- if(button3.isChecked())
- {
- Log.w(TAG,"----led3on");
- led(2,1);
- }
- else
- {
- Log.w(TAG,"----led3off");
- led(2,0);
- }
- }
- });
- button4.setOnClickListener(newButton.OnClickListener()
- {
- publicvoidonClick(Viewv)
- {
- if(button4.isChecked())
- {
- Log.w(TAG,"----led4on");
- led(3,1);
- }
- else
- {
- Log.w(TAG,"-----led4off");
- led(3,0);
- }
- }
- });
- }
- publicstaticnativeintled(inti,intj);
- static
- {
- System.loadLibrary("LED");
- }
- }
11. 编译整个项目,在bin目录下生成led.apk, 拷贝到开发板中就可以安装运行了.
注意:安装led.apk前,请先加载led.ko模块,并确认编译模块所用的内核版本和android版本一至.另外,还需要通过串口修改 /dev/leds0 的权限为 777.(chmod 777 /dev/leds0),否则led.c中的open("/dev/leds0", o_RDWR)会失败.
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- 使用Git之后出现android library引用失败
- Android(安卓)Push Notification实现信息推送使用及修改使用于项
- Android(安卓)局部刷新
- ListView怎么处理大量的加载数据;比如有10万条数据,你在ListView怎
- [置顶] 《老罗的Android之旅》导读PPT
- android中任务栈的处理方式
- 2013年Linux周刊读者投票出炉 Ubuntu、Android榜上有名