Example usage for android.hardware.usb UsbDevice getDeviceName

List of usage examples for android.hardware.usb UsbDevice getDeviceName

Introduction

In this page you can find the example usage for android.hardware.usb UsbDevice getDeviceName.

Prototype

public @NonNull String getDeviceName() 

Source Link

Document

Returns the name of the device.

Usage

From source file:com.googlecode.android_scripting.facade.USBHostSerialFacade.java

/**
 * collectUSBDevices: start USB connection, from usbserialConnect {{{1
 *//*from   ww  w.ja v  a  2s  .  c om*/
private String connectUSBDevice(String hash) {
    Integer nHash;
    String ret = "";
    boolean deviceFound = false;

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(mService, 0, new Intent(ACTION_USB_PERMISSION),
            0);
    Map<String, UsbDevice> map = mUsbManager.getDeviceList();

    if (hash.equals("")) {
        nHash = 0;
    } else {
        nHash = Integer.parseInt(hash);
    }

    Log.d("USBHostSerial: collectUSBDevices()");
    for (UsbDevice device : map.values()) {
        Log.d("USBHostSerial: try to check " + device.hashCode());
        if (!(nHash == 0) && (nHash != device.hashCode())) {
            continue;
        }
        Log.d("USBHostSerial: requestPermission to =>" + device.getDeviceName());

        UsbSerialConnection conn;
        try {
            conn = new UsbSerialConnection();
        } catch (IOException e) {
            Log.d("can't create UsbSerialConnection object");
            continue;
        }
        conn.mDevice = device;
        conn.options = new String(options);
        addConnection(conn);

        mUsbManager.requestPermission(device, mPermissionIntent);
        deviceFound = true;
        ret += ",\"" + conn.getUUID() + "\"";
    }
    if (!deviceFound) {
        connectionFailed();
        return "device not found";
    }
    return "[\"OK\"" + ret + "]";
}

From source file:com.googlecode.android_scripting.facade.USBHostSerialFacade.java

/**
 * getConnection (UsbDevice): obtain connection object from the connection entries {{{1
 *///  w w w  . ja  va2 s .co m
private UsbSerialConnection getConnection(UsbDevice device) throws IOException {
    for (Map.Entry<String, UsbSerialConnection> entry : connections.entrySet()) {
        UsbSerialConnection conn;
        conn = entry.getValue();
        if (conn.mDevice == device) {
            return conn;
        }
        if (conn.mDevice.hashCode() == device.hashCode()) {
            return conn;
        }
        Log.d(String.format("USB Host Serial: %s != %s", conn.mDevice.getDeviceName(), device.getDeviceName()));
    }
    return null;
}

From source file:com.example.nfcreaderorigin.MainActivity.java

public void getUsbConnect() {

    mReaderAdapterX = "";
    for (UsbDevice device : mManager.getDeviceList().values()) {
        if (mReader.isSupported(device)) {
            mReaderAdapterX = device.getDeviceName().toString();
        }//from   w  ww. j  a v a 2s .c  o  m
    }
}

From source file:com.example.nfcreaderorigin.MainActivity.java

public void getDevices() {

    boolean requested = false;

    // Disable open button
    mOpenButton.setEnabled(false);//  w  w  w.j a  va  2s .co m

    mReaderAdapterX = "";

    for (UsbDevice device : mManager.getDeviceList().values()) {

        if (mReader.isSupported(device)) {
            mReaderAdapterX = device.getDeviceName();
        }
    }
    String deviceName = mReaderAdapterX;
    if (deviceName != null) {

        // For each device
        for (UsbDevice device : mManager.getDeviceList().values()) {

            // If device name is found
            if (deviceName.equals(device.getDeviceName())) {

                // Request permission
                mManager.requestPermission(device, mPermissionIntent);

                requested = true;

                break;
            }
        }
    }

    if (!requested) {

        // Enable open button
        mOpenButton.setEnabled(true);
    }
}

From source file:com.example.nfcreaderorigin.MainActivity.java

