在Android的HAL层和JNI层添加led的接口,包含以下一些步骤: 1. 在HAL层添加Light的接口 a. 定义LED的文件节点路径: char const * const TORCH_FILE = "/sys/class/leds/orange:ph21:led2/brightness"; b. 定义控制LED的接口: static int set_light_torch(struct light_device_t* dev, struct light_state_t const* state)
{
int err = 0;
pthread_mutex_lock(&g_lock);
err = write_int(TORCH_FILE, state->color & 0xFF);
pthread_mutex_unlock(&g_lock);
return err;
} c. 定义对应的Light ID: 在Lights.h中添加 #define LIGHT_ID_TORCH "torch" d. 在open_lights函数里面添加如下代码: else if (0 == strcmp(LIGHT_ID_TORCH, name))
set_light = set_light_torch; 2. 在JNI层添加相应的接口 a. 在com_android_server_LightsService.cpp文件的init_native( )函数中添加led的Light结构体: devices->lights[LIGHT_INDEX_TORCH] = get_device(module, LIGHT_ID_TORCH); b. 在Light类型的enum变量中添加led的类型索引 LIGHT_INDEX_TORCH = 8, c. 在LightService.java文件中添加led Light的Id public static final int LIGHT_ID_TORCH = 8; 3. 在Java服务层添加LightServices的接口 a. 在PowerManagerService.java文件中添加相应的接口,具体如下: 在PowerManagerService的类定义中增加 led Light的对象 private LightsService.Light mTorchLight; 在systemReady( )接口中创建led Light的实例 mTorchLight = mLightsService.getLight(LightsService.LIGHT_ID_TORCH); 在PowerManagerService的类定义中添加调用led Light的接口 @Override // Binder call
public void setTorchLight(int brightness) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
final long ident = Binder.clearCallingIdentity();
try {
setTorchLightBrightness(brightness);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
public void setTorchLightBrightness(int brightness) {
LightsService.Light light;
synchronized (mLock) {
if (!mSystemReady) {
return;
}
light = mTorchLight;
}
// Control light outside of lock.
light.setBrightness(brightness);
} b. 在PowerManager.java文件里面定义 供应用层调用的led API: public void setTorchBrightness(int brightness) {
try {
mService.setTorchLight(brightness);
} catch (RemoteException e) {
}
} c. 在IPowerManager.aidl文件中添加led的接口: void setTorchLight(int brightness); d. 在BridgePowerManager.java文件中添加接口: @Override
public void setTorchLight(int arg0) throws RemoteException {
// pass for now.
} 至此,从HAL到JNI的所有接口都已经添加完毕,应用层可以通过LightService来获取led的Light对象,然后对手led进行操作。
转载:Android绕开Camera打开手电筒的LED的实现方法另外还可参考:android平台led开发之内核硬件驱动层
|