Introduction

In the previous article, we saw that one is the client and another is the server because we are following the client and server architecture. We are very familiar with the traditional client and server architecture in which one is the server and another one is the client. Using the MAC address of the devices we can do the communication. Capable hardware can be simulated by this capable software, in other words, an Android application.
After the successful connection between two devices, each one will have a connected BluetoothSocket. The method we will use to transfer the data is the same as what we do in networking concepts of Java. Let us have a look at the points I will explain.
Note: the read() and write() methods are blocking calls. read(byte[]) will block the call until there is something to read from the stream. These methods just block the flow of control if the remote or any associated device is not calling.
Let us have a look at the example that shows the code as in the following.
Code
  1. private class ConnectedThread extends Thread {
  2. private final BluetoothSocket mmSocket;
  3. private final InputStream mmInStream;
  4. private final OutputStream mmOutStream;
  5. public ConnectedThread(BluetoothSocket socket) {
  6. mmSocket = socket;
  7. InputStream tmpIn = null;
  8. OutputStream tmpOut = null;
  9. // Get the input and output streams, using temp objects because
  10. // member streams are final
  11. try {
  12. tmpIn = socket.getInputStream();
  13. tmpOut = socket.getOutputStream();
  14. } catch (IOException e) { }
  15. mmInStream = tmpIn;
  16. mmOutStream = tmpOut;
  17. }
  18. public void run() {
  19. byte[] buffer = new byte[1024]; // buffer store for the stream
  20. int bytes; // bytes returned from read()
  21. // Keep listening to the InputStream until an exception occurs
  22. while (true) {
  23. try {
  24. // Read from the InputStream
  25. bytes = mmInStream.read(buffer);
  26. // Send the obtained bytes to the UI activity
  27. mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
  28. .sendToTarget();
  29. } catch (IOException e) {
  30. break;
  31. }
  32. }
  33. }
  34. /* Call this from the main activity to send data to the remote device */
  35. public void write(byte[] bytes) {
  36. try {
  37. mmOutStream.write(bytes);
  38. } catch (IOException e) { }
  39. }
  40. /* Call this from the main activity to shutdown the connection */
  41. public void cancel() {
  42. try {
  43. mmSocket.close();
  44. } catch (IOException e) { }
  45. }
  46. }

    Analysis

    Here the constructor acquires the necessary streams and once executed the thread is waiting for data to come through the InputStream. The thread cancel() method is important so that the connection can be terminated at any time by closing the BluetoothSocket. This should always be called when you are done using the Bluetooth connection.

    Exploring the profiles

    Bluetooth support for profiles was added by the Android version 3.0 Bluetooth API. According to Google, a Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. An example is a Hands-Free profile. For a mobile phone to connect to a wireless headset, both devices must support the Hands-Free profile.
    The Interface BluetoothProfile can be implemented in their own classes to support a specific profile, a Bluetooth profile. The Android Bluetooth API provides implementations for the following Bluetooth profiles.

    Headset

    The Headset profile provides support for Bluetooth headsets to be used with mobile phones. Android provides the BluetoothHeadset class for use as a proxy for controlling the Bluetooth Headset Service via Inter-Process Communication (IPC). This includes both Bluetooth Headset and Hands-Free (v1.5) profiles. The BluetoothHeadset class includes support for AT commands. For more discussion of this topic, see Vendor-specific AT commands.

    A2DP

    The Advanced Audio Distribution Profile (A2DP) profile defines how high-quality audio can be streamed from one device to another over a Bluetooth connection. Android provides the BluetoothA2dp class that is a proxy for controlling the Bluetooth A2DP Service via IPC that is quite interesting.

    Health Device

    Let us consider the fact that devices are used for a heart pressure checker and blood pressure checker using the Bluetooth protocol called health device. Note that these values are also referenced in the ISO/IEEE 11073-20601 [7] specification as MDC_DEV_SPEC_PROFILE_* in the Nomenclature Codes.
    Working with a Profile
    Code
    1. BluetoothHeadset mBluetoothHeadset;
    2. // Get the default adapter
    3. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    4. // Establish connection to the proxy.
    5. mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
    6. private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    7. public void onServiceConnected(int profile, BluetoothProfile proxy) {
    8. if (profile == BluetoothProfile.HEADSET) {
    9. mBluetoothHeadset = (BluetoothHeadset) proxy;
    10. }
    11. }
    12. public void onServiceDisconnected(int profile) {
    13. if (profile == BluetoothProfile.HEADSET) {
    14. mBluetoothHeadset = null;
    15. }
    16. }
    17. };
    18. // ... call functions on mBluetoothHeadset
    19. // Close proxy connection after use.
    20. mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
    Creating an HDP Application
    Here is the basic procedure involved in creating an Android HDP application:

    Summary

    This article illustrates the management of the connections and the profiles of Bluetooth. This article shows the connection between the devices along with their profiles and the basics of streaming data using Android.