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.
source share