android连接usb外设通讯_Android O连接USB3.0接口的外设

下面的流程在非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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值