USB opendevice 即使有权限也会失败 - HostAPI 受限

USB opendevice fails even if it has permission - HostAPI is restricted

我正在尝试使用 Android USB 主机 API 连接 Android 和 Arduino UNO。 我发现使用 intent-filter 的设备。 我获得了适当的 UsbInterfaceUsbEndpoint。在我尝试打开 UsbDeviceConnection 之前,一切似乎都很好,尽管已授予权限 ,但它总是失败。 什么原因?

下面的片段:

       @Override
       protected void onResume() {
           super.onResume();
           usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
           Intent intent = getIntent();
           String action = intent.getAction();
           if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
               device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
           }
       }

       public boolean connect(UsbDevice device){

       [...] // search for appropriate interfaces/endpoints

       Log.i(TAG, "usbInEndpoint = " + usbInEndpoint.toString());
       Log.i(TAG, "usbOutEndpoint = " + usbOutEndpoint.toString());
       Log.i(TAG, "usbInterface = " + usbInterface.toString());
       Log.i(TAG, "permission = " + usbManager.hasPermission(device));

       usbDeviceConnection = usbManager.openDevice(device); 
       Log.i(TAG, "usbDeviceConnection = " + usbDeviceConnection));

       [...]

       }

Logcat 摘录如下:

usbInEndpoint = UsbEndpoint[mAddress=131,mAttributes=2,mMaxPacketSize=64,mInterval=1]
usbOutEndpoint = UsbEndpoint[mAddress=4,mAttributes=2,mMaxPacketSize=64,mInterval=1]
usbInterface = UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[
    UsbEndpoint[mAddress=4,mAttributes=2,mMaxPacketSize=64,mInterval=1]
    UsbEndpoint[mAddress=131,mAttributes=2,mMaxPacketSize=64,mInterval=1]]
permission = true

D/UsbService: openDevice(/dev/bus/usb/001/002) : HostAPI is restricted

usbDeviceConnection = null

编辑

我编辑了问题和 Logcat 摘录,因为我注意到 UsbService 在我调用 opendevice 后记录 "Host API is restricted",所以我猜这是一种设备安全设置。如何检查?我用的是三星 A8

我回答我自己的问题,希望这对遇到同样问题的其他人有所帮助。这是因为我的手机由 MDM 管理。