我开始在Raspberry Pi 3上使用Adafruit Ultimate GPS Breakout v3和Android Things v1.0.1。我在这里使用代码: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); }
}
}
答案 0 :(得分:1)
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
语句,以确保那里没有例外。