You need to use the Usb* classes.
Here is some code I cut out of a demo we created.
It reads the data but doesn't do anything more than log it.
Expect to find some cut and paste errors in here. If you are an Android developer, it should get you started.
As I recall, this code originated in a USBTest example.
- Code: Select all
private UsbManager mManager;
private UsbDevice mDevice;
private UsbDeviceConnection mDeviceConnection;
private UsbInterface mInterface;
private UsbEndpoint mEndpointIn;
private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
private final WaiterThread mWaiterThread = new WaiterThread();
public long[] t = new long[3];
public long[] r = new long[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mManager = (UsbManager)getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
// listen for new devices
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
// check for existing devices
for (UsbDevice device : mManager.getDeviceList().values()) {
mInterface = findInterface(device);
if (findEndPoints(mInterface)) {
setUsbInterface(device, mInterface);
break;
}
}
}
private UsbInterface findInterface(UsbDevice device) {
int count = device.getInterfaceCount();
for (int i = 0; i < count; i++) {
UsbInterface intf = device.getInterface(i);
if (device.getVendorId() == 1133) // 3Dx devices
{
return intf;
}
}
return null;
}
protected boolean findEndPoints(UsbInterface intf) {
int nEndPoints = intf.getEndpointCount();
for (int i = 0; i < intf.getEndpointCount(); i++) {
UsbEndpoint ep = intf.getEndpoint(i);
int eptype = ep.getType();
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
} else {
mEndpointIn = ep;
return true;
}
}
}
return false;
}
// Sets the current USB device and interface
private boolean setUsbInterface(UsbDevice device, UsbInterface intf) {
if (mDeviceConnection != null) {
if (mInterface != null) {
mDeviceConnection.releaseInterface(mInterface);
mInterface = null;
}
mDeviceConnection.close();
mDevice = null;
mDeviceConnection = null;
}
if (device != null && intf != null) {
mManager.requestPermission(device, mPermissionIntent);
UsbDeviceConnection connection = mManager.openDevice(device);
if (connection != null) {
if (connection.claimInterface(intf, true)) {
mDevice = device;
mDeviceConnection = connection;
mInterface = intf;
mWaiterThread.start();
return true;
} else {
connection.close();
}
} else {
}
}
return false;
}
public int bytesToInt(byte lsb, byte msb)
{
int x = (((int)(msb) << 8) & 0xff00) | (((int)lsb) & 0x00ff);
// Negative?
if ((x & 0x8000) != 0) x |= 0xffff0000;
return x;
}
private class WaiterThread extends Thread {
public boolean mStop;
public void run() {
while(true) {
byte[] bytes = new byte[7];
int TIMEOUT = 0;
int length = 7;
int result;
result = mDeviceConnection.bulkTransfer(mEndpointIn, bytes, length, TIMEOUT);
// Translation packet comes in before rotation packet. Wait until you have both before
// doing anything with the data
if (bytes[0] == 1) // Translation packet
{
t[0] = bytesToInt(bytes[1], bytes[2]);
t[1] = bytesToInt(bytes[3], bytes[4]);
t[2] = bytesToInt(bytes[5], bytes[6]);
}
else if (bytes[0] == 2) // Rotation packet
{
r[0] = bytesToInt(bytes[1], bytes[2]);
r[1] = bytesToInt(bytes[3], bytes[4]);
r[2] = bytesToInt(bytes[5], bytes[6]);
String dataString = new String();
dataString = String.format("t:%d %d %d r: %d %d %d ", t[0],t[1],t[2],r[0],r[1],r[2]);
log(dataString);
}
else if (bytes[0] == 3) // Button packet
{
}
}
}
}
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = device.getDeviceName();
if (mDevice != null && mDevice.equals(deviceName)) {
}
} else if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
}
}
else {
}
}
}
}
};
}
}