0

I'm trying to read data for from an Arduino Due to my HTC Sensation XE through USB hosting. However, my phone doesn't seem to recognize the Arduino. My code follows the general code listed in the Android developer website here: http://developer.android.com/guide/topics/connectivity/usb/host.html. Also this: http://android.serverbox.ch/?p=549.

Could someone look at my code to see if there's something wrong with it? I get no errors and the debugging process shows it just doesn't recognize the device so deviceList is always null.

My MainActivity:

package com.imp.racing.dashboard;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Iterator;


public class MainActivity extends ActionBarActivity {
private static final String TAG = "OnReceive";
private static final String ACTION_USB_PERMISSION =     "com.android.example.USB_PERMISSION";

UsbDevice device;
UsbManager mUsbManager;
UsbEndpoint epIN;
UsbDeviceConnection connection;
PendingIntent mPermissionIntent;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    if(savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


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


}

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                        //call method to set up device communication
                        //setUpDataTransfer();
                    }
                }
                else {
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
};



void  setUpDataTransfer(){
    connection = mUsbManager.openDevice(device);
    assert connection != null;
    connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
    connection.controlTransfer(0x21, 32, 0, 0,
            new byte[]{(byte) 0x80, 0x25, 0x00, 0x00,
                    0x00, 0x00, 0x08}, 7, 0
    );

    UsbInterface usbIf = device.getInterface(1);
     //epIN = usbIf.getEndpoint(UsbConstants.USB_ENDPOINT_XFER_BULK);
    epIN = usbIf.getEndpoint(UsbConstants.USB_DIR_IN);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    return id == R.id.action_settings || super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}

public void dataLog(View view){
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator;
    if (deviceList != null) {
        deviceIterator = deviceList.values().iterator();
        for (UsbDevice device : deviceList.values()) {
            device = deviceIterator.next();
            mUsbManager.requestPermission(device, mPermissionIntent);
            String s = device.getDeviceName();
            int pid = device.getProductId();
            int did = device.getDeviceId();
            int vid = device.getVendorId();
            TextView tv = (TextView) findViewById(R.id.device_info);
            tv.setText(s+"\n"+Integer.toString(pid)+"\n"+Integer.toString(vid));
        }
    }


   //Intent log = new Intent(this,DataLogActivity.class);
   //startActivity(log);
}

}

AndroidManifest.xml :

 <?xml version="1.0" encoding="utf-8"?>
 <manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imp.racing.dashboard">
<uses-feature
    android:name="android.hardware.usb.host" />


<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="19" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.imp.racing.dashboard.MainActivity"
        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_ATTfACHED" />
        </intent-filter>

        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />


    </activity>

    <activity
        android:name="com.imp.racing.dashboard.DataLogActivity"
        android:label="@string/title_activity_data_log"
        android:parentActivityName="com.imp.racing.dashboard.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.imp.racing.dashboard.MainActivity" />
    </activity>
</application>

device_filter.xml :

 <?xml version="1.0" encoding="utf-8"?>

 <resources>
    <usb-device vendor-id="2341"
                product-id="003D"
                             />
 </resources>
user2766109
  • 95
  • 1
  • 9
  • 1
    Do you have proper cabling to put the phone in OTG mode? Are you sure that is the correct VID & PID? Do you know for a fact that your phone has OTG support? A little web searching suggests it may not (in factory configuration) so perhaps you should test with some other hardware and software to verify that. – Chris Stratton Mar 11 '14 at 14:27
  • Thanks for the info. I used the Nexus 5 and everything seems to work well. – user2766109 Mar 11 '14 at 17:41
  • Certain devices do not contain the required files to connect to devices on USB-Host, the following answer has fixed this problem on many devices but requires root: http://stackoverflow.com/a/11992683/2698179 – keag Jun 23 '14 at 19:39

0 Answers0