我正在开发使用 GPS 数据的应用程序。我有一个外置蓝牙GPS设备,但是我找不到如何使用外置GPS蓝牙模块。我在我的 AndroidManifest 文件中添加了蓝牙权限,但我不知道如何继续...
请帮忙。
最佳答案
您应该按照教程 Peter pointed 中的描述创建到设备的连接.
- 发现设备并向用户显示一个列表以供其选择。我假设,您已经完成了此操作,并且您现在已为您的设备设置了
BluetoothDevice device
变量。 作为客户端连接:
// 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
检索信息。您现在可以打开
InputStream
,NMEA 消息从中以纯文本形式出现。因此,为了方便起见,您可以使用BufferedReader
逐行读取消息。 像这样:// 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!!!
记住:这段代码非常简单。在实际应用中,每个与网络、设备或文件系统资源的连接都应在不需要时关闭,正确处理错误(异常)并以可读易懂的格式向用户显示。
关于android - 如何在应用程序中使用蓝牙 GPS 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6910817/