Android 检测外接USB设备、读取GPIO节点
一.检测外接USB设备、读取GPIO节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.app.Activity; import android.hardware.input.InputManager; import android.os.Bundle; import android.util.Log; import android.view.InputDevice; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); String gpio0 = readNode( "/sys/class/xh_custom/xh_custom_gpio/device/gpio0" ); int gpio = Integer.parseInt(gpio0); } private void detectUsbDeviceWithInputManager() { InputManager im = (InputManager) getSystemService(INPUT_SERVICE); int [] devices = im.getInputDeviceIds(); for ( int id : devices) { InputDevice device = im.getInputDevice(id); Log.d( "gatsby" , "detectUsbDeviceWithInputManager: " + device.getName()); } } private void detectInputDeviceWithShell() { try { Process p = Runtime.getRuntime().exec( "cat /proc/bus/input/devices" ); BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream())); String line = null ; while ((line = in.readLine()) != null ) { String deviceInfo = line.trim(); //对获取的每行的设备信息进行过滤,获得自己想要的。 //if (deviceInfo.contains("Name=")) Log.d( "gatsby" , "detectInputDeviceWithShell: " + deviceInfo); } Log.d( "gatsby" , "-----------------------" ); } catch (Exception e) { e.printStackTrace(); } } private String readNode(String sys_path) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec( "cat " + sys_path); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ( null != (line = br.readLine())) { //Log.d("gatsby", "readNode data ---> " + line); return line; } } catch (IOException e) { e.printStackTrace(); Log.d( "gatsby" , "*** ERROR *** Here is what I know: " + e.getMessage()); } return null ; } } |
1.2.InputManager
1 2 3 4 5 | 10 - 27 11 : 38 : 13.550 2008 2008 D gatsby : detectUsbDeviceWithInputManager: Virtual 10 - 27 11 : 38 : 13.551 2008 2008 D gatsby : detectUsbDeviceWithInputManager: rk29-keypad 10 - 27 11 : 38 : 13.552 2008 2008 D gatsby : detectUsbDeviceWithInputManager: PixArt USB Optical Mouse 10 - 27 11 : 38 : 13.553 2008 2008 D gatsby : detectUsbDeviceWithInputManager: rockchip_headset 10 - 27 11 : 38 : 13.554 2008 2008 D gatsby : detectUsbDeviceWithInputManager: ff420030.pwm |
1.3.cat /proc/bus/input/devices
1 2 3 4 5 6 7 8 9 10 | I: Bus= 0003 Vendor=0d8c Product= 0014 Version= 0100 N: Name= "C-Media Electronics Inc. USB Audio Device" P: Phys=usb-xhci-hcd. 11 .auto- 1 /input3 S: Sysfs=/devices/platform/usb @fe900000 /fe900000.dwc3/xhci-hcd. 11 .auto/usb7/ 7 - 1 / 7 - 1 : 1.3 / 0003 :0D8C: 0014.0003 /input/input5 U: Uniq= H: Handlers=event4 cpufreq keychord B: PROP= 0 B: EV= 13 B: KEY=e000000000000 0 B: MSC= 10 |
二.工作线程更新UI方法
2.1.布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:gravity= "center" android:orientation= "horizontal" tools:context= "com.gatsby.gpiotest.MainActivity" > <LinearLayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" android:gravity= "center" android:layout_weight= "2" > <TextView android:id= "@+id/ie802_gpio1_value" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "GPIO1" android:textSize= "35sp" /> <Button android:id= "@+id/ie802_gpio1_btn" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/red" android:textSize= "35sp" /> </LinearLayout> <LinearLayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" android:gravity= "center" android:layout_weight= "2" > <TextView android:id= "@+id/ie802_gpio2_value" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "GPIO2" android:textSize= "35sp" /> <Button android:id= "@+id/ie802_gpio2_btn" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/red" android:textSize= "35sp" /> </LinearLayout> </LinearLayout> |
2.3.使用handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextView text_gpio1,text_gpio2; String gpio1_Value,gpio2_Value; int gpio1 ,gpio2; Button ie802_gpio1_btn,ie802_gpio2_btn; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { // 更新UI switch (msg.what) { case 1 : gpio1_Value = readNode( "/sys/class/xh_custom/xh_custom_gpio/device/gpio1" ); gpio1= Integer.parseInt(gpio1_Value); break ; case 2 : gpio2_Value = readNode( "/sys/class/xh_custom/xh_custom_gpio/device/gpio2" ); gpio2= Integer.parseInt(gpio2_Value); break ; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_gpio1 = (TextView) findViewById(R.id.ie802_gpio1_value); text_gpio2 = (TextView) findViewById(R.id.ie802_gpio2_value); ie802_gpio1_btn =(Button)findViewById(R.id.ie802_gpio1_btn); ie802_gpio2_btn =(Button)findViewById(R.id.ie802_gpio2_btn); Timer timer = new Timer(); timer.scheduleAtFixedRate( new MyTask( this ), 1 , 100 ); } private class MyTask extends TimerTask { private Activity context; MyTask(Activity context) { this .context = context; } @Override public void run() { // 耗时操作略.... // 更新UI方法 1 Message message1 = new Message(); message1.what = 1 ; mHandler.sendMessage(message1); Message message2 = new Message(); message2.what = 2 ; mHandler.sendMessage(message2); // 更新UI方法 2 mHandler.post(updateThread); // 更新UI方法 3 context.runOnUiThread(updateThread); } } Runnable updateThread = new Runnable() { @Override public void run() { text_gpio1.setText( "距离传感器 GPIO1 Value ->" +gpio1_Value); if (gpio1== 0 ) { ie802_gpio1_btn.setBackgroundResource(R.drawable.green); } else { ie802_gpio1_btn.setBackgroundResource(R.drawable.red); } text_gpio2.setText( "金属感应传感器 GPIO2 Value ->" +gpio2_Value); if (gpio2== 0 ) { ie802_gpio2_btn.setBackgroundResource(R.drawable.green); } else { ie802_gpio2_btn.setBackgroundResource(R.drawable.red); } } }; public static String readNode(String sys_path) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec( "cat " + sys_path); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ( null != (line = br.readLine())) { //Log.d("gatsby", "readNode data ---> " + line); return line; } } catch (IOException e) { e.printStackTrace(); Log.d( "gatsby" , "*** ERROR *** Here is what I know: " + e.getMessage()); } return null ; } } |
分类:
RockChip
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2020-10-27 Android 默认修改壁纸