5

我正在尝试通过带有 4.2(果冻豆)的 android 设备“nexus 7”通过 USB 与我的 freeduino 板进行通信,该板类似于 arduino uno。这是我的 freeduino 板的照片。 它看起来就像这样

几个月后,我使用开发人员指南与设备进行通信,但没有结果。我想我错过了一些细节。我试图简单地显示我的 freeduino 板的供应商 ID。我的清单文件如下所示。

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.usb_host_final_try.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_ATTACHED" />
    </intent-filter>

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

</manifest>

我在 res/xml 目录中创建了一个文件 device_filter。看起来像这样

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="0403" product-id="07d7" />
</resources>

我的 MainActivity.java 有以下代码。

package com.example.usb_host_final_try;

import java.util.HashMap;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        Intent intent = new Intent();
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        UsbDevice device = deviceList.get("deviceName");
        int vid=device.getVendorId();
        TextView tv = (TextView) findViewById(R.id.textview);
        tv.setText(Integer.toString(vid));
    }

请帮帮我,我从过去几个月开始就搞砸了。我试过这样做,但它也不起作用。

关联

关联

堆栈溢出问题

堆栈溢出问题

我已经尝试了所有这些,但仍然无法成功。我还尝试使用 if else 循环来查看枚举是否有效,如果没有设备将文本设置为未找到的设备,则将文本设置为供应商 ID。在模拟器上显示没有找到设备,但在我的平板电脑上我连接了它强制关闭的设备,否则它仍然显示没有找到任何设备,没有任何连接。

堆栈跟踪在这里..

5104) 01-05 09:10:46.766: E/AndroidRuntime(1712): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-05 09:10:46.766: E/AndroidRuntime(1712): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-05 09:10:46.766: E/AndroidRuntime(1712): ... 11 更多 01-05 09:10:49.104: I/Process(1712) : 发送信号。PID:1712 SIG:9

4

2 回答 2

5

编辑:如果在 XML 中定义,则实际上不需要权限,因此下面的答案不适用。问题是 NPE - 代码中不存在这样的设备“deviceName”。


看起来您没有获得使用 USB 附件的权限,如文档此答案中所述。

在你的onCreate()

UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
private static final String ACTION_USB_PERMISSION =
    "com.android.example.USB_PERMISSION";
...
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);

在你的Activity

private static final String ACTION_USB_PERMISSION =
    "com.android.example.USB_PERMISSION";
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) {
                UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

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

要显示对话框以获取权限:

UsbAccessory accessory;
...
mUsbManager.requestPermission(accessory, mPermissionIntent);
于 2013-01-05T06:58:32.487 回答
1

主要活动现在将变成这样,其余部分保持不变

    package com.example.usb_host_final_try;

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

import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected static final String TAG = null;

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

        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            UsbDevice device = deviceIterator.next();
            String s=device.getDeviceName();
            int pid= device.getProductId();
            int vid = device.getVendorId();
            TextView tv = (TextView) findViewById(R.id.textview);
            tv.setText(s+"\n"+Integer.toString(pid)+"\n"+Integer.toString(vid));
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

多亏了 oleg Vaskevich!

于 2013-01-05T10:57:08.160 回答