In this tutorial, you’ll learn at last how to connect to a Bluetooth device and how to send data to it…
Profiles and UUID
The Bluetooth standard defines some profiles, i.e. functionalities that Bluetooth devices offer.
To simplify, Bluetooth profiles correspond to “what” a device can do; for example, a Bluetooth headset implements for sure the HSP (Headset Service Profile) profile, that defines how to transmit/receive audio streams and how to send basic commands (“answer”, “turn up the volume”…) to the paired mobile phone. Some advanced headsets implement also the A2DP (Advanced Audio Distribution Profile) profile, allowing the user to listen music streamed from the mobile phone.
When establishing a connection, the device that starts it may use the SDP (Service Discovery Protocol) protocol to discover which services are offered by the other one, that is which profiles it implements.
Each service is defined using a 128 bit numeric identifier (UUID). Usually, a short form of that identifier is used:
- a 128 bit base UUID is defined, 0x00000000-0000-1000-8000-00805F9B34FB
- the short form of the service UUID is inserted replacing the first 8 zeroes
- this is the complete UUID of the service
For example, the HSP service has short UUID 0x1108, therefore it’s complete UUID is
0x00001108-0000-1000-8000-00805F9B34FB
SPP
The simplest profile and most used to communicate with embedded devices is the Serial Port Profile(SPP), with short UUID 0x1101.
This profile emulates a serial link between the two devices:
Android
In the previous tutorials, you learned how to list the devices paired to your smartphone.
Each device corresponds to an instance of the BluetoothDevice object; this object offers two methods to open a communication channel:
- createRfcommSocketToServiceRecord(UUID)
- createInsecureRfcommSocketToServiceRecord(UUID)
Both the two methods require the UUID of the profile and they differ only because the first one creates asecure (encrypted) connection. The most common Bluetooth modules used in embedded applicationsdon’t support this type of connection, so you should use the second method.
If the methods have success, they return a BluetoothSocket object, that represents the communication channel between the smartphone and the paired device; if they fail, an exception is raised.
Let’s see how to send data to a device that implements the SPP profile.
First, define the profile’s UUID:
UUID SPP_UUID = java.util.UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
then, get a communication channel with the device:
BluetoothSocket btSocket = null; try { btSocket = targetDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); } catch (IOException e) { Toast.makeText(this, "Unable to open a serial socket with the device", Toast.LENGTH_SHORT).show(); }
At this point, the channel is not open, use the connect() method to connect to the device:
try { btSocket.connect(); } catch (IOException e) { Toast.makeText(this, "Unable to connect to the device", Toast.LENGTH_SHORT).show(); }
When connected, the BluetoothSocket object offers two Streams, one to send data (OutputStream) and one to receive it (InputStream). For convenience, you can use an OutputStreamWriter object that makes it easier to send characters through a stream:
try { OutputStreamWriter writer = new OutputStreamWriter(btSocket.getOutputStream()); writer.write("Hello World!\r\n"); writer.flush(); } catch (IOException e) { Toast.makeText(this, "Unable to send message to the device", Toast.LENGTH_SHORT).show(); }
The StreamWriter has a local buffer: to be sure that all the data has been sent, remember to call flush() at the end.
Don’t forget to close the socket at the end:
try { btSocket.close(); } catch (IOException e) { Toast.makeText(this, "Unable to close the connection to the device", Toast.LENGTH_SHORT).show(); }
In the next page, I’ll show you an app that implements what explained above…
For more details ,please refer to original post
http://www.lucadentella.it/en/2014/05/02/android-e-bluetooth-4/
Leave a Reply
You must be logged in to post a comment.