android.hardware.usb.UsbDeviceConnection

Here are the examples of the java api class android.hardware.usb.UsbDeviceConnection taken from open source projects.

1. USBActivity#getDeviceStatus()

Project: accessory-samples
Source File: USBActivity.java
View license
private void getDeviceStatus(UsbDevice device) {
    UsbDeviceConnection connection = mUsbManager.openDevice(device);
    //Create a sufficiently large buffer for incoming data
    byte[] buffer = new byte[LENGTH];
    connection.controlTransfer(REQUEST_TYPE, REQUEST, REQ_VALUE, REQ_INDEX, buffer, LENGTH, 2000);
    //Parse received data into a description
    String description = parseConfigDescriptor(buffer);
    mDisplayText.setText(description);
    connection.close();
}

2. UsbVehicleInterface#openInterface()

View license
private UsbDeviceConnection openInterface(UsbManager manager, UsbDevice device, UsbInterface iface) throws UsbDeviceException {
    UsbDeviceConnection connection = manager.openDevice(device);
    if (connection == null) {
        throw new UsbDeviceException("Couldn't open a connection to " + "device -- user may not have given permission");
    }
    mInterface = iface;
    connection.claimInterface(mInterface, true);
    return connection;
}

3. ScaleActivity#setDevice()

View license
/*
     * Enumerate the endpoints and interfaces on the connected
     * device to find the Interrupt endpoint we need to poll
     * for the scale data.
     */
private void setDevice(UsbDevice device) {
    Log.d(TAG, "setDevice " + device);
    if (device == null) {
        //Cancel connections
        mConnection = null;
        mRunning = false;
        resetDisplay();
        return;
    }
    //Verify the device has what we need
    if (device.getInterfaceCount() != 1) {
        Log.e(TAG, "could not find interface");
        return;
    }
    UsbInterface intf = device.getInterface(0);
    // device should have one endpoint
    if (intf.getEndpointCount() != 1) {
        Log.e(TAG, "could not find endpoint");
        return;
    }
    // endpoint should be of type interrupt
    UsbEndpoint ep = intf.getEndpoint(0);
    if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT || ep.getDirection() != UsbConstants.USB_DIR_IN) {
        Log.e(TAG, "endpoint is not Interrupt IN");
        return;
    }
    mDevice = device;
    mEndpointIntr = ep;
    UsbDeviceConnection connection = mUsbManager.openDevice(device);
    if (connection != null && connection.claimInterface(intf, true)) {
        mConnection = connection;
        //Get scale attributes
        updateAttributesData(mConnection);
        //Update the weight limit information
        updateLimitData(mConnection);
        //Start the polling thread
        mRunning = true;
        Thread thread = new Thread(null, this, "ScaleMonitor");
        thread.start();
    } else {
        mConnection = null;
        mRunning = false;
    }
}

4. MidiDeviceConnectionWatcher#onDeviceDetached()

View license
/**
     * Notify the specified device has been detached
     *
     * @param detachedDevice the USB MIDI device
     */
private void onDeviceDetached(@NonNull UsbDevice detachedDevice) {
    deviceDetachedListener.onDeviceDetached(detachedDevice);
    // Stop input device's thread.
    Set<MidiInputDevice> inputDevices = midiInputDevices.get(detachedDevice);
    if (inputDevices != null && inputDevices.size() > 0) {
        for (MidiInputDevice inputDevice : inputDevices) {
            if (inputDevice != null) {
                inputDevice.stop();
                deviceDetachedListener.onMidiInputDeviceDetached(inputDevice);
            }
        }
        midiInputDevices.remove(detachedDevice);
    }
    Set<MidiOutputDevice> outputDevices = midiOutputDevices.get(detachedDevice);
    if (outputDevices != null) {
        for (MidiOutputDevice outputDevice : outputDevices) {
            if (outputDevice != null) {
                outputDevice.stop();
                deviceDetachedListener.onMidiOutputDeviceDetached(outputDevice);
            }
        }
        midiOutputDevices.remove(detachedDevice);
    }
    UsbDeviceConnection deviceConnection = deviceConnections.get(detachedDevice);
    if (deviceConnection != null) {
        deviceConnection.close();
        deviceConnections.remove(detachedDevice);
    }
}