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

The following examples show how to use android.hardware.usb.UsbDeviceConnection#close() . 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: UsbSession.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
@Override
public @NonNull
Iso7816Connection openIso7816Connection() throws IOException {
    UsbInterface ccidInterface = getInterface(UsbConstants.USB_CLASS_CSCID);
    if (ccidInterface == null) {
        throw new IOException("No CCID interface found!");
    }
    Pair<UsbEndpoint, UsbEndpoint> endpointPair = findEndpoints(ccidInterface, UsbConstants.USB_ENDPOINT_XFER_BULK);
    if (endpointPair.first == null || endpointPair.second == null) {
        throw new IOException("Unable to find endpoints!");
    }

    UsbDeviceConnection connection = openConnection();
    if (connection == null) {
        throw new IOException("exception in UsbManager.openDevice");
    }

    if (!connection.claimInterface(ccidInterface, true)) {
        connection.close();
        throw new IOException("Interface couldn't be claimed");
    }

    return new UsbIso7816Connection(connection, ccidInterface, endpointPair.first, endpointPair.second);
}
 
Example 2
Source File: UsbSession.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and starts session for communication with yubikey using HID interface
 * @return session for communication with yubikey (supported over USB only)
 * @throws IOException if Keyboard HID interface or endpoints are not found
 */
public @NonNull
UsbHidConnection openHidKeyboardConnection() throws IOException {
    UsbInterface hidInterface = getInterface(UsbConstants.USB_CLASS_HID);
    if (hidInterface == null) {
        throw new IOException("No HID interface found");
    }

    if (hidInterface.getInterfaceSubclass() != UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) {
        throw new IOException("No expected HID interface");
    }

    UsbDeviceConnection connection = openConnection();
    if (connection == null) {
        throw new IOException("exception in UsbManager.openDevice");
    }

    if (!connection.claimInterface(hidInterface, true)) {
        connection.close();
        throw new IOException("Interface couldn't be claimed");
    }

    return new UsbHidConnection(connection, hidInterface);
}
 
Example 3
Source File: MainActivity.java    From astrobee_android with Apache License 2.0 6 votes vote down vote up
protected void setupUsb(UsbDevice device) {
    UsbInterface inf = device.getInterface(0);
    UsbDeviceConnection conn = mUsbManager.openDevice(device);
    if (conn == null) {
        Log.wtf("MainActivity", "unable to open device?");
        return;
    }

    if (!conn.claimInterface(inf, true)) {
        conn.close();
        Log.wtf("MainActivity", "unable to claim interface!");
        return;
    }

    mBlinkDevice = device;
    mBlinkConn = conn;
}
 
Example 4
Source File: MainActivity.java    From astrobee_android with Apache License 2.0 6 votes vote down vote up
protected void setupUsb(UsbDevice device) {
    UsbInterface inf = device.getInterface(0);
    UsbDeviceConnection conn = mUsbManager.openDevice(device);
    if (conn == null) {
        Log.wtf("MainActivity", "unable to open device?");
        return;
    }

    if (!conn.claimInterface(inf, true)) {
        conn.close();
        Log.wtf("MainActivity", "unable to claim interface!");
        return;
    }

    mBlinkDevice = device;
    mBlinkConn = conn;
}
 
Example 5
Source File: UsbDeviceInfo.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * USB機器情報(ベンダー名・製品名・バージョン・シリアル等)を取得する
 * @param manager
 * @param device
 * @param out
 * @return
 */
@SuppressLint("NewApi")
public static UsbDeviceInfo getDeviceInfo(
	@NonNull final UsbManager manager,
	@Nullable final UsbDevice device, @Nullable final UsbDeviceInfo out) {

	final UsbDeviceConnection connection
		= (device != null && manager.hasPermission(device))
			? manager.openDevice(device) : null;

	try {
		return getDeviceInfo(connection, device, out);
	} finally {
		if (connection != null) {
			connection.close();
		}
	}
}
 
Example 6
Source File: MainActivity.java    From astrobee_android with Apache License 2.0 5 votes vote down vote up
private void performUsbPermissionCallback(final UsbDevice device) {
    if (mUsbHandler.getLooper().getThread() != Thread.currentThread()) {
        mUsbHandler.post(new Runnable() {
            @Override
            public void run() {
                performUsbPermissionCallback(device);
            }
        });
        return;
    }

    if (mPicoflexx != null) {
        Log.d(TAG, "Already have a picoflexx");
        return;
    }

    UsbDeviceConnection conn = mUsbManager.openDevice(device);
    Log.i(TAG, "USB Device: " + device.getDeviceName() + ", fd: " + conn.getFileDescriptor());

    if (!openPicoflexx(conn.getFileDescriptor())) {
        Log.e(TAG, "error initializing the picoflexx");
        mUiHandler.obtainMessage(WHAT_STATE, STATE_ERROR, R.string.error_initializing)
                .sendToTarget();
        conn.close();
        return;
    }

    mConn = conn;
    mPicoflexx = device;

    final int width = getMaxWidth();
    final int height = getMaxHeight();

    mCloud.setWidth(width);
    mCloud.setHeight(height);
    mCloud.setRowStep(width * 12);

    mUiHandler.obtainMessage(WHAT_STATE, STATE_IDLE, -1).sendToTarget();
}
 
Example 7
Source File: USBMonitor.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * デバイスを閉じる
 * Java内でインターフェースをopenして使う時は開いているインターフェースも閉じる
 */
public void close() {
	if (DEBUG) Log.i(TAG, "UsbControlBlock#close:");

	UsbDeviceConnection connection;
	synchronized (this) {
		connection = mConnection;
		mConnection = null;
	}
	if (connection != null) {
		// 2015/01/06 closeしてからonDisconnectを呼び出すように変更
		// openしているinterfaceが有れば閉じる XXX Java側でインターフェースを使う時
		final int n = mInterfaces.size();
		for (int i = 0; i < n; i++) {
			final SparseArray<UsbInterface> intfs = mInterfaces.valueAt(i);
			if (intfs != null) {
				final int m = intfs.size();
				for (int j = 0; j < m; j++) {
					final UsbInterface intf = intfs.valueAt(j);
					connection.releaseInterface(intf);
				}
				intfs.clear();
			}
		}
		mInterfaces.clear();
		connection.close();
		final USBMonitor monitor = getMonitor();
		final UsbDevice device = getDevice();
		if ((monitor != null) && (device != null)) {
			monitor.callOnDisconnect(device, this);
		}
	}
}
 
Example 8
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 9
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;
}