Android usb enumeration

心已入冬 提交于 2019-12-03 16:03:47

The crash comes when you are trying to access the device with

connection = mManager.openDevice(device);

It crashes because it is throwing a SecurityException because you haven't obtained permission from the user to use the device. It looks like you tried to obtain permission from the user in the line before this

mManager.requestPermission(device, mPermissionIntent);

But you need to understand that the call to requestPermission() is asynchronous. It doesn't return immediately with the permission. What it does is that it shows the user a dialog and asks the user if he will grant permission to your application. Once the user grants or denies permission the dialog is dismissed and the PendingIntent that you passed to requestPermission() is used to broadcast an Intent indicating the permission was granted (or not). You need to listen for this in a registered BroadcastReceiver and when the onReceive() method is called you can then examine the extras in the received Intent and decide how to proceed. Only if the user grants you permission can you move on to call openDevice().

This sounds kinda complicated, but that's the way it works.

Basically, you need to call

mManager.requestPermission(device, mPermissionIntent);

and then wait until permission has been granted before you try to access the device.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!