-
+
(S9-NZE) Read sensors values of temperature, humidity, radar and adaptive brightness 读取温湿度,光感,雷达传感器值
## 第一步 添加 ShellUtil 工具类 (Step 1 Add the ShellUtil utility class) ```java /** * shell脚本工具类 (Shell script utility class) * * @author longyn 2023/5/15 * @version 1.0.0 */ public class ShellUtil { private static final LruCache<Long, Shell> map = new LruCache<>(30); public static String shell(String cmd) { return shell(cmd,false); } public static String shell(String cmd,boolean readErrorInputStream) { Thread thread = Thread.currentThread(); long threadId = thread.getId(); Shell shell = map.get(threadId); if (shell == null) { shell = new Shell(); map.put(threadId, shell); } return shell.cmd(cmd,readErrorInputStream); } static final class Shell { private final String TAG = ShellUtil.class.getSimpleName(); public String cmd(String cmd,boolean readErrorInputStream) { DataOutputStream dataOutputStream = null; BufferedReader bufferedReader = null; try { Process process = Runtime.getRuntime().exec("su"); OutputStream outputStream = process.getOutputStream(); dataOutputStream = new DataOutputStream(outputStream); dataOutputStream.writeBytes("cd / \n"); dataOutputStream.flush(); dataOutputStream.writeBytes(cmd + "\n"); dataOutputStream.writeBytes("exit \n"); dataOutputStream.flush(); long timeout = 500; long startTime = System.nanoTime(); long rem = TimeUnit.MILLISECONDS.toNanos(timeout); do { try { process.exitValue(); break; } catch (IllegalThreadStateException ex) { if (rem > 0) Thread.sleep( Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100)); } rem = TimeUnit.MILLISECONDS.toNanos(timeout) - (System.nanoTime() - startTime); } while (rem > 0); InputStreamReader inputStreamReader = new InputStreamReader(readErrorInputStream?process.getErrorStream():process.getInputStream()); bufferedReader = new BufferedReader(inputStreamReader); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } return stringBuilder.toString(); } catch (Throwable e) { e.printStackTrace(); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (bufferedReader != null) { bufferedReader.close(); } } catch (Throwable ignored) { } } return null; } } } ``` ## 雷达(RadarUtil) ```java /** * RadarUtil * @author longyn 2024/01/18 * @version 1.0.0 */ public class RadarUtil { private static final class RadarHelperTypeClass { private static final RadarUtil in = new RadarUtil(); } public static RadarUtil getInstance() { return RadarHelperTypeClass.in; } private final String TAG = getClass().getSimpleName(); public RadarUtil() { new Thread(() -> { while (true) { try { String result = ShellUtil.shell("getevent -c 1 /dev/input/event4"); if (result == null || TextUtils.isEmpty(result)) { continue; } String[] s = result.split(" "); if (s.length < 2) { continue; } //雷达靠近 远离值 (Radar Approach: Away from the value) int code = Integer.parseInt(s[1], 16); Log.d(TAG,"code:"+code); } catch (Throwable e) { e.printStackTrace(); } } },"RadarHelperThread").start(); } } ``` ## 温湿度(Temperature and humidity) ```java //获取温度(Get the temperature) public float getTemp() { String temp = ShellUtil.shell("cat /sys/devices/platform/twi.1/i2c-1/1-0040/temp1_input"); try { if (TextUtils.isEmpty(temp)) { return 0f; } return Math.round(Float.parseFloat(temp) / 1000f); } catch (Throwable e) { e.printStackTrace(); } return 0f; } //获取湿度 (Get humidity) public float getHumidity() { String humidity = ShellUtil.shell("cat /sys/devices/platform/twi.1/i2c-1/1-0040/humidity1_input"); try { if (TextUtils.isEmpty(humidity)) { return 0f; } return Math.round(Float.parseFloat(humidity) / 1000f); } catch (Throwable e) { e.printStackTrace(); } return 0f; } ``` ## 亮度传感器 Light sensor (adaptive brightness) ```java SensorManager sensorManager = (SensorManager) getApplicationContext() .getSystemService(Context.SENSOR_SERVICE); sensorManager.registerListener(new LightSensorEventListener(), sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL); ``` ```java public class LightSensorEventListener implements SensorEventListener { private final String TAG = this.getClass().getSimpleName(); @Override public void onSensorChanged(SensorEvent event) { try { if (event == null) return; if (event.values == null) return; if (event.values.length < 1) { return; } float value = event.values[0]; if (event.sensor.getType() == Sensor.TYPE_LIGHT) { //if (value <= 10f) low //else (value > 10f) hight } } catch (Throwable e) { e.printStackTrace(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } ```
龙永宁
2024年5月30日 10:59
手机扫码
复制链接
手机扫一扫转发分享
复制链接