/** Called when the activity is first created. */
@Override/* ww w  . j av a  2  s.co m*/
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setBlockNumber();
    // Get USB manager
    mManager = (UsbManager) getSystemService(Context.USB_SERVICE);

    // Initialize reader
    mReader = new Reader(mManager);
    mReader.setOnStateChangeListener(new OnStateChangeListener() {

        @Override
        public void onStateChange(int slotNum, int prevState, int currState) {

            if (prevState < Reader.CARD_UNKNOWN || prevState > Reader.CARD_SPECIFIC) {
                prevState = Reader.CARD_UNKNOWN;
            }

            if (currState < Reader.CARD_UNKNOWN || currState > Reader.CARD_SPECIFIC) {
                currState = Reader.CARD_UNKNOWN;
            }

            // Create output string
            final String outputString = "Slot " + slotNum + ": " + stateStrings[prevState] + " -> "
                    + stateStrings[currState];
            final String state = stateStrings[currState];

            // Show output
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    logMsg(outputString);
                    if (state.equals("Present")) {
                        mAuthenticateRead = false;
                        if (mReadMode) {
                            setBlockNumber();
                            try {

                                mTaskReadStart = false;
                                mTaskStart = false;
                                mTaskWriteStart = false;

                                TimeRead = 0;
                                TimeWrite = 0;
                                timeEnd = 0;
                                initStart = 0;
                                nowRead = 0;
                                nowWrite = 0;
                                nowStart = 0;

                                mBlockAuthen = true;
                                mReadMode = true;
                                mWriteMode = false;

                                System.gc();

                                reader();

                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }

                    }
                }
            });
        }
    });

    // Register receiver for USB permission
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mReceiver, filter);

    sdf = new SimpleDateFormat("mm:ss.SSS");

    mResponseTextView = (TextView) findViewById(R.id.main_text_view_response);
    mResponseTextView.setMovementMethod(new ScrollingMovementMethod());
    mResponseTextView.setMaxLines(MAX_LINES);
    mResponseTextView.setText("");

    mTextType = (TextView) findViewById(R.id.textType);
    mReadTime = (TextView) findViewById(R.id.textViewReadSet);
    mWriteTime = (TextView) findViewById(R.id.textViewWriteSet);
    mEndTime = (TextView) findViewById(R.id.textViewEndSet);

    mWriteType = (EditText) findViewById(R.id.EditText_writeDataType);
    mQty = (EditText) findViewById(R.id.QTY);

    formatter = new DecimalFormat("###,###,###.##");

    mWriteDataBalanceEditText = (EditText) findViewById(R.id.EditText_writeDataBalance);
    mWriteDataIdEditText = (EditText) findViewById(R.id.EditText_writeDataId);

    mTextMode = (TextView) findViewById(R.id.textMode);
    mTextBalance = (TextView) findViewById(R.id.BALANCE);

    mTextId = (TextView) findViewById(R.id.textId);
    // Initialize reader spinner

    for (UsbDevice device : mManager.getDeviceList().values()) {
        if (mReader.isSupported(device)) {
            mReaderAdapterX = device.getDeviceName().toString();
        }
    }
    // Initialize open button
    mOpenButton = (Button) findViewById(R.id.btnStart);
    mOpenButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            getDevices();
            // Disable open button
            mOpenButton.setEnabled(false);

        }
    });

    // Initialize close button
    mCloseButton = (Button) findViewById(R.id.btnStop);
    mCloseButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // Disable buttons

            mOpenButton.setEnabled(false);
            // Clear slot items

            // Close reader
            logMsg("Closing reader...");
            new CloseTask().execute();
        }
    });

    mWriteData = (Button) findViewById(R.id.btnWrite);
    mWriteData.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mAuthenticateRead == true) {
                mBlockWrite = true;
                setBlockNumber();
                mReadMode = false;
                mWriteMode = true;
                mTextMode.setText("- W R I T E -");
                //               mReadData.setVisibility(View.VISIBLE);
                //               mWriteData.setVisibility(View.GONE);
                try {
                    writer();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(), "Please Authenticate first", Toast.LENGTH_SHORT).show();
            }

        }
    });

    mReadData = (Button) findViewById(R.id.btnRead);
    mReadData.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            mBlockAuthen = true;
            setBlockNumber();
            mReadMode = true;
            mWriteMode = false;
            mTextMode.setText("- R E A D -");
            //            mReadData.setVisibility(View.GONE);
            //            mWriteData.setVisibility(View.VISIBLE);

        }
    });

    // PIN verification command (ACOS3)
    byte[] pinVerifyData = { (byte) 0x80, 0x20, 0x06, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };

    // Initialize PIN verify structure (ACOS3)
    mPinVerify.setTimeOut(0);
    mPinVerify.setTimeOut2(0);
    mPinVerify.setFormatString(0);
    mPinVerify.setPinBlockString(0x08);
    mPinVerify.setPinLengthFormat(0);
    mPinVerify.setPinMaxExtraDigit(0x0408);
    mPinVerify.setEntryValidationCondition(0x03);
    mPinVerify.setNumberMessage(0x01);
    mPinVerify.setLangId(0x0409);
    mPinVerify.setMsgIndex(0);
    mPinVerify.setTeoPrologue(0, 0);
    mPinVerify.setTeoPrologue(1, 0);
    mPinVerify.setTeoPrologue(2, 0);
    mPinVerify.setData(pinVerifyData, pinVerifyData.length);

    // PIN modification command (ACOS3)
    byte[] pinModifyData = { (byte) 0x80, 0x24, 0x00, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };

    // Initialize PIN modify structure (ACOS3)
    mPinModify.setTimeOut(0);
    mPinModify.setTimeOut2(0);
    mPinModify.setFormatString(0);
    mPinModify.setPinBlockString(0x08);
    mPinModify.setPinLengthFormat(0);
    mPinModify.setInsertionOffsetOld(0);
    mPinModify.setInsertionOffsetNew(0);
    mPinModify.setPinMaxExtraDigit(0x0408);
    mPinModify.setConfirmPin(0x01);
    mPinModify.setEntryValidationCondition(0x03);
    mPinModify.setNumberMessage(0x02);
    mPinModify.setLangId(0x0409);
    mPinModify.setMsgIndex1(0);
    mPinModify.setMsgIndex2(0x01);
    mPinModify.setMsgIndex3(0);
    mPinModify.setTeoPrologue(0, 0);
    mPinModify.setTeoPrologue(1, 0);
    mPinModify.setTeoPrologue(2, 0);
    mPinModify.setData(pinModifyData, pinModifyData.length);

    // Initialize read key option
    mReadKeyOption.setTimeOut(0);
    mReadKeyOption.setPinMaxExtraDigit(0x0408);
    mReadKeyOption.setKeyReturnCondition(0x01);
    mReadKeyOption.setEchoLcdStartPosition(0);
    mReadKeyOption.setEchoLcdMode(0x01);

    // Disable buttons
    //      mCloseButton.setEnabled(false);
    //      mControlButton.setEnabled(false);

    // Hide input window
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}