Java Code Examples for android.hardware.usb.UsbDeviceConnection#getSerial()

The following examples show how to use android.hardware.usb.UsbDeviceConnection#getSerial() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: USBMonitor.java    From AndroidUSBCamera with Apache License 2.0 4 votes vote down vote up
/**
 * ベンダー名・製品名・バージョン・シリアルを取得する
 * @param manager
 * @param device
 * @param _info
 * @return
 */
@TargetApi(Build.VERSION_CODES.M)
public static UsbDeviceInfo updateDeviceInfo(final UsbManager manager, final UsbDevice device, final UsbDeviceInfo _info) {
	final UsbDeviceInfo info = _info != null ? _info : new UsbDeviceInfo();
	info.clear();

	if (device != null) {
		if (BuildCheck.isLollipop()) {
			info.manufacturer = device.getManufacturerName();
			info.product = device.getProductName();
			info.serial = device.getSerialNumber();
		}
		if (BuildCheck.isMarshmallow()) {
			info.usb_version = device.getVersion();
		}
		if ((manager != null) && manager.hasPermission(device)) {
			final UsbDeviceConnection connection = manager.openDevice(device);
			if(connection == null) {
				return null;
			}
			final byte[] desc = connection.getRawDescriptors();

			if (TextUtils.isEmpty(info.usb_version)) {
				info.usb_version = String.format("%x.%02x", ((int)desc[3] & 0xff), ((int)desc[2] & 0xff));
			}
			if (TextUtils.isEmpty(info.version)) {
				info.version = String.format("%x.%02x", ((int)desc[13] & 0xff), ((int)desc[12] & 0xff));
			}
			if (TextUtils.isEmpty(info.serial)) {
				info.serial = connection.getSerial();
			}

			final byte[] languages = new byte[256];
			int languageCount = 0;
			// controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
			try {
				int result = connection.controlTransfer(
					USB_REQ_STANDARD_DEVICE_GET, // USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE
    				USB_REQ_GET_DESCRIPTOR,
    				(USB_DT_STRING << 8) | 0, 0, languages, 256, 0);
				if (result > 0) {
        			languageCount = (result - 2) / 2;
				}
				if (languageCount > 0) {
					if (TextUtils.isEmpty(info.manufacturer)) {
						info.manufacturer = getString(connection, desc[14], languageCount, languages);
					}
					if (TextUtils.isEmpty(info.product)) {
						info.product = getString(connection, desc[15], languageCount, languages);
					}
					if (TextUtils.isEmpty(info.serial)) {
						info.serial = getString(connection, desc[16], languageCount, languages);
					}
				}
			} finally {
				connection.close();
			}
		}
		if (TextUtils.isEmpty(info.manufacturer)) {
			info.manufacturer = USBVendorId.vendorName(device.getVendorId());
		}
		if (TextUtils.isEmpty(info.manufacturer)) {
			info.manufacturer = String.format("%04x", device.getVendorId());
		}
		if (TextUtils.isEmpty(info.product)) {
			info.product = String.format("%04x", device.getProductId());
		}
	}
	return info;
}
 
Example 2
Source File: UsbDeviceInfo.java    From libcommon with Apache License 2.0 4 votes vote down vote up
/**
 * USB機器情報(ベンダー名・製品名・バージョン・シリアル等)を取得する
 * @param connection
 * @param device
 * @param out
 * @return
 */
