People always feel happy sharing the magical moments of life in the form of 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 server 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). The 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 wants 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 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
After 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.
This 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.