【问题标题】:Send a simple picture through USB Host on AndroidAndroid上通过USB Host发送简单图片
【发布时间】:2017-01-05 17:06:01
【问题描述】:

我已经在https://developer.android.com/guide/topics/connectivity/usb/host.html 上使用代码 sn-ps 尝试了该程序

我能够发现UsbDevice 已连接,但仍然不知道如何建立连接,以便我可以传递一个简单的 jpeg 文件并在接收端接收它。任何指导将不胜感激。

【问题讨论】:

    标签: android android-usb usb-hostcontroller


    【解决方案1】:

    我不确定您到底卡在哪里,但您发布的链接似乎包含所需的所有信息。 一旦您持有您的UsbDevice,您需要请求通信权限(请参阅获取与设备通信的权限)。然后您可以使用以下代码传输数据:

    private Byte[] bytes;  //Convert your jpeg into a byte array and load it in this variable.
    private static int TIMEOUT = 0;
    private boolean forceClaim = true;
    
    ...
    
    UsbInterface intf = device.getInterface(0);
    UsbEndpoint endpoint = intf.getEndpoint(0);
    UsbDeviceConnection connection = mUsbManager.openDevice(device); //this opens the connection
    connection.claimInterface(intf, forceClaim);
    connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //this actually sends the bytes to the other device.
    

    另一方面,您需要将字节数组转换回 jpeg

    See this code for a full sample.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多