activity_main.xml
MainActivity.java
package com.raycloud.wolf.usbmanagertest;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.hardware.usb.UsbConstants;import android.hardware.usb.UsbDevice;import android.hardware.usb.UsbManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;public class MainActivity extends AppCompatActivity { private TextView msgTextView; private UsbManager usbManager; private HashSetprinterSet = new HashSet(); private BroadcastReceiver usbBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { Toast.makeText(context, "接入了一个USB设备", Toast.LENGTH_SHORT).show(); retrievePrinter(); } else if (action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { Toast.makeText(context, "移除了一个USB设备", Toast.LENGTH_SHORT).show(); retrievePrinter(); } else { if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) Toast.makeText(context, "已授予打印机USB权限", Toast.LENGTH_SHORT).show(); else Toast.makeText(context, "未授予打印机USB权限", Toast.LENGTH_SHORT).show(); } } }; // private final String tag = "MainActivity"; private static final String ACTION_USB_PERMISSION_GRANTED = "com.raycloud.wolf.usbmanagertest.action.USB_PERMISSION_GRANTED"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); msgTextView = findViewById(R.id.msgTextView); findViewById(R.id.retrievePrinterButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { retrievePrinter(); } }); findViewById(R.id.requestPermissionButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Iterator iterator = printerSet.iterator(); while (iterator.hasNext()) { UsbDevice usbDevice = iterator.next(); if (!usbManager.hasPermission(usbDevice)) usbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(ACTION_USB_PERMISSION_GRANTED), 0)); Toast.makeText(MainActivity.this, "打印机已具备USB权限", Toast.LENGTH_SHORT).show(); } } }); usbManager = (UsbManager) getSystemService(USB_SERVICE); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); intentFilter.addAction(ACTION_USB_PERMISSION_GRANTED); registerReceiver(usbBroadcastReceiver, intentFilter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(usbBroadcastReceiver); } private void retrievePrinter() { HashMap deviceList = usbManager.getDeviceList(); Iterator iterator = deviceList.values().iterator(); int numberOfPrinters = 0; while (iterator.hasNext()) { UsbDevice usbDevice = iterator.next(); int interfaceCount = usbDevice.getInterfaceCount(); for (int i = 0; i != interfaceCount; ++i) { if (usbDevice.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER) { ++numberOfPrinters; printerSet.add(usbDevice); } } } msgTextView.setText(String.format("检索到%d台打印机", numberOfPrinters)); }}