Android 用 libusb 操作 USB 设备,无须 root

基本思路:

利用 Android (3.1版本以上)的 USB HOST API 获得 USB 设备的 FileDescriptor,然后libusb 使用 FileDescriptor 打开 USB 设备,当然 libusb 需要做少量修改,后面有代码。

总体的效果就是,用户插入USB 设备,或者启动 Android 系统,你的 App 会根据事先设定的 device filter 自动启动。如果是第一次启动,会询问用户是否授权使用该USB设备。

关键代码(如何使用libusb,请参考官方文档):
获取 FileDescriptor

    // get FileDescriptor by Android USB Host API
    UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
     
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(mContext, 0,
                                                                 new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    mContext.registerReceiver(mUsbReceiver, filter);
     
    int fd = -1;
    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        Log.i(TAG, device.getDeviceName() + " " + Integer.toHexString(device.getVendorId()) +
                   " " + Integer.toHexString(device.getProductId()));
     
        manager.requestPermission(device, mPermissionIntent);
        UsbDeviceConnection connection = manager.openDevice(device);
        if(connection != null){
           fd = connection.getFileDescriptor();
        } else
           Log.e(TAG, "UsbManager openDevice failed");
        break;
    }

接着修改 libusb,我用的是libusbx-1.0.17,主要工作是增加一个 libusb_open_fd 函数,替代原来的 libusb_open,将上一步的 fd 传入即可。
libusbx\libusb\core.c 中在 libusb_open 后面增加 libusb_open_fd 函数

    int LIBUSB_CALL libusb_open_fd(libusb_device *dev, libusb_device_handle **handle, int fd)
    {
    struct libusb_context *ctx = DEVICE_CTX(dev);
    struct libusb_device_handle *_handle;
    size_t priv_size = usbi_backend->device_handle_priv_size;
    int r;
    usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
     
    _handle = malloc(sizeof(*_handle) + priv_size);
    if (!_handle)
    return LIBUSB_ERROR_NO_MEM;
     
    r = usbi_mutex_init(&_handle->lock, NULL);
    if (r) {
    free(_handle);
    return LIBUSB_ERROR_OTHER;
    }
     
    _handle->dev = libusb_ref_device(dev);
    _handle->claimed_interfaces = 0;
    memset(&_handle->os_priv, 0, priv_size);
     
    r = usbi_backend->open_fd(_handle, fd);
    if (r < 0) {
    usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
    libusb_unref_device(dev);
    usbi_mutex_destroy(&_handle->lock);
    free(_handle);
    return r;
    }
     
    usbi_mutex_lock(&ctx->open_devs_lock);
    list_add(&_handle->list, &ctx->open_devs);
    usbi_mutex_unlock(&ctx->open_devs_lock);
    *handle = _handle;
     
    /* At this point, we want to interrupt any existing event handlers so
     * that they realise the addition of the new device's poll fd. One
     * example when this is desirable is if the user is running a separate
     * dedicated libusb events handling thread, which is running with a long
     * or infinite timeout. We want to interrupt that iteration of the loop,
     * so that it picks up the new fd, and then continues. */
    usbi_fd_notification(ctx);
     
    return 0;
    }

在libusbx\libusb\libusb.h中找到libusb_open函数的声明,在其下面添加libusb_open_fd声明:

int LIBUSB_CALL libusb_open_fd(libusb_device *dev, libusb_device_handle **handle, int fd);

在libusbx\libusb\libusbi.h中找到结构体usbi_os_backend,在其内部成员open下面添加

int (*open_fd)(struct libusb_device_handle *handle, int fd);

libusbx\libusb\os\linux_usbfs.c 中在 op_open 后面增加 op_open_fd 函数

    static int op_open_fd(struct libusb_device_handle *handle, int fd) {
    struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
     
    hpriv->fd = fd;
     
    return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
    }

libusbx\libusb\os\linux_usbfs.c 中在 结构体 linux_usbfs_backend 中 .open = op_open 一行后面增加一行

        .open_fd = op_open_fd,


————————————————
版权声明:本文为CSDN博主「葫芦娃二娃」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hubbybob1/article/details/52101356

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值