@SuppressLint("NewApi")
public static UsbDeviceInfo getDeviceInfo(
	@Nullable final UsbDeviceConnection connection,
	@Nullable final UsbDevice device, @Nullable final UsbDeviceInfo out) {

	final UsbDeviceInfo result = out != null ? out : new UsbDeviceInfo();
	result.clear();

	result.device = device;
	if (device != null) {
		if (BuildCheck.isAPI29() && (connection != null)) {
			// API>=29でターゲットAPI>=29ならパーミッションがないとシリアル番号を読み込めない
			// connectionがnullでないならopenできているのでパーミッションがある
			result.manufacturer = device.getManufacturerName();
			result.product = device.getProductName();
			result.configCounts = device.getConfigurationCount();
		} else if (BuildCheck.isLollipop()) {	// API >= 21
			result.manufacturer = device.getManufacturerName();
			result.product = device.getProductName();
			result.serial = device.getSerialNumber();
			result.configCounts = device.getConfigurationCount();
		}
		if (BuildCheck.isMarshmallow()) {	// API >= 23
			result.version = device.getVersion();
		}
		if (connection != null) {
			final byte[] desc = connection.getRawDescriptors();
			if (desc != null) {
				if (TextUtils.isEmpty(result.usb_version)) {
					result.usb_version = String.format("%x.%02x", ((int)desc[3] & 0xff), ((int)desc[2] & 0xff));
				}
				if (TextUtils.isEmpty(result.version)) {
					result.version = String.format("%x.%02x", ((int)desc[13] & 0xff), ((int)desc[12] & 0xff));
				}
				if (BuildCheck.isAPI29()) {	// API >= 29
					// API>=29でターゲットAPI>=29ならパーミッションがないとシリアル番号を読み込めない
					result.serial = device.getSerialNumber();
				}
				if (TextUtils.isEmpty(result.serial)) {
					result.serial = connection.getSerial();
				}
				if (result.configCounts < 0) {
					// FIXME 未実装 デバイスディスクリプタをパースせんとなりゃん
					result.configCounts = 1;
				}

				final byte[] languages = new byte[256];
				int languageCount = 0;
				// controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
				int res = connection.controlTransfer(
					USB_REQ_STANDARD_DEVICE_GET, // USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE
					USB_REQ_GET_DESCRIPTOR,
					(USB_DT_STRING << 8)/* | 0*/, 0, languages, 256, 0);
				if (res > 0) {
					languageCount = (res - 2) / 2;
				}
				if (languageCount > 0) {
					if (TextUtils.isEmpty(result.manufacturer)) {
						result.manufacturer = UsbUtils.getString(connection, desc[14], languageCount, languages);
					}
					if (TextUtils.isEmpty(result.product)) {
						result.product = UsbUtils.getString(connection, desc[15], languageCount, languages);
					}
					if (TextUtils.isEmpty(result.serial)) {
						result.serial = UsbUtils.getString(connection, desc[16], languageCount, languages);
					}
				}
			}
		}
		if (TextUtils.isEmpty(result.manufacturer)) {
			result.manufacturer = USBVendorId.vendorName(device.getVendorId());
		}
		if (TextUtils.isEmpty(result.manufacturer)) {
			result.manufacturer = String.format("%04x", device.getVendorId());
		}
		if (TextUtils.isEmpty(result.product)) {
			result.product = String.format("%04x", device.getProductId());
		}
	}

	return result;
}
 
Example 3
Source File: USBMonitor.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * ベンダー名・製品名・バージョン・シリアルを取得する
 * @param manager
 * @param device
 * @param _info
 * @return
 */
public static UsbDeviceInfo updateDeviceInfo(final UsbManager manager, final UsbDevice device, final UsbDeviceInfo _info) {
	final UsbDeviceInfo info = _info != null ? _info : new UsbDeviceInfo();
	info.clear();

	if (device != null) {
		if (BuildCheck.isLollipop()) {
			info.manufacturer = device.getManufacturerName();
			info.product = device.getProductName();
			info.serial = device.getSerialNumber();
		}
		if (BuildCheck.isMarshmallow()) {
			info.usb_version = device.getVersion();
		}
		if ((manager != null) && manager.hasPermission(device)) {
			final UsbDeviceConnection connection = manager.openDevice(device);
			final byte[] desc = connection.getRawDescriptors();

			if (TextUtils.isEmpty(info.usb_version)) {
				info.usb_version = String.format("%x.%02x", ((int)desc[3] & 0xff), ((int)desc[2] & 0xff));
			}
			if (TextUtils.isEmpty(info.version)) {
				info.version = String.format("%x.%02x", ((int)desc[13] & 0xff), ((int)desc[12] & 0xff));
			}
			if (TextUtils.isEmpty(info.serial)) {
				info.serial = connection.getSerial();
			}

			final byte[] languages = new byte[256];
			int languageCount = 0;
			// controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
			try {
				int result = connection.controlTransfer(
					USB_REQ_STANDARD_DEVICE_GET, // USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE
    				USB_REQ_GET_DESCRIPTOR,
    				(USB_DT_STRING << 8) | 0, 0, languages, 256, 0);
				if (result > 0) {
        			languageCount = (result - 2) / 2;
				}
				if (languageCount > 0) {
					if (TextUtils.isEmpty(info.manufacturer)) {
						info.manufacturer = getString(connection, desc[14], languageCount, languages);
					}
					if (TextUtils.isEmpty(info.product)) {
						info.product = getString(connection, desc[15], languageCount, languages);
					}
					if (TextUtils.isEmpty(info.serial)) {
						info.serial = getString(connection, desc[16], languageCount, languages);
					}
				}
			} finally {
				connection.close();
			}
		}
		if (TextUtils.isEmpty(info.manufacturer)) {
			info.manufacturer = USBVendorId.vendorName(device.getVendorId());
		}
		if (TextUtils.isEmpty(info.manufacturer)) {
			info.manufacturer = String.format("%04x", device.getVendorId());
		}
		if (TextUtils.isEmpty(info.product)) {
			info.product = String.format("%04x", device.getProductId());
		}
	}
	return info;
}