下面是LED控制代码,其原理是LED驱动在/proc/driver/目录下生成一个led设备文件(具体看linux环境下的驱动开发),然后写这个文件即可控制LED的亮灭以及闪烁亮、闪烁灭。表示LED的亮灭以及闪烁亮、闪烁灭的值由其LED驱动开发者指定。
package com.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.InterruptedException;
import java.io.File;
import java.io.OutputStreamWriter;
import android.util.Log;
public class Led {
static void ControlLed(String str)
{
try {
Log.d("zxb", str);
Thread.sleep(200);
FileOutputStream fos = new FileOutputStream("/proc/driver/led");
fos.write(str.getBytes());
fos.close();
/*
File file = new File("/proc/driver/led");
FileOutputStream fos;
fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(str);
osw.flush();
fos.close();
osw.close();
*/
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void LedOn()
{
ControlLed("0");
}
public static void LedOff()
{
ControlLed("1");
}
public static void LedFlashOn()
{
ControlLed("2");
}
public static void LedFlashOff()
{
ControlLed("3");
}
}