How to use bluetooth gps module in app?

I am working on an application that uses GPS data. I have an external Bluetooth Bluetooth device, but I cannot find how to use an external Bluetooth Bluetooth module. I added Bluetooth permission to my AndroidManifest file, but I don’t know how to proceed ...

Please, help.

+4
source share
2 answers

You should create a connection to the device as described in the Peter point tutorial.

  • Open the devices and present the list to the user. I assume that you have done this, and now you have the BluetoothDevice device variable installed on your device.
  • Connect as a 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 
  • Get information. You can now open an InputStream from which NMEA messages come in plain text. Thus, you can use BufferedReader for convenience and read messages line by line. Something like that:

     // 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 a real application, each connection to a network, device, or file system resource must be closed when it is not required, errors (exceptions) are properly processed and displayed to the user in a readable and understandable format.

+7
source

Android only supports the Bluetooth RFCOMM protocol (serial emulation). Make sure your GPS supports this protocol.

Then start with the provided Bluetooth guide .

+1
source

All Articles