Android Things & Adafruit Ultimate GPS Breakout v3

Android Things & Adafruit Ultimate GPS Breakout v3

我开始玩 Adafruit Ultimate GPS Breakout v3 Android Things v1.0.1 on Raspberry Pi 3. 我在这里使用代码:Github Contrib Drivers 尝试并获取 GPS 坐标,但我运气不好,所以我希望有人能给我指出正确的方向。

GpsModuleCallBack() 未在日志中显示任何内容。

这是我的代码:

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); }
    }
}

Android Things 驱动程序为您打开外设端口,因此您不应直接从 PeripheralManager 访问 UART 并使用该驱动程序。删除以下代码块:

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);
}

最有可能的是,您的示例中的下一个代码块抛出异常,因为它无法打开 UART(无法打开两次)但空的 catch 块正在吞噬它:

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

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

您可能还应该在此处添加一个 Log 语句,以确保没有例外。