Introduction
- Get the InputStream and outPutStream that handle transmission through the socket via getInputstream() and getOutputStream, respectively.
- For reading and writing data to the streams with read(byte[ ]) and write([ ]).
- private class ConnectedThread extends Thread {
- private final BluetoothSocket mmSocket;
- private final InputStream mmInStream;
- private final OutputStream mmOutStream;
- public ConnectedThread(BluetoothSocket socket) {
- mmSocket = socket;
- InputStream tmpIn = null;
- OutputStream tmpOut = null;
- // Get the input and output streams, using temp objects because
- // member streams are final
- try {
- tmpIn = socket.getInputStream();
- tmpOut = socket.getOutputStream();
- } catch (IOException e) { }
- mmInStream = tmpIn;
- mmOutStream = tmpOut;
- }
- public void run() {
- byte[] buffer = new byte[1024]; // buffer store for the stream
- int bytes; // bytes returned from read()
- // Keep listening to the InputStream until an exception occurs
- while (true) {
- try {
- // Read from the InputStream
- bytes = mmInStream.read(buffer);
- // Send the obtained bytes to the UI activity
- mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
- .sendToTarget();
- } catch (IOException e) {
- break;
- }
- }
- }
- /* Call this from the main activity to send data to the remote device */
- public void write(byte[] bytes) {
- try {
- mmOutStream.write(bytes);
- } catch (IOException e) { }
- }
- /* Call this from the main activity to shutdown the connection */
- public void cancel() {
- try {
- mmSocket.close();
- } catch (IOException e) { }
- }
- }
Analysis
Exploring the profiles
Headset
A2DP
Health Device
- First get the default adapter, as described in Setting Up Bluetooth.
- Use getProfileProxy() to establish a connection to the profile proxy object associated with the profile.
- We must Set up a BluetoothProfile.ServiceListener. This listener will notify BluetoothProfile IPC clients when they have been connected to or disconnected from the service.
- Then, In onServiceConnected(), get a handle to the profile proxy object.
- Once you have the profile proxy object, we can use it to monitor the state of the connection and perform other operations that are relevant to that profile.
- Let us have a look at the code below to understand how to connect to a BluetoothHeadset proxy object so that you can control the Headset profile.
Code
- BluetoothHeadset mBluetoothHeadset;
- // Get the default adapter
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // Establish connection to the proxy.
- mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
- private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
- public void onServiceConnected(int profile, BluetoothProfile proxy) {
- if (profile == BluetoothProfile.HEADSET) {
- mBluetoothHeadset = (BluetoothHeadset) proxy;
- }
- }
- public void onServiceDisconnected(int profile) {
- if (profile == BluetoothProfile.HEADSET) {
- mBluetoothHeadset = null;
- }
- }
- };
- // ... call functions on mBluetoothHeadset
- // Close proxy connection after use.
- mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
Creating an HDP Application
- Get a reference to the BluetoothHealth proxy object
Similar to a regular headset and A2DP profile devices as we already have seen above, one must call getProfileProxy() with BluetoothProfile. ServiceListener and the HEALTH profile types to establish a connection with the profile proxy object.
- Create a BluetoothHealthCallback and register an application configuration (BluetoothHealthAppConfiguration) that acts as a health sink in the Android mechanism procedure.
- Establish a connection to a health device. Some devices will initiate the connection. It is unnecessary to carry out this step for those devices.
- When connected successfully to a health device, read/write to the health device using the file descriptor. The received data needs to be interpreted using a health manager that implements the IEEE 11073-xxxxx specifications.
- When done, close the health channel and unregister the application. The channel also closes when there is extended inactivity.

Tom MohanPosted Mar 5, 2015, 10:36 PM
Second part also nice one.