Android host: connect to a USB mass storage device

I am currently trying to develop an application that accesses xml files on a USB device. I read the Google Documentation about Android Host for Android. Now I can find my USB device, find out its specifications (for example, PID / VID), but I can’t access the files of the USB device :(

Here is my activity code that is looking for devices:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_visu);


  affichage = (TextView) findViewById(R.id.afficher);
  context = VisuActivity.this.getApplicationContext();
  UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
  HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

  Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

  if(deviceList.size()==1){
      while(deviceIterator.hasNext()){
             device = deviceIterator.next();

      }

      UsbInterface mUsbInterface = device.getInterface(0);
      UsbEndpoint endpoint = mUsbInterface.getEndpoint(0);

      UsbDeviceConnection connection = manager.openDevice(device);

   }
}
  /* What To Do Now ???? */

I tried to find some example on the Internet, but now I'm lost !: (

Does anyone know how to make a read (and ultimately) file on a USB device? I heard that there is a Mass Storage protocol, but I do not find it and do not understand!

+5
source share
1 answer

Listen to intent filter

 <intent-filter>
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <action android:name="android.intent.action.MEDIA_UNMOUNTED" />

                <data android:scheme="file" />
 </intent-filter>

, usb , , - , , USB , usb.

, usb,

public void onReceive(Context context, Intent intent) {
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        File file=new File("/storage/usbdisk");
                File[] listFiles = file.listFiles();
                for ( File f : listFiles){
                    f.getAbsoluteFilePath()
                    //you can do all file processing part here
                }
    }
}

usb , "/storage" "usb", usb "/storage/usbdisk"

-1

All Articles