我正在开发一个应用程序,需要使用GPS数据。我有一个外部蓝牙GPS设备,但是我不知道如何使用这个外部GPS蓝牙模块。我已经将蓝牙权限添加到了我的AndroidManifest文件中,但是我不知道该如何继续操作...
请帮帮我。
您应该按照Peter的教程所指示的方式创建与设备的连接。
BluetoothDevice device
variable set to your device.Connect as client:
// This is the default UUID you set for connection - it should work
private static final UUID DEFAULT_SPP_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
// ....
BluetoothSocket bluetoothSocket = device
.createRfcommSocketToServiceRecord(DEFAULT_SPP_UUID);
// ....
bluetoothSocket.connect(); // Do this when you want to start data retrieval
Retrieve information. You can now open an InputStream
, from which NMEA messages come in plain text. So you can use BufferedReader
for convenience and read messages line by line.
Something like this:
// After successful connect you can open InputStream
InputStream in = bluetoothSocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
while (true) {
String nmeaMessage = br.readLine();
Log.d("NMEA", nmeaMessage);
// parse NMEA messages
}
// !!!CLOSE Streams!!!
请注意:此代码非常简化。在实际应用中,每个与网络、设备或文件系统资源的连接都应在不需要时关闭,错误(异常)应正确处理并以可读和易懂的格式显示给用户。