Code example for UsbDeviceConnection

Methods: claimInterface, controlTransfer

3
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;
            Thread thread = new Thread(null, this, "ScaleMonitor");
            thread.start();
 
            //Get scale attributes 
            updateAttributesData(mConnection);
            //Update the weight limit information 
            updateLimitData(mConnection);
        } else { 
            Toast.makeText(this, "open FAIL", Toast.LENGTH_SHORT).show();
            mConnection = null;
            mRunning = false;
        } 
    } 
 
    private static final int MSG_DATA = 101;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) { 
            switch (msg.what) { 
            case MSG_DATA: 
                byte[] data = (byte[]) msg.obj; 
                //This value will always be 0x03 for the data report 
                byte reportId = data[0]; 
                //Maps to a status constant defined for HID POS 
                byte status = data[1]; 
                setWeightColor(status); 
 
                //Maps to a units constant defined for HID POS 
                int units = (data[2] & 0xFF); 
 
                //Scaling applied to the weight value, if any 
                byte scaling = data[3]; 
 
                //Two byte value representing the weight itself 
                int weight = (data[5] & 0xFF) << 8; 
                weight += (data[4] & 0xFF); 
 
                switch (units) { 
                    case UNITS_GRAM: 
                        updateWeightGrams(weight); 
                        break; 
                    case UNITS_OUNCE: 
                        updateWeightPounds(weight); 
                        break; 
                    default: 
                        mWeight.setText("---"); 
                        break; 
                } 
                break; 
            default: 
                break; 
            } 
        } 
    }; 
 
    public void postWeightData(byte[] data) {
        mHandler.sendMessage(Message.obtain(mHandler, MSG_DATA, data));
    } 
 
    private void updateAttributesData(UsbDeviceConnection connection) {
        int requestType = 0xA1; // 1010 0001b
        int request = 0x01; //HID GET_REPORT
        int value = 0x0301; //Feature report, ID = 1
        int index = 0; //Interface 0
        int length = 3;
 
        byte[] buffer = new byte[length];
        connection.controlTransfer(requestType, request, value, index, buffer, length, 2000);
 
        //Parse the data and fill the display 
        byte reportId = buffer[0]; //Always 0x01
 
        int scaleClass = (buffer[1] & 0xFF);
        //Maps to a units constant defined for HID POS 
        int defaultUnits = (buffer[2] & 0xFF);
 
        mAttributes.setText(String.format("Class: %d, Default Units: %s", scaleClass, getUnitDisplayString(defaultUnits)));
    } 
 
    /* 
     * Get the weight limit of the connected scale by requesting a 
     * Weight Limit Feature Report. 
     */ 
    private void updateLimitData(UsbDeviceConnection connection) {
        int requestType = 0xA1; // 1010 0001b
        int request = 0x01; //HID GET_REPORT
        int value = 0x0305; //Feature report, ID = 5
        int index = 0; //Interface 0
        int length = 5;
 
        byte[] buffer = new byte[length];
        connection.controlTransfer(requestType, request, value, index, buffer, length, 2000);
 
        //Parse the data and fill the display 
        byte reportId = buffer[0]; //Always 0x05
        //Maps to a units constant defined for HID POS 
        int unit = (buffer[1] & 0xFF);