如何在应用程序中使用蓝牙GPS模块?

4

我正在开发一个应用程序,需要使用GPS数据。我有一个外部蓝牙GPS设备,但是我不知道如何使用这个外部GPS蓝牙模块。我已经将蓝牙权限添加到了我的AndroidManifest文件中,但是我不知道该如何继续操作...

请帮帮我。

2个回答

8

您应该按照Peter的教程所指示的方式创建与设备的连接。

  1. 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.
  2. 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
    
  3. 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!!!
    

请注意:此代码非常简化。在实际应用中,每个与网络、设备或文件系统资源的连接都应在不需要时关闭,错误(异常)应正确处理并以可读和易懂的格式显示给用户。


谢谢,我已经解决了这个问题,但对于其他人来说,这是一个很好的手册 :) - west44

1

Android 只支持 Bluetooth RFCOMM(串行仿真)协议。请确保您的 GPS 支持此协议。

接下来可以从提供的蓝牙教程开始学习。


我的GPS模块与市场提供的软件很好地配合工作,该软件提供了此选项。我已经成功通过蓝牙连接到模块,但是我无法获取纬度、经度、卫星等信息。 - west44

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接