通过注册广播的方式监听USB设备 。
广播Action
- UsbManager.ACTION_USB_DEVICE_ATTACHED
Usb设备插入或者挂载事件通知 - UsbManager.ACTION_USB_DEVICE_DETACHED
Usb设备拔出或者卸载时间通知
定义广播
private final BroadcastReceiver mUsbMonitorReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
int vendorId = device.getVendorId();
int productId = device.getProductId();
} else