我一直遵循此处的指导,以便以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);
}
}
}
}
当前行为:
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)上也遇到了同样的问题。