• usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备


    在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion=“12”。

    没有外设的情况下,结果如下图所示:


    通过OTG线插入读卡器设备后,结果如下:


    为了指定应用程序以usb-host模式运行,在AndroidManifest.xml文件中需要添加<uses-feature android:name="android.hardware.usb.host">并且修改安卓的minSDKVersion="12"

    主要代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.androidusbhost"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-feature android:name="android.hardware.usb.host" />
        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.androidusbhost.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    主要的java代码如下:
    package com.example.androidusbhost;
    
    import java.util.HashMap;
    import java.util.Iterator;
    
    import android.hardware.usb.UsbConstants;
    import android.hardware.usb.UsbDevice;
    import android.hardware.usb.UsbManager;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.app.Activity;
    import android.content.Context;
    
    public class MainActivity extends Activity {
    
     Button btnCheck;
     TextView textInfo;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btnCheck = (Button) findViewById(R.id.check);
      textInfo = (TextView) findViewById(R.id.info);
      btnCheck.setOnClickListener(new OnClickListener() {
    
       @Override
       public void onClick(View arg0) {
        checkInfo();
       }
      });
     }
    
     private void checkInfo() {
      UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
      HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
      Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
      
      String i = "";
      while (deviceIterator.hasNext()) {
       UsbDevice device = deviceIterator.next();
       i += "
    " +
        "DeviceID: " + device.getDeviceId() + "
    " +
        "DeviceName: " + device.getDeviceName() + "
    " +
        "DeviceClass: " + device.getDeviceClass() + " - " 
         + translateDeviceClass(device.getDeviceClass()) + "
    " +
        "DeviceSubClass: " + device.getDeviceSubclass() + "
    " +
        "VendorID: " + device.getVendorId() + "
    " +
        "ProductID: " + device.getProductId() + "
    ";
      }
      
      textInfo.setText(i);
     }
     
     private String translateDeviceClass(int deviceClass){
      switch(deviceClass){
      case UsbConstants.USB_CLASS_APP_SPEC: 
       return "Application specific USB class";
      case UsbConstants.USB_CLASS_AUDIO: 
       return "USB class for audio devices";
      case UsbConstants.USB_CLASS_CDC_DATA: 
       return "USB class for CDC devices (communications device class)";
      case UsbConstants.USB_CLASS_COMM: 
       return "USB class for communication devices";
      case UsbConstants.USB_CLASS_CONTENT_SEC: 
       return "USB class for content security devices";
      case UsbConstants.USB_CLASS_CSCID: 
       return "USB class for content smart card devices";
      case UsbConstants.USB_CLASS_HID: 
       return "USB class for human interface devices (for example, mice and keyboards)";
      case UsbConstants.USB_CLASS_HUB: 
       return "USB class for USB hubs";
      case UsbConstants.USB_CLASS_MASS_STORAGE: 
       return "USB class for mass storage devices";
      case UsbConstants.USB_CLASS_MISC: 
       return "USB class for wireless miscellaneous devices";
      case UsbConstants.USB_CLASS_PER_INTERFACE: 
       return "USB class indicating that the class is determined on a per-interface basis";
      case UsbConstants.USB_CLASS_PHYSICA: 
       return "USB class for physical devices";
      case UsbConstants.USB_CLASS_PRINTER: 
       return "USB class for printers";
      case UsbConstants.USB_CLASS_STILL_IMAGE: 
       return "USB class for still image devices (digital cameras)";
      case UsbConstants.USB_CLASS_VENDOR_SPEC: 
       return "Vendor specific USB class";
      case UsbConstants.USB_CLASS_VIDEO: 
       return "USB class for video devices";
      case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER: 
       return "USB class for wireless controller devices";
      default: return "Unknown USB class!";
      
      }
     }
    }
    布局文件主要代码:
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="web"
            android:text="http://android-er.blogspot.com/"
            android:textStyle="bold" />
        <Button
            android:id="@+id/check"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Check USB devices" />
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>


  • 相关阅读:
    解决nodejs使用yarn安装vue-cli提示'vue' 不是内部或外部命令
    转载——java synchronized详解
    转载——Java中this关键字和super关键字的使用
    转载—— JUnit单元测试教程(翻译自Java Code Geeks)
    Usage and Idioms——Use with Maven
    转载——JUnit 5 新特性
    Usage and Idioms——Categories
    转载——单元测试利器 JUnit 4
    Usage and Idioms——Test Fixtures
    深入JUnit源码之Rule
  • 原文地址:https://www.cnblogs.com/sowhat4999/p/4439873.html
Copyright © 2020-2023  润新知