Android USB 设备如何区分是打印机还是U盘

项目中需要区分USB的连接设备是打印机还是电子秤,然后上网搜索到了一篇博客,根据这个博客写的一个小的例子:非常感谢hao2014_的这篇博客http://blog.csdn.net/u013686019/article/details/50409421


MainActivity:

package com.example.administrator.usbmanagertest;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.sb);
    }

    public void showUsbList(View view) {
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()){
            UsbDevice usbDevice = deviceIterator.next();
            sb.append("DeviceName="+usbDevice.getDeviceName()+"\n");
            sb.append("DeviceId="+usbDevice.getDeviceId()+"\n");
            sb.append("VendorId="+usbDevice.getVendorId()+"\n");
            sb.append("ProductId="+usbDevice.getProductId()+"\n");
            sb.append("DeviceClass="+usbDevice.getDeviceClass()+"\n");
            int deviceClass = usbDevice.getDeviceClass();
            if(deviceClass==0) {
                UsbInterface anInterface = usbDevice.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();

                sb.append("device Class 为0-------------\n");
                sb.append("Interface.describeContents()="+anInterface.describeContents()+"\n");
                sb.append("Interface.getEndpointCount()="+anInterface.getEndpointCount()+"\n");
                sb.append("Interface.getId()="+anInterface.getId()+"\n");
                //http://blog.csdn.net/u013686019/article/details/50409421
                //http://www.usb.org/developers/defined_class/#BaseClassFFh
                //通过下面的InterfaceClass 来判断到底是哪一种的,例如7就是打印机,8就是usb的U盘
                sb.append("Interface.getInterfaceClass()="+anInterface.getInterfaceClass()+"\n");
                if(anInterface.getInterfaceClass()==7){
                    sb.append("此设备是打印机\n");
                }else if(anInterface.getInterfaceClass()==8){
                    sb.append("此设备是U盘\n");
                }
                sb.append("anInterface.getInterfaceProtocol()="+anInterface.getInterfaceProtocol()+"\n");
                sb.append("anInterface.getInterfaceSubclass()="+anInterface.getInterfaceSubclass()+"\n");
                sb.append("device Class 为0------end-------\n");
            }

            sb.append("DeviceProtocol="+usbDevice.getDeviceProtocol()+"\n");
            sb.append("DeviceSubclass="+usbDevice.getDeviceSubclass()+"\n");
            sb.append("+++++++++++++++++++++++++++\n");
            sb.append("                           \n");
        }

        textView.setText(sb);
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.usbmanagertest.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showUsbList"
        android:text="Show USB List" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:textSize="25sp"
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


参考博客地址:


http://blog.csdn.net/u013686019/article/details/50409421

一、问题

现在有USB设备插入Android系统,那么如何得知插入的设备类型?是USB打印机,U盘,还是USB鼠标


二、USB类型规定

对于USB类型,USB组织是有规定的,见:http://www.usb.org/

比如:

Base Class

Descriptor Usage

Description

00h

Device

Use class information in the Interface Descriptors

01h

Interface

Audio 

02h

Both

Communications and CDC Control

03h

Interface

HID (Human Interface Device)

05h

Interface

Physical

06h

Interface

Image

07h

Interface

Printer

三、Linux对USB设备类型定义

在kernel中,有两个结构体的相关成员表征USB设备的类型,第一个usb_device_descriptor

[cpp]  view plain   copy
  print ?
  1. include/uapi/linux/usb/ch9.h  
  2. /* USB_DT_DEVICE: Device descriptor */  
  3. struct usb_device_descriptor {  
  4.     __u8  bDeviceClass;  
  5.     __u8  bDeviceSubClass;  
  6.     __u8  bDeviceProtocol;  
  7. } __attribute__ ((packed));  
bDeviceClass成员值区别不同的USB设备,如果该值为0呢?看上边:

Base Class

Descriptor Usage

Description

00h

Device

Use class information in the Interface Descriptors

就在第二个结构体usb_interface_descriptor中:

[cpp]  view plain   copy
  print ?
  1. include/uapi/linux/usb/ch9.h  
  2. struct usb_interface_descriptor {  
  3.     __u8  bInterfaceClass;  
  4.     __u8  bInterfaceSubClass;  
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值