A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘凯1213 中级黑马   /  2015-9-15 16:08  /  842 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

droid中USB相关的API位于android.hardware.usb包。此外在运行Android 2.3.4的设备上,还有支持USB配件的库。本章关注USB通信的主机模式。有关USB外设模式的信息可参见:http://developer.android.com/guide/topics/connectivity/usb/accessory.html。

在USB的设计中,会有一个设备充当主机。除了其他功能,主机还可以给所连接的设备供电,这就是不需要给USB鼠标添加额外的电池,以及可以使用笔记本电脑上的USB端口给智能手机充电的原因。

Android设备也可以作为USB主机为外部设备供电,这意味着可以把诸如读卡器、指纹扫描仪,以及其他USB外设连接到Android设备上。

要在应用中打开USB通信,首先要定义连接USB设备时需要启动的Activity。下面展示了相应的清单文件。注意metadata元素引用的XML文件,它可以用来选择应用程序要触发的USB设备。

<activity
    android:name=".MyUsbDemo"
    android:label="@string/app_name" >
    <intent-filter>
        <action
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
/>
    </intent-filter>
        <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
</activity>
device_filter.xml文件如下所示。本例会过滤Arduino Uno板,在智能手机上插入这样的USB设备会启动Activity。

<resources>
    <usb-device vendor-id="9025" product-id="67" />
</resources>
下面代码所示的onResume()方法演示了如何从启动Activity的Intent中获取UsbDevice实例。

protected void onResume() {
    super.onResume();
    Intent intent = getIntent();
    UsbDevice device = (UsbDevice) intent.
            getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d(TAG, "Found USB Device: " + device.toString());
new Thread(new UsbCommunciation(device)).start();
}
获取UsbDevice对象后,通过打开连接、声明接口、获取读写终端来与设备通信。下面的例子通过使用buildTransfer()方法不断地往设备中写入同样的消息和读取响应。

private class UsbCommunciation implements Runnable {
    private final UsbDevice mDevice;

    UsbCommunciation(UsbDevice dev) {
        mDevice = dev;
    }

    @Override
    public void run() {
        UsbDeviceConnection usbConnection = mUsbManager.openDevice(mDevice);
        if (!usbConnection.claimInterface(mDevice.getInterface(1),true)) {
            return;
        }
        // 设置Arduino USB串口转换器
        usbConnection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
        usbConnection.controlTransfer(0x21, 32, 0, 0,new byte[]{(byte) 0x80, 0x25, 0x00,0x00, 0x00, 0x00, 0x08},7, 0);

        UsbEndpoint output = null;
        UsbEndpoint input = null;

        UsbInterface usbInterface = mDevice.getInterface(1);
        for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
            if (usbInterface.getEndpoint(i).getType() ==
                UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (usbInterface.getEndpoint(i).getDirection() ==
                    UsbConstants.USB_DIR_IN) {
                    input = usbInterface.getEndpoint(i);
                }
                if (usbInterface.getEndpoint(i).getDirection() ==
                    UsbConstants.USB_DIR_OUT) {
                    output = usbInterface.getEndpoint(i);
                }
            }
        }

    byte[] readBuffer = new byte[MAX_MESSAGE_SIZE];
    while (!mStop) {
        usbConnection.bulkTransfer(output, TEST_MESSAGE,TEST_MESSAGE.length, 0);
        int read = usbConnection.bulkTransfer(input, readBuffer,0, readBuffer.length,0);
        handleResponse(readBuffer, read);
        SystemClock.sleep(1000);
    }
    usbConnection.close();
    usbConnection.releaseInterface(usbInterface);
    }
}
在无线接口不够用时,USB通信会很方便。如果开发者需要为一个新配件开始原型设计,而又没有可以工作的蓝牙或者Wi-Fi栈时,USB会是一个简单的解决方案。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马