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;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值