6

First let me state that I am brand new to Android Development so maybe USB is a bit complex for a first app but it is my only reason for wanting to write an app in the first place.

I would like to communicate to a Garmin GPS over USB. I have done so in the past with success from the PC but it was via a driver supplied by Garmin.

As far as I am aware there is no such driver for Android so I need to write directly to the USB.

Garmin publishes this documentation:

http://www8.garmin.com/support/pdf/USBAddendum.pdf

Basically it says you must transmit on bulk out: 00 00 00 00 05 00 00 00 00 00 00 00

to tell the device to prepare for a transfer. When I do so, my bulktransfer fails with -1. If I have a 0 for the timeout then bulktransfer never returns. I assume becuase there is no response from the gps.

I have included my code below. The code detects the GPS and opens it. But the first bulktransfer never completes. I am certain I am sending to the bulk out endpoint. Can anyone get me started?

    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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        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);
            // device should have three endpoints
            if (intf.getEndpointCount() != 3) {
                Log.e(TAG, "could not find endpoint");
                return;
            }           
            // endpoint 0 should be of type interrupt
            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;

            // endpoint 1 should be of type bulk
            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;

            // endpoint 2 should be of type bulk
            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};

                    //14 00 00 00 FE 00 00 00 00 00 00 00
                    int x = connection.bulkTransfer(mEndpointBulkOut,init, init.length, 100);

                    Log.e(TAG, "BulTransfer returned " + x);




                } else {
                    Log.d(TAG, "open FAIL");
                    mConnection = null;
                }
             }
        }

}
4
  • 1
    To help you get answers, it would be useful to supply the LogCat just in case there is any extra debug messages there. I don't know anything about handling USB, but I would ask are you sure that the found endpoint really is the GPS? Oh, and I've just noticed that ep2.getDirection() != UsbConstants.USB_DIR_IN - shouldn't that be `USB_DIR_OUT'? That might explain the hang as it is waiting to read.
    – Ken Y-N
    Apr 12, 2013 at 5:24
  • I think you might have something there on those directions checking.
    – spinnaker
    Apr 12, 2013 at 12:09
  • For someone not knowing anything about usb, you nailed it. It is communicating now. I must have looked at that code 10 times I need to learn not to work till late at night after a long day. :) You should answer my question so you get credit. I will add an answer with my corrected code.
    – spinnaker
    Apr 12, 2013 at 12:27
  • 1
    it's OK, just mark your own answer as accepted - I think you can do that!
    – Ken Y-N
    Apr 13, 2013 at 5:33

1 Answer 1

3

It was a problem with me mixing up my endpoints. Here is the corrected code, now I am having an issue with getting a response but I will probably create another post.

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) {
    // Inflate the menu; this adds items to the action bar if it is present.
    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);
        // device should have three endpoints
        if (intf.getEndpointCount() != 3) {
            Log.e(TAG, "could not find endpoint");
            return;
        }           
        // endpoint 0 should be of type interrupt
        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;

        /*********   Endpoint 0 Bulk Out ************/
        // endpoint 1 should be of type bulk
        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;


        /*************   Endpoint 3 Bulk in *************/
        // endpoint 2 should be of type bulk
        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 input endpoint");
            return;             
        }                


        mEndpointBulkIn = 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};





                //14 00 00 00 FE 00 00 00 00 00 00 00
                int x = connection.bulkTransfer(mEndpointBulkOut,init, init.length, 100);





                Log.e(TAG, "BulTransfer returned " + x);






            } else {
                Log.d(TAG, "open FAIL");
                mConnection = null;
            }
         }
    }

}
1
  • spinnaker,did you get success to receive acknowledge from garmin gps? I'have stucked into same problem for last 2 weeks,I send "start-session" packet but receive no right acknowledge packet from gps.may you share your experiences about this issue?
    – R1349
    Sep 4, 2016 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.