gpt4 book ai didi

android - 适用于安卓的 USB 主机

转载 作者:行者123 更新时间:2023-12-04 18:46:02 32 4
gpt4 key购买 nike

我正在尝试通过带有 4.2(果冻 bean )的 android 设备“nexus 7”通过 USB 与我的 freeduino 板进行通信,该板类似于 arduino uno。 This is the photo of my freeduino board. It look exactly like this

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

<?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));
}

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

link

link

Stack overfolow question

stack overflow question

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

堆栈跟踪在这里..

01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.624: D/AndroidRuntime(1658): Shutting down VM 01-05 09:10:35.624: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 01-05 09:10:35.644: E/AndroidRuntime(1658): FATAL EXCEPTION: main 01-05 09:10:35.644: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.os.Looper.loop(Looper.java:137) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-05 09:10:35.644: E/AndroidRuntime(1658): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 09:10:35.644: E/AndroidRuntime(1658): at java.lang.reflect.Method.invoke(Method.java:511) 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-05 09:10:35.644: E/AndroidRuntime(1658): at dalvik.system.NativeStart.main(Native Method) 01-05 09:10:35.644: E/AndroidRuntime(1658): Caused by: java.lang.NullPointerException 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.Activity.performCreate(Activity.java:5104) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-05 09:10:35.644: E/AndroidRuntime(1658): ... 11 more 01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:46.754: D/AndroidRuntime(1712): Shutting down VM 01-05 09:10:46.754: W/dalvikvm(1712): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 01-05 09:10:46.766: E/AndroidRuntime(1712): FATAL EXCEPTION: main 01-05 09:10:46.766: E/AndroidRuntime(1712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.os.Looper.loop(Looper.java:137) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-05 09:10:46.766: E/AndroidRuntime(1712): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 09:10:46.766: E/AndroidRuntime(1712): at java.lang.reflect.Method.invoke(Method.java:511) 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-05 09:10:46.766: E/AndroidRuntime(1712): at dalvik.system.NativeStart.main(Native Method) 01-05 09:10:46.766: E/AndroidRuntime(1712): Caused by: java.lang.NullPointerException 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.Activity.performCreate(Activity.java:5104) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-05 09:10:46.766: E/AndroidRuntime(1712): ... 11 more 01-05 09:10:49.104: I/Process(1712): Sending signal. PID: 1712 SIG: 9

最佳答案

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

您似乎没有获得使用 USB 附件的权限,如 here in the docs 所述在 this answer .

在您的 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);

关于android - 适用于安卓的 USB 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168947/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com