0

I need to read and write data files on a USB stick from my Android application. So far, I have not had much success.

My primary reference for doing this has been the Android Developer's web site documentation at: http://developer.android.com/guide/topics/connectivity/usb/host.html

The closest existing Stack Overflow answer that I have found on this topic is 10183794.

I am developing and testing my code on a generic, no-name Android tablet. It is running Android 4.2.2.

The tablet has a Host USB port into which I can directly insert my USB stick. The tablet does not have an OTG micro USB connector.

When I insert a USB stick into the tablet, the tablet itself does detect it. I can see that the USB stick is mounted at: /mnt/usbhost1.

So, while the tablet can detect the presence of the USB stick, the application that I am developing does not seem to see it.

The Android Developers USB Host documentation describes 2 ways to detect a USB stick: 1. Discovering a device and 2. Enumerating a device. Neither approach is working for me.

My Manifest.xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android1"
   android:versionCode="1"
   android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission
    android:required="true"
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>      

<uses-permission
    android:required="true"
    android:name="android.permission.READ_EXTERNAL_STORAGE"/>   

<uses-feature android:name="android.hardware.usb.host" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.android1.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name" >
      <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
         <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />  
         <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>  
      <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
                  android:resource="@xml/device_filter" />       
    </activity>
    <receiver android:name=".UsbBroadcastReceiver" >
    </receiver>             
</application>

The xml/device_filter file referenced above is as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <usb-device />
</resources>

I have not included a vendor-id, product-id, nor any other attribute for usb-device above since the documentation states that not specifying them will match every USB device, which is what I want.

When I run the application and try to open a data file on the USB, the application does not display a dialog asking me to grant permission for the application to access the USB stick. From what I read in the USB Host documentation, I should be getting a dialog message.

Instead, I get the following error message:

07-17 13:34:14.630: D/tom5(4639): error msg: /mnt/usbhost1/smartshoe-data/SS-History-20150717-1334-14.txt: open failed: EACCES (Permission denied)

Then I tried the "Enumerating a device" approach. The code that I added into my app is as follows:

public class MainActivity extends Activity {
   private final String ACTION_USB_PERMISSION= "com.example.android1.USB_PERMISSION";

   private final IntentFilter intentFilterUsb = new IntentFilter();
   UsbManager mUsbManager;
   UsbBroadcastReceiver mReceiverUsb;
   PendingIntent UsbPermissionIntent; 
   UsbDevice device;
   ..................

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

    WiflyMain.mainActivity = this; 

    setContentView(R.layout.form4splashscreenss1); 
    intentFilterUsb.addAction(ACTION_USB_PERMISSION);     
    .............

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);   
    mReceiverUsb = new UsbBroadcastReceiver(mUsbManager);     
    registerReceiver(mReceiverUsb, intentFilterUsb);    
    UsbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION),0);

    //Check to see if the app sees a USB device
    HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList();
    Log.d("tom5","DEVICE LIST = " + deviceList);
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while(deviceIterator.hasNext())
      {
        UsbDevice deviceXX = deviceIterator.next();
        Log.d("tom5","DEVICE = " + deviceXX.toString());
        Log.d("tom5","DEVICE NAME =" + deviceXX.getDeviceName());
      }//while 
    //mUsbManager.requestPermission(device, UsbPermissionIntent);

When I run this code with a USB inserted in the tablet, I get the following Log message:

07-17 13:26:38.770: D/tom5(3613): DEVICE LIST = {}

indicating that the USB device list is empty. Apparently my app does not see the USB stick.

I commented out the call to mUsbManager.requestPermission, since it will crash the app if the device parameter is null.

I didn't include the UsbBroadcastReceiver code here because the app never executes it (except for the constructor code).

Is there anything that I am missing here that would get my app to recognize the USB stick? Any help that you could provide would be much appreciated. Thank you.

OneWorld
  • 17,512
  • 21
  • 86
  • 136

0 Answers0