Introduction
People always feel happy in sharing in any form. I have seen people's face when they share something, not in the case of Information Technology but in the real world as well. People always feel happy in sharing the magical moments of life in the form of the pictures and some other forms.
Let us consider the sharing of the data by two friends using Android devices. These friends will care only about the data, not with the mechanism of the Bluetooth technology but when in the case of development these things become important, let us see how.
Now the sharing of the data is done using Bluetooth and many other technologies in Android-enabled devices. What will we do in those cases?
We must make one the client and another the server because we are following the client and sever architecture. We are very well familiar with the traditional client and server architecture in which one is the server and another is the client. Using a MAC address of the devices we can do the communication.
Connecting as a server
When connecting two devices one must be the server by holding an open BluetoothServerSocket. First the data will be written on the socket and thus maintain the communication. When the BluetoothSocket is acquired from the BluetoothServerSocket, the BluetoothServerSocket can be discarded, unless you want to accept more connections.
Step 1
First get a BluetoothServerSocket by calling the method listenUsingRfcommWithServiceRecord(String, UUID). String is an identifiable name of your service that the system will automatically write a protocol that is the Service discovery Protocol database entry on the device.
Step 2
Start listening for connection requests by calling accept(). This is a blocking call, it depends on the user whether he want to accept or reject. It will return when either a connection has been accepted or an exception has occurred indicating the user has rejected the request. A connection is accepted only when a remote device has sent a connection request with a UUID matching the one registered with this listening server socket. When successful, accept() will return a connected BluetoothSocket.
Step 3
Calling the close() method.
The method releases the server socket and all its resources because the connection must be closed since otherwise it continues to consume, but does not close the connected BluetoothSocket that's been returned by accept(). Unlike TCP/IP, RFCOMM only allows one connected client per channel at a time, so in most cases it makes sense to call close() on the BluetoothServerSocket immediately after accepting a connected socket.
When the call is blocked in these cases we must write a code to abort a blocked call such as accept(), call close() on the BluetoothServerSocket from another thread and the blocked call will immediately return.
Note: All methods on a BluetoothServerSocket or BluetoothSocket must be thread-safe.
Code:
-
- private class AcceptThread extends Thread {
- private final BluetoothServerSocket mmServerSocket;
-
- public AcceptThread() {
-
-
- BluetoothServerSocket tmp = null;
- try {
-
- tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
- } catch (IOException e) { }
- mmServerSocket = tmp;
- }
-
- public void run() {
- BluetoothSocket socket = null;
-
- while (true) {
- try {
- socket = mmServerSocket.accept();
- } catch (IOException e) {
- break;
- }
-
- if (socket != null) {
-
- manageConnectedSocket(socket);
- mmServerSocket.close();
- break;
- }
- }
- }
-
-
- public void cancel() {
- try {
- mmServerSocket.close();
- } catch (IOException e) { }
- }
- }
Connecting as a ClientAfter executing as a server then it must be able to accept and send data as well. The client must acquire a BluetoothSocket and initiate the connection.
Step 1
Using the BluetoothDevice, get a BluetoothSocket by calling createRfcommSocketToServiceRecord(UUID).
Step 2
Initiate the connection by calling connect().
Code:
- private class ConnectThread extends Thread {
- private final BluetoothSocket mmSocket;
- private final BluetoothDevice mmDevice;
-
- public ConnectThread(BluetoothDevice device) {
-
-
- BluetoothSocket tmp = null;
- mmDevice = device;
-
-
- try {
-
- tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) { }
- mmSocket = tmp;
- }
-
- public void run() {
-
- mBluetoothAdapter.cancelDiscovery();
-
- try {
-
-
- mmSocket.connect();
- } catch (IOException connectException) {
-
- try {
- mmSocket.close();
- } catch (IOException closeException) { }
- return;
- }
-
-
- manageConnectedSocket(mmSocket);
- }
-
-
- public void cancel() {
- try {
- mmSocket.close();
- } catch (IOException e) { }
- }
- }
SummaryThis article illustrated how the sharing mechanism works using Bluetooth. Although we have only provided the basics of the Bluetooth technology as we make a client and server architecture.