硬件访问服务AIDL JNI 方式

通过AIDL实现硬件访问系统服务

AIDL 可以在Android中实现进程间通信 IPC

在android 5 源码上 添加一个 Led 控制的小例子

一个简单的例子LedService

新建ILedService.aidl  

package android.os;

interface ILedService{
     
     int ledCtl(int which,int status);   
    
}

放在 源码路径下

android-5.0.2/frameworks/base/core/java/android/os/ILedService.aidl

修改 Android.mk文件  在 aidl 文件处添加一条 

core/java/android/os/ILedService.aidl

使用mmm 命令生成  ILedService.java

使用mmm 命令 的前提是 需要编译好android 源码 并且  设置好环境变量

执行如下命令设置

. setenv

lunch 选择开发板类型

成功后会生成在

./target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/ILedService.java

创建 LedService.java 实现ILedService.Stub接口

package    com.android.server;
import    android.os.ILedService;

//./frameworks/base/services/java/com/android/server/SystemServer.java

public LedService extends ILedService.Stub{
    
    private static final String TAG="LedService";
    
    /*调用本地方法的C函数*/
    public int ledCtrl(int which, int status) throws android.os.RemoteException{
        return native_ledCtrl(which,status);
    }

    public LedService(){
        native_ledOpen();
    }

    public static native int native_ledCtrl(int which ,int status);
    public static native int native_ledOpen();
    public static native void native_ledClose();

}

修改  SystemServer.java   添加  ServiceManager.addService("led", (IBinder)new LedService());

实现  com_android_service_LedService.cpp 应将操作函数  jni

修改 onload.cpp

//声明
int register_android_server_LedService(JNIEnv* env);
//注册
register_android_server_LedService(env);

重新编译 mmm frameworks/base/services

生成镜像文件  make snod

  

原文地址:https://www.cnblogs.com/qingducx/p/5189046.html