http://www.arm9home.net/read.php?tid-14336-fpage-0-toread--page-1.html

参考友善的LED程序,自己也写了个,不是用友善的libfriendlyarm-hardware.so,自己用NDK中的例子hello-jni写了个程序,已经编译成libhello-jni.so,可惜上不了图。

自己摸索了好久总算有点收获,感谢各位网友的帮忙。部门源码贴出来给大家参考! 刚搞完就拿出来分享了,欢迎大家指正。有谁想看libfriendlyarm-hardware.so的源码的可以参考这

个。

JNI部分 用C语言实现

复制代码
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  20. int fd;
  21. /* This is a trivial JNI example where we use a native method
  22. * to return a new VM String. See the corresponding Java source
  23. * file located at:
  24. *
  25. * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  26. */
  27. jstring
  28. Java_com_helloworld_helloworld_stringFromJNI( JNIEnv* env,
  29. jobject thiz )
  30. {
  31. return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  32. }
  33. jint Java_com_helloworld_helloworld_openled( JNIEnv* env,
  34. jobject thiz )
  35. {
  36. fd = open("/dev/leds0", O_RDONLY);
  37. if (fd < 0)
  38. {
  39. fd = open("/dev/leds", O_RDONLY);
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. jint Java_com_helloworld_helloworld_setLedState( JNIEnv* env,
  45. jobject thiz ,jint ledID,jint ledState)
  46. {
  47. ioctl(fd,ledState, ledID);
  48. return 1;
  49. }
  50. jint Java_com_helloworld_helloworld_closeled( JNIEnv* env,
  51. jobject thiz )
  52. {
  53. close(fd);
  54. return 1;
  55. }


APP部分即JAVA

复制代码
  1. package com.helloworld;
  2. import android.app.Activity;
  3. import android.widget.TextView;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.view.View.OnClickListener;
  8. public class helloworld extends Activity {
  9. private Button btnLED1On;
  10. private Button btnLED1Off;
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState)
  14. {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. openled();
  18. btnLED1On = (Button)findViewById(R.id.btnLED1On);
  19. btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
  20. btnLED1On.setOnClickListener(new View.OnClickListener()
  21. {
  22. public void onClick(View v) /**当按钮按下*/
  23. {
  24. setLedState(0,1);
  25. }
  26. }
  27. );
  28. btnLED1Off.setOnClickListener(new View.OnClickListener()
  29. {
  30. public void onClick(View v) /**当按钮按下*/
  31. {
  32. setLedState(0,0);
  33. }
  34. }
  35. );
  36. }
  37. /* A native method that is implemented by the
  38. * 'hello-jni' native library, which is packaged
  39. * with this application.
  40. */
  41. public native String stringFromJNI();
  42. public native int openled();
  43. public native int setLedState(int ledID,int ledState);
  44. public native int closeled();
  45. /* This is another native method declaration that is *not*
  46. * implemented by 'hello-jni'. This is simply to show that
  47. * you can declare as many native methods in your Java code
  48. * as you want, their implementation is searched in the
  49. * currently loaded native libraries only the first time
  50. * you call them.
  51. *
  52. * Trying to call this function will result in a
  53. * java.lang.UnsatisfiedLinkError exception !
  54. */
  55. public native String unimplementedStringFromJNI();
  56. /* this is used to load the 'hello-jni' library on application
  57. * startup. The library has already been unpacked into
  58. * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  59. * installation time by the package manager.
  60. */
  61. static {
  62. System.loadLibrary("hello-jni");
  63. }
  64. }

图片:
图片:
应用程序在这

右边的那个LED程序是我的

程序下载在 http://www.arm9home.net/job.php?action-download-pid-107653-tid-14336-aid-2661.html




http://www.arm9home.net/read.php?tid-14366-fpage-0-toread--page-1.html

Myled.rar(65 K) 下载次数:310
Myled_apk.rar(15 K) 下载次数:207
windows系统上安装与使用Android NDK r5 .rar(8 K) 下载次数:210
Android通过JNI调用驱动程序(完全解析实例)...rar(358 K) 下载次数:363
在主界面

checkbox做的


Myled.java

复制代码
  1. package com.led;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.CheckBox;
  5. import android.widget.CompoundButton;
  6. import com.LedCtr.LedCtr;
  7. public class Myled extends Activity {
  8. /** Called when the activity is first created. */
  9. private CheckBox checkBox1;
  10. private CheckBox checkBox2;
  11. private CheckBox checkBox3;
  12. private CheckBox checkBox4;
  13. LedCtr led;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. led=new LedCtr();
  19. led.openled();
  20. led.setLedState(0,0);
  21. led.setLedState(1,0);
  22. led.setLedState(2,0);
  23. led.setLedState(3,0);
  24. checkBox1=(CheckBox) findViewById(R.id.cb1);
  25. checkBox2=(CheckBox) findViewById(R.id.cb2);
  26. checkBox3=(CheckBox) findViewById(R.id.cb3);
  27. checkBox4=(CheckBox) findViewById(R.id.cb4);
  28. checkBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
  29. @Override
  30. public void onCheckedChanged(CompoundButton buttonView,
  31. boolean isChecked) {
  32. if(checkBox1.isChecked()){
  33. led.setLedState(0,1);
  34. }
  35. else
  36. {
  37. led.setLedState(0,0);
  38. }
  39. }});
  40. checkBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
  41. @Override
  42. public void onCheckedChanged(CompoundButton buttonView,
  43. boolean isChecked) {
  44. if(checkBox2.isChecked()){
  45. led.setLedState(1,1);
  46. }
  47. else
  48. {
  49. led.setLedState(1,0);
  50. }
  51. }});
  52. checkBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
  53. @Override
  54. public void onCheckedChanged(CompoundButton buttonView,
  55. boolean isChecked) {
  56. if(checkBox3.isChecked()){
  57. led.setLedState(2,1);
  58. }
  59. else
  60. {
  61. led.setLedState(2,0);
  62. }
  63. }});
  64. checkBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
  65. @Override
  66. public void onCheckedChanged(CompoundButton buttonView,
  67. boolean isChecked) {
  68. if(checkBox4.isChecked()){
  69. led.setLedState(3,1);
  70. }
  71. else
  72. {
  73. led.setLedState(3,0);
  74. }
  75. }});
  76. }
  77. }


