//红外
new Thread(new Runnable() {
@Override
public void run() {
while (true){
checkGPIO83();
SystemClock.sleep(1000);
}
}
}).start();
private void checkGPIO83() {
File status_file = new File("/sys/class/gpio/gpio83/value");
try {
status_reader = new BufferedReader(new InputStreamReader(new FileInputStream(status_file), "UTF-8"));
String status = status_reader.readLine();
if (status.equals("0")) {
Tools.writeStrToFile("0", "", "/sys/class/gpio/gpio20/value", false);
} else if (status.equals("1")) {
Tools.writeStrToFile("1", "", "/sys/class/gpio/gpio20/value", false);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 写数据到文件
* @param str 写入的字符串
* @param name 文件名
* @param path 文件路径
* @param supplements 是否补充添加
* @return
* @throws IOException
*/
public static Boolean writeStrToFile(String str, String name, String path, Boolean supplements) {
String old_str;
File file_path = new File(path);
if (!file_path.exists()) {
file_path.mkdirs();
}
File file = new File(path+name);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
return false;
}
}
try {
FileWriter fileWritter = new FileWriter(file);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
if(supplements){
String old_data;
try {
old_data = getStrFromFile(name,path);
if(TextUtils.isEmpty(old_data)){
old_str = old_data;
}else{
old_str = "";
}
str = old_str + str;
} catch (JSONException e) {
bufferWritter.close();
return false;
}
}
bufferWritter.write(str);
bufferWritter.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
* 读取文件值
* @param name
* @param path
* @return
* @throws JSONException
*/
public static String getStrFromFile(String name, String path) throws JSONException{
String result = "";
File file = new File(path+name);
if (!file.exists()) {
result = "0";
}else{
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader br = new BufferedReader(isr);
String str = "";
String mimeTypeLine = null ;
while ((mimeTypeLine = br.readLine()) != null) {
str = str+mimeTypeLine;
}
br.close();
result = str;
} catch (Exception e) {
result = "0";
}
}
return result;
}