0

I have tried to access the pendrive in mobile phones. Usb pen drives were detecting in android mobiles. But how to access the contents inside pendrive in android. Below is the code taken from How to receive USB connection status broadcast? I have checked with the code and able to get the usb and now i need to get the contents of pen drive.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mDeviceText = (TextView) findViewById(R.id.text_status);
    mConnectButton = (Button) findViewById(R.id.button_connect);
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
}

@Override
protected void onRestart() {
    super.onRestart();
    recreate();
}

@Override
protected void onResume() {
    super.onResume();

    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
    updateDeviceList();
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mUsbReceiver);
}

public void onConnectClick(View v) {
    if (mDevice == null) {
        return;
    }
    mUsbManager.requestPermission(mDevice, mPermissionIntent);
}


private static final String ACTION_USB_PERMISSION = "com.android.recipes.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {

            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
                    && device != null) {

                getDeviceStatus(device);
            } else {
                Log.d(TAG, "permission denied for device " + device);
            }
        }
    }
};


private void getDeviceStatus(UsbDevice device) {
    UsbDeviceConnection connection = mUsbManager.openDevice(device);
    byte[] buffer = new byte[LENGTH];
    connection.controlTransfer(REQUEST_TYPE, REQUEST, REQ_VALUE, REQ_INDEX,
            buffer, LENGTH, 2000);
    connection.close();
}

private void updateDeviceList() {
    HashMap<String, UsbDevice> connectedDevices = mUsbManager
            .getDeviceList();
    if (connectedDevices.isEmpty()) {
        mDevice = null;
        mDeviceText.setText("No Devices Currently Connected");
        mConnectButton.setEnabled(false);
    } else {
        for (UsbDevice device : connectedDevices.values()) {
            mDevice = device;
        }
        mDeviceText.setText("USB Device connected");
        mConnectButton.setEnabled(true);
    }
}
varrun
  • 1
  • 1

0 Answers0