LedCtr.java
复制代码
  1. package com.LedCtr;
  2. public class LedCtr {
  3. public native int openled();
  4. public native int setLedState(int ledID,int ledState);
  5. public native int closeled();
  6. public native StringunimplementedStringFromJNI();
  7. static {
  8. System.loadLibrary("led-jni");
  9. }
  10. }


led-jni
复制代码
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  20. int fd;
  21. /* This is a trivial JNI example where we use a native method
  22. * to return a new VM String. See the corresponding Java source
  23. * file located at:
  24. *
  25. * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  26. */
  27. jstring
  28. Java_com_LedCtr_LedCtr_stringFromJNI( JNIEnv* env,
  29. jobject thiz )
  30. {
  31. return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  32. }
  33. jint Java_com_LedCtr_LedCtr_openled( JNIEnv* env,
  34. jobject thiz )
  35. {
  36. fd = open("/dev/leds0", O_RDONLY);
  37. if (fd < 0)
  38. {
  39. fd = open("/dev/leds", O_RDONLY);
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. jint Java_com_LedCtr_LedCtr_setLedState( JNIEnv* env,
  45. jobject thiz ,jint ledID,jint ledState)
  46. {
  47. ioctl(fd,ledState, ledID);
  48. return 1;
  49. }
  50. jint Java_com_LedCtr_LedCtr_closeled( JNIEnv* env,
  51. jobject thiz )
  52. {
  53. close(fd);
  54. return 1;
  55. }

更多相关文章

  1. android中bitmap和drawable之间的转换
  2. Android中webview和js之间的交互及注意事项
  3. Android(安卓)编程常见问题解答
  4. Android通过Intent发送电子邮件含附件
  5. MediaPlayer实现带播放条的音乐播放和视频播放,可以作为程序参考
  6. Android之Adapter优化
  7. android JNI 使用 for mac
  8. PC 与 Android(安卓)的adb同步通信(一)
  9. 程序代码删除联系人的分组

随机推荐

  1. android技术牛人博客
  2. Android Looper&Handler 源码
  3. android序列化与反序列话HashMap到sqlite
  4. android:设计一个具有3个选项的菜单程序,
  5. 【Android】Suggestion: use tools:overr
  6. SDK无法下载更新的问题
  7. [Android]可自定义显示时长的Toast
  8. Android通过网络URL获取图片并显示
  9. android post方式传递参数并获取返回数据
  10. 解决安卓9.0版本不能访问http请求问题