Recipe Opening a Bluetooth Socket

To establish a Bluetooth connection with another device, the application needs to implement either the client-side or server-side socket. After the server and client are bonded, there is a connected Bluetooth socket for each device on the same RFCOMM (Bluetooth transport protocol). However, the client device and service device obtain the Bluetooth socket in different ways.The server receives the Bluetooth socket instance when an incoming connection is accepted. The client receives the instance when it opens an RFCOMM channel to the server.

Server-side initialization uses the generic client-server programming model with applications requiring an open socket for accepting incoming requests (similar to TCP). The interface BluetoothServerSocket should be used to create a server listening port. After the connection is accepted, a BluetoothSocket is returned and can be used to manage the connection.

The BluetoothServerSocket can be obtained from the BluetoothAdapter instance's method listenUsingRfcommWithServiceRecord() .After obtaining the socket, the accept() method starts listening for a request and returns only when either a connection has been accepted or an exception has occurred. The BluetoothSocket then returns when accept() returns a valid connection. Finally, the close() method should be called to release the server socket and its resources because RFCOMM allows only one connected client per channel at a time.This does not close the connected BluetoothSocket. The following excerpt shows how these steps are done:

BluetoothServerSocket myServerSocket

= myBluetoothAdapter.listenUsingRfcommWithServiceRecord(name, uuid); myServerSocket.accept(); myServerSocket.close();

Note that the accept() method is a blocking call and so it should not be implemented inside the main thread. It is better idea to implement this inside a working thread, as shown in Listing 7.11.

Listing 7.11 Example of Establishing a Bluetooth Socket private class AcceptThread extends Thread {

private final BluetoothServerSocket mmServerSocket;

public AcceptThread() {

// Use a temporary object that is later assigned // to mmServerSocket, because mmServerSocket is final BluetoothServerSocket tmp = null; try {

// MY_UUID is the app's UUID string, also used by the client tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID); } catch (IOException e) { } mmServerSocket = tmp;

BluetoothSocket socket = null;

// Keep listening until exception occurs or a socket is returned while (true) { try {

socket = mmServerSocket.accept(); } catch (IOException e) { break;

// If a connection was accepted if (socket != null) {

// Do work to manage the connection (in a separate thread)




/** Will cancel the listening socket, and cause thread to finish */ public void cancel() { try {

mmServerSocket.close(); } catch (IOException e) { }

To implement the client device mechanism, the BluetoothDevice needs to be obtained from the remote device.Then the socket needs to be retrieved to make the connection.To retrieve the BluetoothSocket, use the BluetoothDevice method createRfcommSocketToServiceRecord(UUID) with the UUID used in listenUsingRfcommWithServiceRecord.After the socket is retrieved, the connect() method can be used to initiate a connection. This method is also blocking and should also be implemented in a separate thread, as shown in Listing 7.12.

Listing 7.12 Example of Connecting to a Bluetooth Socket private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {

// Use a temporary object that is later assigned to mmSocket, // because mmSocket is final BluetoothSocket tmp = null; mmDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice try {

// MY_UUID is the app's UUID string, also used by the server code tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } mmSocket = tmp;

// Cancel discovery because it will slow down the connection mAdapter.cancelDiscovery();

// Connect the device through the socket. This will block // until it succeeds or throws an exception mmSocket.connect(); } catch (IOException connectException) {

// Unable to connect; close the socket and get out try {

mmSocket.close(); } catch (IOException closeException) { } return;

// Do work to manage the connection (in a separate thread) manageConnectedSocket(mmSocket);

/** Will cancel an in-progress connection, and close the socket */ public void cancel() { try {

After the connection is established, the normal InputStream and OutputStream can be used to read and send data between the Bluetooth devices.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


    What is name and my_uuid in bluetooth rfcomm?
    3 years ago

Post a comment