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

标签 android bluetooth gps external

我正在开发使用 GPS 数据的应用程序。我有一个外置蓝牙GPS设备,但是我找不到如何使用外置GPS蓝牙模块。我在我的 AndroidManifest 文件中添加了蓝牙权限,但我不知道如何继续...

请帮忙。

最佳答案

您应该按照教程 Peter pointed 中的描述创建到设备的连接.

  1. 发现设备并向用户显示一个列表以供其选择。我假设,您已经完成了此操作,并且您现在已为您的设备设置了 BluetoothDevice device 变量。
  2. 作为客户端连接:

    // 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. 检索信息。您现在可以打开 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/

相关文章:

android - 灰度图像在 NDK/C++ 中失去亮度)Android

android - MvvmCross - 将汉堡菜单更改为后退按钮

android - 从应用程序 : Method not found , 收到错误 -32601 Android 中的 Flutter 升级问题

java - Android Studio 等待蓝牙启用

android - 如何在屏幕关闭时保持蓝牙连接?

iphone - 在模拟器中发现MFI蓝牙设备

android - 如何在 Android Webkit 或 Opera Mobile 10 中获取 GPS 位置

linux - 根据数据包类型拆分 TTY 设备

android - 如何在评级栏中给每颗星留出空间

php - 如何用外键存储地理位置?