Android: 查找USB设备

Discovering USB devices in Android

http://gssdaily.com/forum/viewtopic.php?f=17&t=96082

by jack » Sat Sep 29, 2012 10:12 pm

Discovering USB devices in Android

Your application can discover USB devices by either using an intent filter to be notified when the user connects a device or by enumerating USB devices that are already connected. Using an intent filter is useful if you want to be able to have your application automatically detect a desired device. Enumerating connected USB devices is useful if you want to get a list of all connected devices or if your application did not filter for an intent.

Code: Select all
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
... 
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
UsbDevice device = deviceList.get("deviceName");


If desired, you can also just obtain an iterator from the hash map and process each device one by one:

Code: Select all
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
...
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
    UsbDevice device = deviceIterator.next()
    //your code
}

你可能感兴趣的:(Android: 查找USB设备)