android led_misc驱动 + 测试应用程序(ndk-build)


1. 新建一个project目录ledapp 在ledapp下建立一个 jni 目录。如果要使用 NDK 自带的 Android.mk,必须叫做这个名字。

把源码放到这个目录。这里程序的名字是 led_misc_test.c,那么目录结构为 ledapp/jni/led_misc_test.c

 //led_misc_test.c
  1 #include<unistd.h>
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<sys/ioctl.h>
  5 
  6 int main(int argc, char **argv)
  7 {
  8     int fd;
  9     unsigned int state;
 10 
 11     if(argc != 2 || sscanf(argv[1],"%d",&state) != 1 || state > 1 ){
 12         printf("Useage: xxx #./filename 1|0 \n");
 13         exit(0);
 14     }
 15 
 16     fd = open("/dev/leds",0);
 17     if(fd < 0){
 18         printf("error to open /dev/leds\n");
 19         exit(0);
 20     }
 21 
 22     ioctl(fd,state,NULL);
 23     close(fd);
 24     return 0;
 25 }

2. 从 android-ndk-r6b 的 sample 里复制一份 Android.mk 到 jni 目录。因为我们编译的是 Native App 可执行程序而不是 JNI lib,所以需要修改 Android.mk,把 include $(BUILD_SHARED_LIBRARY ) 改成 include $(BUILD_EXECUTABLE ).

这里给出示例的 Android.mk 文件内容:

  1 # Copyright (C) 2009 The Android Open Source Project
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #      http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
 10 # distributed under the License is distributed on an "AS IS" BASIS,
 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 # See the License for the specific language governing permissions and
 13 # limitations under the License.
 14 #
 15 LOCAL_PATH := $(call my-dir)
 16 
 17 include $(CLEAR_VARS)
 18 
 19 LOCAL_MODULE    := led_misc_test
 20 LOCAL_SRC_FILES := led_misc_test.c
 21 
 22 include $(BUILD_EXECUTABLE)                                 

3. 在 ledapp目录中运行 ndk-build (如果没有添加环境变量则要运行)/home/android/android-ndk-r6b/ndk-build. 如果编译没有错误,那么输出为:


那么,我们需要的可执行文件就在./libs/armeabi 目录下了。可以用 adb push上传 或 u盘拷贝 到手机(开发板)或者模拟器中运行测试。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值