First, let me say that I'm completely new to Android Development, so maybe USB is a little harder for the first application, but this is the only reason I want to write the application in the first place.
I would like to communicate with the Garmin GPS Navigator via USB. I have done this in the past with success with a PC, but it was through the driver provided by Garmin.
As far as I know, there is no such driver for Android, so I need to write directly to USB.
Garmin publishes this documentation:
http://www8.garmin.com/support/pdf/USBAddendum.pdf
This basically means that you should pass by and large: 00 00 00 00 00 05 00 00 00 00 00 00
inform the device that it is ready to transfer. When I do this, my bulktransfer fails with -1. If I have 0 for a timeout, then bulktransfer will never return. I assume there is no answer from gps.
I have included my code below. The code detects the GPS and opens it. But the first bulktransfer never ends. I am sure that I am sending to the main exit. Can anyone start me?
public class MainActivity extends Activity {
private static final String TAG = "TestGarmin";
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbEndpoint mEndpointIntr;
private UsbEndpoint mEndpointBulkOut;
private UsbEndpoint mEndpointBulkIn;
private static int TIMEOUT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onResume() {
super.onResume();
Intent intent = getIntent();
Log.d(TAG, "intent: " + intent);
String action = intent.getAction();
String s = UsbManager.ACTION_USB_DEVICE_ATTACHED;
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
setDevice(device);
}
private void setDevice(UsbDevice device) {
Log.d(TAG, "setDevice " + device);
if (device.getInterfaceCount() != 1) {
Log.e(TAG, "could not find interface");
return;
}
UsbInterface intf = device.getInterface(0);
if (intf.getEndpointCount() != 3) {
Log.e(TAG, "could not find endpoint");
return;
}
UsbEndpoint ep0 = intf.getEndpoint(0);
if (ep0.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
Log.e(TAG, "endpoint 0 is not interrupt type");
return;
}
if (ep0.getDirection() != UsbConstants.USB_DIR_IN) {
Log.e(TAG, "endpoint 0 is not an input endpoint");
return;
}
mEndpointIntr = ep0;
UsbEndpoint ep1 = intf.getEndpoint(1);
if (ep1.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
Log.e(TAG, "endpoint 1 is not bulk type");
return;
}
if (ep1.getDirection() != UsbConstants.USB_DIR_OUT) {
Log.e(TAG, "endpoint 1 is not an output endpoint");
return;
}
mEndpointBulkOut = ep1;
UsbEndpoint ep2 = intf.getEndpoint(2);
if (ep2.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
Log.e(TAG, "endpoint 2 is not bulk type");
return;
}
if (ep2.getDirection() != UsbConstants.USB_DIR_IN) {
Log.e(TAG, "endpoint 2 is not an output endpoint");
return;
}
mEndpointBulkOut = ep2;
mDevice = device;
if (device != null) {
UsbDeviceConnection connection = mUsbManager.openDevice(device);
if (connection != null && connection.claimInterface(intf, true)) {
Log.d(TAG, "open SUCCESS");
mConnection = connection;
byte[] init = {0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
int x = connection.bulkTransfer(mEndpointBulkOut,init, init.length, 100);
Log.e(TAG, "BulTransfer returned " + x);
} else {
Log.d(TAG, "open FAIL");
mConnection = null;
}
}
}
}