Code example for UsbManager

Methods: openDevice, requestPermission

0
                } 
            } 
            //If a new device is attached, connect to it 
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { 
                Log.i(TAG, "Device Attached"); 
                mUsbManager.requestPermission(device, mPermissionIntent); 
            } 
            //If this is our permission request, check result 
            if (ACTION_USB_PERMISSION.equals(action)) { 
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) 
                        && device != null) { 
                    //Connect to the device 
                    setDevice(device); 
                } else { 
                    Log.d(TAG, "permission denied for device " + device); 
                    setDevice(null); 
                } 
            } 
        } 
    }; 
 
    public void onZeroClick(View view) {
        if (mConnection != null) {
            setZeroScale(mConnection);
        } 
    } 
 
    /* 
     * Methods to update the UI with the weight value read 
     * from the USB scale. 
     */ 
 
    private void resetDisplay() { 
        mWeight.setText("---");
        setWeightColor(0); 
 
        mStatus.setText("{ }");
        mAttributes.setText("{ }");
    } 
 
    private void setWeightColor(int status) {
        switch(status) {
        case STATUS_FAULT:
        case STATUS_UNDERZERO:
            mWeight.setTextColor(Color.RED);
            break; 
        case STATUS_STABLE:
        case STATUS_STABLEZERO:
            mWeight.setTextColor(Color.GREEN);
            break; 
        default: 
            mWeight.setTextColor(Color.WHITE);
            break; 
        } 
    } 
     
    // Value is the number of oz. times 100 
    private static final int POUNDS_PER_OZ = 160; // 16lb per 0.1oz
    private void updateWeightPounds(int weight) {
        String result;
 
        if (weight >= POUNDS_PER_OZ) {
            float pounds = weight / POUNDS_PER_OZ;
            float remainder = (weight % POUNDS_PER_OZ) / 10.0f;
            result = String.format("%.0f lb, %.1f oz", pounds, remainder);
        } else { 
            float ounces = weight / 10.0f;
            result = String.format("%.1f oz", ounces);
        } 
 
        mWeight.setText(result);
    } 
 
    // Value is the number of grams 
    private static final int GRAMS_PER_KG = 1000;
    private void updateWeightGrams(int weight) {
        String result;
 
        if (weight >= GRAMS_PER_KG) {
            float kg = weight / GRAMS_PER_KG;
            float remainder = (weight % GRAMS_PER_KG);
            result = String.format("%.0f kg, %.1f g", kg, remainder);
        } else { 
            float grams = weight;
            result = String.format("%.0f g", grams);
        } 
 
        mWeight.setText(result);
    } 
 
    /* 
     * 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)) {
            //Start the polling thread 
            Toast.makeText(this, "open SUCCESS", Toast.LENGTH_SHORT).show();
            mConnection = connection;
            mRunning = true;