Java – Send data from android to a connected USB storage device in USB host mode

Send data from android to a connected USB storage device in USB host mode… here is a solution to the problem.

Send data from android to a connected USB storage device in USB host mode

In my application, I’m using USB host mode, which provides information about connected USB mass storage devices, such as USB Flash Drive in my use case. Now I need to create a file on the connected flash drive and save some data in the file. So far, I found the device connected to the following one

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
        private Button mCheckForDevice;
        private TextView mDeviceInfo;
        private static final String ACTION_USB_PERMISSION = "";
        private PendingIntent mPermissionIntent;
        private UsbManager mUsbManager;
        private UsbDevice mDeviceFound;
        private UsbDeviceConnection mConnection;
        private UsbInterface mUsbInterface = null;
        private UsbEndpoint mInputEndpoint = null;
        private UsbEndpoint mOutputEndpoint = null;

@RequiresApi(api = Build.VERSION_CODES. LOLLIPOP)
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            mCheckForDevice = (Button) findViewById(;
            mDeviceInfo = (TextView) findViewById(;

mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            registerReceiver(mUsbReceiver, filter);

while (deviceIterator.hasNext()) {
                final UsbDevice device =;
                mDeviceFound = device;
                i += "\n" +
                        "DeviceID: " + device.getDeviceId() + "\n" +
                        "DeviceName: " + device.getDeviceName() + "\n" +
                        "VendorID: " + device.getVendorId() + "\n" +
                        "ProductID: " + device.getProductId() + "\n" +
                        "Serial Number: " + device.getSerialNumber() + "\n";
            mCheckForDevice.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES. LOLLIPOP)
                public void onClick(View v) {
                    if(deviceList.size() > 0){
                        Toast.makeText(getApplicationContext(), "No device found", Toast.LENGTH_SHORT).show();
        String i = "";
        private int count ;
        private void checkInfo(UsbDevice device) {
            if(count == 1) {
                mUsbManager.requestPermission(device, mPermissionIntent);

} else
                Toast.makeText(this, "Already connected", Toast.LENGTH_SHORT).show();

        protected void onPause() {
            try {
            }catch (Exception e){

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
            Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show(); writeToFile("onReceive");
                if (ACTION_USB_PERMISSION.equals(action)) {
                    synchronized (this) {
                        UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.d(TAG, "permission denied for device " + device);
                            Toast.makeText(context, "permission denied for device" + device, Toast.LENGTH_SHORT).show();

private void connectUsb(UsbDevice device) {
            if(device != null){
                for(int i=0; i<device.getInterfaceCount(); i++){
                    mUsbInterface = device.getInterface(i);
                    UsbEndpoint tOut = null;
                    UsbEndpoint tIn = null;
                    int usbEndPointCount = mUsbInterface.getEndpointCount();
                    if(usbEndPointCount >=2){
                        for(int j =0; j<usbEndPointCount; j++){
                            if(mUsbInterface.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK){
                                if(mUsbInterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT){
                                    tOut = mUsbInterface.getEndpoint(j);
                                }else if(mUsbInterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN){
                                    tIn = mUsbInterface.getEndpoint(j);
                        if(tIn!=null & tOut !=null){
                            mInputEndpoint = tIn;
                            mOutputEndpoint = tOut;
                mConnection = mUsbManager.openDevice(device);
                if (mConnection.claimInterface(mUsbInterface, true)) {
                    Toast.makeText(this, "Connected to device", Toast.LENGTH_SHORT).show();

String msg = "Hello world";
                    byte[] byteArray = msg.getBytes();
                    int dataTransfered = mConnection.bulkTransfer(mOutputEndpoint,byteArray,byteArray.length, 0);

int controlTransfer = mConnection.controlTransfer( UsbConstants.USB_DIR_OUT, 1,0,0,byteArray,byteArray.length,0);
                    Toast.makeText(this, "controlTransfer " +controlTransfer, Toast.LENGTH_SHORT).show();

} else {
                    Log.i(TAG, "Could not connect!");
                    Toast.makeText(this, "Could not connect", Toast.LENGTH_SHORT).show();
                Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:text="Search for devices "
        android:textSize="15sp" />

        android:textSize="15sp" />

In the example above, I tried to send a String value to the connected device, and the returned int value is the number of characters sent. But where is this information stored on connected devices? I mean location?

Once a connection is established with the device, is there a way to create a file on the connected device? Links to relevant blog posts are helpful.

I found This is on the stack, but the solution doesn’t help anything.

Thanks for any help. Thank you.


You are searching for bulk endpoints, which means you are trying to use BBB for sector read/write (see USB specification for mass storage class BBB).

Now I need to create a file on the connected flash drive and save some data in the file.
Use endpoints to perform sector read/write, but file system drivers (such as FAT32 or EXT4, etc.) must be written on it.
File creation is a file system operation, not a sector-level read/write.

Is there a way to create a file on a connected device after establishing a connection with it?
You must use the Android file system installation tool and then use the Common File API to create the file.

Related Problems and Solutions