下面的流程在非Android O系统版本的手机中运行正常,在Android O连接USB2.0时也运行正常,仅在Android O连接USB3.0/3.1时,第一个指令的发送接收也正常, 然而第二个指令发送就失败了, 返回-1, 尝试过把发送时的bulkTransfer方法的第三个参数(即数据长度)修改为EndPoint的MaXPacketSize, 接收时的bulkTransfer方法的第三个参数修改为接收到数据的大小, 在我的使用中为0x0c, 这样第二个指令就可以正常发送接收, 但是第三个指令还是会发送失败, 可能跟这个有关, 但是最多就发送成功了两个指令
寻找USB设备
val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
//this method just filter the vendorId of device
val dev = lookupCompatibleDevice(usbManager) ?: return
申请权限
if (usbManager.hasPermission(dev)){
//if has permission already
connect(context, dev)
}else {
//request permission and connect device in receiver
registerPermissionReceiver(context)
val mPermissionIntent = PendingIntent.getBroadcast(context, 0, Intent(
ACTION_USB_PERMISSION), 0)
usbManager.requestPermission(de