android 監測紅外/gpio

        //紅外
        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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章