zoukankan      html  css  js  c++  java
  • Android 检测外接USB设备、读取GPIO节点

    一.检测外接USB设备、读取GPIO节点

    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

    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

    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.布局

    <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

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

      

  • 相关阅读:
    通过网格拆分高德地图
    vue-router重定向 不刷新问题
    vue-scroller记录滚动位置
    鼠标滚轮更改transform的值(vue-scroller在PC端的上下滑动)
    position sticky的兼容
    js截图及绕过服务器图片保存至本地(html2canvas)
    禁止页面回退到某个页面(如避免登录成功的用户返回到登录页)
    手动创建script解决跨域问题(jsonp从入门到放弃)
    逻辑回归的常见面试点总结
    听说你不会调参?TextCNN的优化经验Tricks汇总
  • 原文地址:https://www.cnblogs.com/crushgirl/p/15470266.html
Copyright © 2011-2022 走看看