Search code examples
javaandroidraspberry-piandroid-thingsadafruit

Android Things & Adafruit Ultimate GPS Breakout v3


I am starting to play with the Adafruit Ultimate GPS Breakout v3 with Android Things v1.0.1 on the Raspberry Pi 3. I am using the code here: Github Contrib Drivers to try and get the GPS co-ordinates but I am not having any luck so I'm hoping somebody could point me in the right direction.

The GpsModuleCallBack() isn't showing anything in the log.

Here is my code:

MainActivity.java

public class MainActivity extends Activity {

private static final String UART_DEVICE_NAME = "UART0";

UartDevice mDevice;
NmeaGpsModule mGpsModule;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("GPS Test");

    PeripheralManager gpiolist = PeripheralManager.getInstance();
    List<String> portList = gpiolist.getGpioList();
    if (portList.isEmpty()) {
        Log.w(TAG, "No GPIO port available on this device.");
    } else {
        Log.i(TAG, "List of available ports: " + portList);
    }

    PeripheralManager uartlist = PeripheralManager.getInstance();
    List<String> deviceList = uartlist.getUartDeviceList();
    if (deviceList.isEmpty()) {
        Log.w(TAG, "No UART port available on this device.");
    } else {
        Log.i(TAG, "List of available devices: " + deviceList);
    }


    // Attempt to access the UART device
    try {
        PeripheralManager gpsmanager = PeripheralManager.getInstance();
        mDevice = gpsmanager.openUartDevice(UART_DEVICE_NAME);
        Log.i(TAG, "Accessed " + UART_DEVICE_NAME );
    } catch (IOException e) {
        Log.w(TAG, "Unable to access UART device", e);
    }

    try {
        mGpsModule = new NmeaGpsModule(
               "UART0", 9600, 1.8f);

        mGpsModule.setGpsModuleCallback(new GpsModuleCallback() {
            @Override
            public void onGpsSatelliteStatus(GnssStatus status) {
                Log.i(TAG, "Status: " + status.getSatelliteCount());
            }

            @Override
            public void onGpsTimeUpdate(long timestamp) {
                Log.i(TAG, "Time: " + timestamp);
            }

            @Override
            public void onGpsLocationUpdate(Location location) {
                Log.i(TAG, "Location: " + location.getLatitude() + location.getLongitude());
            }

            @Override
            public void onNmeaMessage(String nmeaMessage) {
                Log.i(TAG, "NMEA Message: " + nmeaMessage);
            }
        });

    } catch (IOException e) {
        // couldn't configure the gps module...
    }

    // Close the GPS module when finished:

    try {
        mGpsModule.close();
    } catch (IOException e) {
        // error closing gps module
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mDevice != null) {
        try {
            mDevice.close();
            mDevice = null;
        } catch (IOException e) {
            Log.w(TAG, "Unable to close UART device", e); }
    }
}

Solution

  • The Android Things drivers open the Peripheral ports for you, so you should not be accessing the UART directly from PeripheralManager AND using the driver. Remove the following block of code:

    try {
        PeripheralManager gpsmanager = PeripheralManager.getInstance();
        mDevice = gpsmanager.openUartDevice(UART_DEVICE_NAME);
        Log.i(TAG, "Accessed " + UART_DEVICE_NAME );
    } catch (IOException e) {
        Log.w(TAG, "Unable to access UART device", e);
    }
    

    Most likely, the next block of code in your example is throwing an exception because it's failing to open the UART (it can't be opened twice) but the empty catch block is swallowing it up:

    try {
        mGpsModule = new NmeaGpsModule(
               "UART0", 9600, 1.8f);
        ...
    
    } catch (IOException e) {
        // couldn't configure the gps module...
    }
    

    You should probably add a Log statement there as well just to make sure the there's no exception.