gpt4 book ai didi

android - 如何与USB设备通信?

转载 作者:可可西里 更新时间:2023-11-01 19:01:13 27 4
gpt4 key购买 nike

我正在尝试建立 USB 通信作为主机。我在这里遵循示例 http://developer.android.com/guide/topics/usb/host.html但我无法正常工作。这是我的代码:

private static final String ACTION_USB_PERMISSION = "com.multitools.andres.LCView";
UsbDevice device;
//Pide permisos al usuario para comunicacion con el dispositivo USB
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
//call method to set up device communication
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(DEBUG) Log.i(TAG, "onCreate() -> MainMenu");

actionBar = getActionBar(); //obtengo el ActionBar
actionBar.setDisplayHomeAsUpEnabled(true); //el icono de la aplicacion funciona como boton HOME
//Menu
setListAdapter(new ArrayAdapter<String>(MainMenu.this, android.R.layout.simple_list_item_1, MenuNames));

//USB
if(DEBUG) Log.i(TAG, "Setting UsbManager -> MainMenu");
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent mPermissionIntent;

if(DEBUG) Log.i(TAG, "Setting PermissionIntent -> MainMenu");
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
if(DEBUG) Log.i(TAG, "Setting IntentFilter -> MainMenu");
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
if(DEBUG) Log.i(TAG, "Setting registerReceiver -> MainMenu");
registerReceiver(mUsbReceiver, filter);
if(DEBUG) Log.i(TAG, "Setting requestPermission -> MainMenu");
mUsbManager.requestPermission(device, mPermissionIntent);

}

当我取消注释 mUsbManager.requestPermission(device, mPermissionIntent); 行时,我会看到 Force Close 对话框 如果我对其进行注释,我不会强制关闭,但它不起作用。我认为问题出在:

private static final String ACTION_USB_PERMISSION = "com.multitools.andres.LCView";

在谷歌的例子中是这样的:

private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

但我不知道我到底要放什么,也找不到任何解释。我必须放在那里或我的错误在哪里?这是我启动应用程序时得到的 LogCat:

04-17 00:57:50.944: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:51.331: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:51.981: I/(1362): onCreate() -> MainMenu
04-17 00:57:52.013: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:52.151: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:52.570: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:52.731: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:53.122: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:53.231: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:53.390: I/(1362): Setting UsbManager -> MainMenu
04-17 00:57:53.451: I/(1362): Setting PermissionIntent -> MainMenu
04-17 00:57:53.470: I/(1362): Setting IntentFilter -> MainMenu
04-17 00:57:53.470: I/(1362): Setting registerReceiver -> MainMenu
04-17 00:57:53.511: I/(1362): Setting requestPermission -> MainMenu
04-17 00:57:53.660: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:53.791: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:54.311: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:54.401: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:54.531: I/(1362): onCreateOptionsMenu() -> MainMenu
04-17 00:57:54.683: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:54.772: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:55.186: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:55.291: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:55.661: I/dalvikvm(1362): threadid=3: reacting to signal 3
04-17 00:57:55.751: I/dalvikvm(1362): Wrote stack traces to '/data/anr/traces.txt'
04-17 00:57:55.791: D/gralloc_goldfish(1362): Emulator without GPU emulation detected.
04-17 01:11:47.323: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:47.720: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:48.124: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:48.291: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:48.452: I/(1459): onCreate() -> MainMenu
04-17 01:11:48.691: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:48.813: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:49.172: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:49.321: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:49.653: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:49.821: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:49.901: I/(1459): Setting UsbManager -> MainMenu
04-17 01:11:49.931: I/(1459): Setting PermissionIntent -> MainMenu
04-17 01:11:50.021: I/(1459): Setting IntentFilter -> MainMenu
04-17 01:11:50.031: I/(1459): Setting registerReceiver -> MainMenu
04-17 01:11:50.051: I/(1459): Setting requestPermission -> MainMenu
04-17 01:11:50.071: D/AndroidRuntime(1459): Shutting down VM
04-17 01:11:50.133: W/dalvikvm(1459): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-17 01:11:50.231: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:50.331: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:50.401: E/AndroidRuntime(1459): FATAL EXCEPTION: main
04-17 01:11:50.401: E/AndroidRuntime(1459): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.multitools.andres/com.multitools.andres.MainMenu}: java.lang.NullPointerException
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.os.Looper.loop(Looper.java:137)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-17 01:11:50.401: E/AndroidRuntime(1459): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:11:50.401: E/AndroidRuntime(1459): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 01:11:50.401: E/AndroidRuntime(1459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 01:11:50.401: E/AndroidRuntime(1459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 01:11:50.401: E/AndroidRuntime(1459): at dalvik.system.NativeStart.main(Native Method)
04-17 01:11:50.401: E/AndroidRuntime(1459): Caused by: java.lang.NullPointerException
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.os.Parcel.readException(Parcel.java:1333)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.os.Parcel.readException(Parcel.java:1281)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.hardware.usb.IUsbManager$Stub$Proxy.requestDevicePermission(IUsbManager.java:535)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.hardware.usb.UsbManager.requestPermission(UsbManager.java:361)
04-17 01:11:50.401: E/AndroidRuntime(1459): at com.multitools.andres.MainMenu.onCreate(MainMenu.java:80)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.Activity.performCreate(Activity.java:4465)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-17 01:11:50.401: E/AndroidRuntime(1459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-17 01:11:50.401: E/AndroidRuntime(1459): ... 11 more
04-17 01:11:50.751: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:50.851: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:51.331: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:51.403: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'
04-17 01:11:51.774: I/dalvikvm(1459): threadid=3: reacting to signal 3
04-17 01:11:51.961: I/dalvikvm(1459): Wrote stack traces to '/data/anr/traces.txt'

谢谢你:)

最佳答案

该字符串只是一个标记,因此您可以识别调用 registerReceiver(mUsbReceiver, filter); 时返回的 Intent 。 .那不是问题所在。

我觉得问题出在这里:

UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

这条线应该让你成为你的 USB 管理器,但据我所知,并不是所有的手机都必须支持 Android USB Accessory API。这一行可能只是分配 nullmUsbManager然后导致你NullPointerException当您在代码中进一步调用方法时。尝试检查这是否不是 null在打电话之前。

有关更多信息,请查看以下链接:

  1. http://developer.android.com/guide/topics/usb/index.html
  2. http://developer.android.com/guide/topics/usb/accessory.html

编辑:

我想我明白现在的问题是什么了。没错,它不是 USB 管理器。它是 UsbDevice 对象 ( device )。它永远不会在您的代码中的任何地方初始化。在这一行中:

mUsbManager.requestPermission(device, mPermissionIntent);

您基本上是在发出一个 Intent ,询问用户您是否可以使用由 UsbDevice 对象表示的设备 device .然而,当这个电话被解雇时 device尚未初始化(因此默认值为 null )。因此,当您尝试请求许可时,您最终会得到 NullPointerException。而不是预期的结果。要解决此问题,您需要确定要连接的设备并将其分配给 device .找here ,在“枚举设备”下找出不同的方法来做到这一点。如果您知道要连接的设备的名称,一种方法是在检索 Usb 管理器后进行以下调用:

HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
device = deviceList.get("<deviceName>");

(显然,您会将 <deviceName> 替换为设备的实际名称。

关于android - 如何与USB设备通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183794/

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