Android手机无法通过USB主机识别Arduino

时间:2014-03-11 14:13:24

标签: android usb arduino

我正试图通过USB托管从我的HTC Sensation XE读取Arduino的数据。但是,我的手机似乎没有认出Arduino。我的代码遵循Android开发人员网站中列出的常规代码:http://developer.android.com/guide/topics/connectivity/usb/host.html。还有:http://android.serverbox.ch/?p=549

有人可以查看我的代码,看看它是否有问题吗?我没有错误,调试过程显示它只是无法识别设备,因此deviceList始终为空。

我的主要活动:

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>

0 个答案:

没有答案