下面列出了android.hardware.usb.UsbDevice#getDeviceName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (UsbStorageProvider.ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (permission) {
discoverDevice(usbDevice);
notifyRootsChanged();
notifyDocumentsChanged(getContext(), getRootId(usbDevice)+ROOT_SEPERATOR);
} else {
// so we don't ask for permission again
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)
|| UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateRoots();
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (UsbStorageProvider.ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (permission) {
discoverDevice(usbDevice);
notifyRootsChanged();
notifyDocumentsChanged(getContext(), getRootId(usbDevice)+ROOT_SEPERATOR);
} else {
// so we don't ask for permission again
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)
|| UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateRoots();
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (UsbStorageProvider.ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (permission) {
discoverDevice(usbDevice);
notifyRootsChanged();
notifyDocumentsChanged(getContext(), getRootId(usbDevice)+ROOT_SEPERATOR);
} else {
// so we don't ask for permission again
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)
|| UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateRoots();
}
}
/**
* Gets a summary of the current select product and vendor ids
*/
private String getSelectedDeviceSummary() {
int productId = sharedPreferences.getInt(
USBGpsProviderService.PREF_GPS_DEVICE_PRODUCT_ID, DEFAULT_GPS_PRODUCT_ID);
int vendorId = sharedPreferences.getInt(
USBGpsProviderService.PREF_GPS_DEVICE_VENDOR_ID, DEFAULT_GPS_VENDOR_ID);
String deviceDisplayedName = "Device not connected - " + vendorId + ": " + productId;
for (UsbDevice usbDevice: usbManager.getDeviceList().values()) {
if (usbDevice.getVendorId() == vendorId && usbDevice.getProductId() == productId) {
deviceDisplayedName =
"USB " + usbDevice.getDeviceProtocol() + " " + usbDevice.getDeviceName() +
" | " + vendorId + ": " + productId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
deviceDisplayedName = usbDevice.getManufacturerName() + usbDevice.getProductName() +
" | " + vendorId + ": " + productId;
}
break;
}
}
return deviceDisplayedName;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
false);
Log.d(LOG_TAG, "ACTION_USB_PERMISSION: " + permission + " Device: " + deviceName);
// sync with connect
gotRights.add(permission);
}
}
/**
* this class needs permission to access USB device before constructing
* @param monitor
* @param device
*/
private UsbControlBlock(final USBMonitor monitor, final UsbDevice device) {
if (DEBUG) Log.i(TAG, "UsbControlBlock:constructor");
mWeakMonitor = new WeakReference<USBMonitor>(monitor);
mWeakDevice = new WeakReference<UsbDevice>(device);
mConnection = monitor.mUsbManager.openDevice(device);
mInfo = updateDeviceInfo(monitor.mUsbManager, device, null);
final String name = device.getDeviceName();
final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null;
int busnum = 0;
int devnum = 0;
if (v != null) {
busnum = Integer.parseInt(v[v.length-2]);
devnum = Integer.parseInt(v[v.length-1]);
}
mBusNum = busnum;
mDevNum = devnum;
// if (DEBUG) {
if (mConnection != null) {
final int desc = mConnection.getFileDescriptor();
final byte[] rawDesc = mConnection.getRawDescriptors();
Log.i(TAG, String.format(Locale.US, "name=%s,desc=%d,busnum=%d,devnum=%d,rawDesc=", name, desc, busnum, devnum) + rawDesc);
} else {
Log.e(TAG, "could not connect to device " + name);
}
// }
}
/**
* 指定したUsbDeviceに関係づけたUsbControlBlockインスタンスを生成する
* 内部でopenDeviceをするのでパーミションを取得してないとダメ
* @param monitor
* @param device
*/
private UsbControlBlock(final USBMonitor monitor, final UsbDevice device)
throws IOException {
// if (DEBUG) Log.v(TAG, "UsbControlBlock:device=" + device);
mWeakMonitor = new WeakReference<USBMonitor>(monitor);
mWeakDevice = new WeakReference<UsbDevice>(device);
// XXX UsbManager#openDeviceはIllegalArgumentExceptionを投げる可能性がある
try {
mConnection = monitor.mUsbManager.openDevice(device);
} catch (final Exception e) {
throw new IOException(e);
}
final String name = device.getDeviceName();
if (mConnection != null) {
final int fd = mConnection.getFileDescriptor();
final byte[] rawDesc = mConnection.getRawDescriptors();
Log.i(TAG, String.format(Locale.US,
"name=%s,fd=%d,rawDesc=", name, fd)
+ BufferHelper.toHexString(rawDesc, 0, 16));
} else {
// 多分ここには来ない(openDeviceの時点でIOException)けど年のために
throw new IOException("could not connect to device " + name);
}
mInfo = UsbDeviceInfo.getDeviceInfo(monitor.mUsbManager, device, null);
monitor.processConnect(device, this);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
false);
Log.d(LOG_TAG, "ACTION_USB_PERMISSION: " + permission + " Device: " + deviceName);
// sync with connect
gotRights.add(permission);
}
}
/**
* this class needs permission to access USB device before constructing
* @param monitor
* @param device
*/
private UsbControlBlock(final USBMonitor monitor, final UsbDevice device) {
if (DEBUG) Log.i(TAG, "UsbControlBlock:constructor");
mWeakMonitor = new WeakReference<USBMonitor>(monitor);
mWeakDevice = new WeakReference<UsbDevice>(device);
mConnection = monitor.mUsbManager.openDevice(device);
mInfo = updateDeviceInfo(monitor.mUsbManager, device, null);
final String name = device.getDeviceName();
final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null;
int busnum = 0;
int devnum = 0;
if (v != null) {
busnum = Integer.parseInt(v[v.length-2]);
devnum = Integer.parseInt(v[v.length-1]);
}
mBusNum = busnum;
mDevNum = devnum;
// if (DEBUG) {
if (mConnection != null) {
final int desc = mConnection.getFileDescriptor();
final byte[] rawDesc = mConnection.getRawDescriptors();
Log.i(TAG, String.format(Locale.US, "name=%s,desc=%d,busnum=%d,devnum=%d,rawDesc=", name, desc, busnum, devnum) + rawDesc);
} else {
Log.e(TAG, "could not connect to device " + name);
}
// }
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
false);
Log.d(LOG_TAG, "ACTION_USB_PERMISSION: " + permission + " Device: " + deviceName);
// sync with connect
gotRights.add(permission);
}
}
public NoPermissionsException(UsbDevice usbDevice) {
// with L+ devices we can get more verbal device name
super("No permission granted to communicate with device " + (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP ? usbDevice.getProductName() : usbDevice.getDeviceName()));
}
public static String getPath(UsbDevice device){
return device.getDeviceName();
}
public static String getPath(UsbDevice device){
return device.getDeviceName();
}
public static String getPath(UsbDevice device){
return device.getDeviceName();
}
/**
* get device name
* @return
*/
public String getDeviceName() {
final UsbDevice device = mWeakDevice.get();
return device != null ? device.getDeviceName() : "";
}
private UsbDeviceInfo getInfoForDevice(UsbDevice dev, UsbDeviceConnection devConn) {
UsbDeviceInfo info = new UsbDeviceInfo();
UsbIpDevice ipDev = new UsbIpDevice();
ipDev.path = dev.getDeviceName();
ipDev.busnum = deviceIdToBusNum(dev.getDeviceId());
ipDev.devnum = deviceIdToDevNum(dev.getDeviceId());
ipDev.busid = String.format("%d-%d", ipDev.busnum, ipDev.devnum);
ipDev.idVendor = (short) dev.getVendorId();
ipDev.idProduct = (short) dev.getProductId();
ipDev.bcdDevice = -1;
ipDev.bDeviceClass = (byte) dev.getDeviceClass();
ipDev.bDeviceSubClass = (byte) dev.getDeviceSubclass();
ipDev.bDeviceProtocol = (byte) dev.getDeviceProtocol();
ipDev.bConfigurationValue = 0;
ipDev.bNumConfigurations = 1;
ipDev.bNumInterfaces = (byte) dev.getInterfaceCount();
info.dev = ipDev;
info.interfaces = new UsbIpInterface[ipDev.bNumInterfaces];
for (int i = 0; i < ipDev.bNumInterfaces; i++) {
info.interfaces[i] = new UsbIpInterface();
UsbInterface iface = dev.getInterface(i);
info.interfaces[i].bInterfaceClass = (byte) iface.getInterfaceClass();
info.interfaces[i].bInterfaceSubClass = (byte) iface.getInterfaceSubclass();
info.interfaces[i].bInterfaceProtocol = (byte) iface.getInterfaceProtocol();
}
AttachedDeviceContext context = connections.get(dev.getDeviceId());
UsbDeviceDescriptor devDesc = null;
if (context != null) {
// Since we're attached already, we can directly query the USB descriptors
// to fill some information that Android's USB API doesn't expose
devDesc = UsbControlHelper.readDeviceDescriptor(context.devConn);
ipDev.bcdDevice = devDesc.bcdDevice;
ipDev.bNumConfigurations = devDesc.bNumConfigurations;
}
ipDev.speed = detectSpeed(dev, devDesc);
return info;
}
public String getDeviceName() {
final UsbDevice device = mWeakDevice.get();
return device != null ? device.getDeviceName() : "";
}
@Override
public CharSequence onBuildingDevicesList(UsbDevice usbDevice) {
return "devID:" + usbDevice.getDeviceId() + " VID:" + Integer.toHexString(usbDevice.getVendorId()) + " PID:" + Integer.toHexString(usbDevice.getProductId()) + " " + usbDevice.getDeviceName();
}
/**
* get device name
* @return
*/
public String getDeviceName() {
final UsbDevice device = mWeakDevice.get();
return device != null ? device.getDeviceName() : "";
}
private ArrayList<DeviceEntry> enumerateUsbDevices() {
ArrayList<DeviceEntry> validStreamingDeviceList = new ArrayList<>(5);
synchronized (JsonManager.lock) {
//Make sure the JsonManger has been initialized
if (!JsonManager.isInitialized())
JsonManager.initialize();
// Enumerate a list of currently connected Usb devices so we can pick out which
// ones are supported easycap devices
UsbManager mUsbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> usbDeviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = usbDeviceList.values().iterator();
// Make sure the device list is empty before enumeration
validStreamingDeviceList.clear();
while(deviceIterator.hasNext()) {
UsbDevice uDevice = deviceIterator.next();
DeviceInfo devInfo = JsonManager.getDevice(uDevice.getVendorId(), uDevice.getProductId(),
DeviceInfo.DeviceStandard.NTSC);
// If a supported device is listed in json list, request permission
// to access it
if (devInfo != null) {
Log.i(TAG, "Supported usb device found: " + uDevice.toString());
Log.i(TAG, "Device ID: " + uDevice.getDeviceId());
Log.i(TAG, "Device Name: " + uDevice.getDeviceName());
Log.i(TAG, "Vendor: ID " + uDevice.getVendorId());
Log.i(TAG, "Product ID: " + uDevice.getProductId());
DeviceEntry devEntry = new DeviceEntry();
devEntry.deviceDescription = devInfo.getDescription() + " @ " + uDevice.getDeviceName();
devEntry.deviceName = uDevice.getDeviceName() + ":" + devInfo.getVendorID()
+ ":" + devInfo.getProductID();
validStreamingDeviceList.add(devEntry);
}
}
}
return validStreamingDeviceList;
}