问题描述
我正在开发使用GPS数据的应用。我有一个外部蓝牙GPS设备,但我找不到如何使用外部GPS蓝牙模块。我将蓝牙许可添加到我的AndroidManifest文件中,但是我不知道如何继续...
请帮助。
您应该按照教程Peter 。
- 发现设备并向用户显示列表以选择一个列表。我假设,您已完成此操作,并且您现在已将
BluetoothDevice设备
变量设置为您的设备。//这是您为连接设置的默认UUID - 它应该工作
private static final UUID DEFAULT_SPP_UUID = UUID
.fromString(00001101-0000-1000-8000-00805F9B34FB);
// ....
BluetoothSocket bluetoothSocket = device
.createRfcommSocketToServiceRecord(DEFAULT_SPP_UUID);
// ....
bluetoothSocket.connect(); //当你想开始数据检索时做这件事
-
检索信息。您现在可以打开
InputStream
,NMEA消息以纯文本形式显示。所以你可以使用BufferedReader
来方便读取消息。
类似于这样:
//成功连接后,您可以打开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);
//解析NMEA消息
}
// !!! CLOSE Streams !!!
请记住:简化。在实际应用中,每一次连接到网络,设备或文件系统资源都应该在不需要的时候关闭,错误(例外)可以以可读和可理解的格式正确处理并显示给用户。
I'm working on app, which uses GPS data. I have an external bluetooth GPS device, but I can't find how to use the external GPS Bluetooth module. I added the bluetooth permission into my AndroidManifest file, but I dont know how to continue...
Please help.
You should create a connection to the device as described in tutorial Peter pointed.
- Discover devices and present a list to the user to select one. I assume, you have done this and you now have
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 useBufferedReader
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!!!
REMEMBER: this code is very simplified. In real application every connection to network, device or filesystem resource should be closed when not needed, errors (Exceptions) properly handled and displayed to user in a readable and understandable format.
这篇关于如何在应用中使用蓝牙GPS模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!