您的位置:首页 > 移动开发 > Android开发

android:与USB设备通信

2013-01-30 16:48 253 查看

Communicating with a USB device in Android

http://gssdaily.com/forum/viewtopic.php?f=17&t=96084



by
jack » Sat Sep 29, 2012 10:18 pm

Communicating with a USB device in Android

Communication with a USB device can be either synchronous or asynchronous. In either case, you should create a new thread on which to carry out all data transmissions, so you don't block the UI thread. To properly set up communication with a device, you need
to obtain the appropriate UsbInterface and UsbEndpoint of the device that you want to communicate on and send requests on this endpoint with a UsbDeviceConnection.

Code:
Select all
rivate Byte[] bytes

private static int TIMEOUT = 0;

private boolean forceClaim = true;

...

UsbInterface intf = device.getInterface(0);

UsbEndpoint endpoint = intf.getEndpoint(0);

UsbDeviceConnection connection = mUsbManager.openDevice(device);

connection.claimInterface(intf, forceClaim);

connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread


jack Posts: 425Joined: Tue Nov 30, 2010 10:29 pm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: