Android 10 USB主机-无法连接到设备,未授予权限(但应该是)

时间:2020-05-11 16:29:59

标签: android usb android-10.0

我一直遵循此处的指导,以便以Android手机为主机连接到USB设备。 https://developer.android.com/guide/topics/connectivity/usb/host.html

从下面的段落中,我相信,只要我为USB_Device_Attached操作正确设置了一个意图过滤器,并为我的设备过滤了-我应该已经拥有连接到它的所有权限。

Note: If your application uses an intent filter to discover USB devices as they're connected, it automatically receives permission if the user allows your application to handle the intent. If not, you must request permission explicitly in your application before connecting to the device.

我的简单示例:

public class MainActivity extends Activity {
    private final static String TAG = "USB_HOST_EXAMPLE";
    UsbManager usbManager;
    UsbDeviceConnection connection;

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

        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        Intent intent = getIntent();
        if (intent != null) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                Log.w(TAG, "Started activty from intent filter, using device: " + device.getProductName());
                connection = usbManager.openDevice(device);
            }
        }
    }
}

当前行为:

  1. 应用已关闭
  2. 连接USB设备
  3. 弹出窗口询问我是否要打开我的应用程序以使用此USB设备。 (因此我的意图过滤器必须正常工作
  4. 应用打开。
  5. 应用程序将连接到设备。
  6. 应用无法连接,出现以下异常:
 java.lang.SecurityException: User has not given 10277/com.example.basic_usb_host permission to access device /dev/bus/usb/001/002

我也尝试枚举设备并请求权限,如here所述,但是当我找到我的USB设备并去请求权限时,在调用< / p>

usbManager.requestPermission(device, permissionIntent);

不显示,广播接收器返回

permission denied for device UsbDevice.....

我使用的是Android 10,并且在Pixel 2和Pixel 3a(植根于3a)上也遇到了同样的问题。

0 个答案:

没有答案