當(dāng)前位置:首頁 > IT技術(shù) > 移動平臺 > 正文

Android 檢測外接USB設(shè)備、讀取GPIO節(jié)點(diǎn)
2021-10-27 14:34:00

一.檢測外接USB設(shè)備、讀取GPIO節(jié)點(diǎn)

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();
                //對獲取的每行的設(shè)備信息進(jìn)行過濾,獲得自己想要的。
                //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("金屬感應(yīng)傳感器 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;
	}
}

  

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >