You are on page 1of 4

Experiment No.

1
Aim: Implementation a Bluetooth network with application as transfer of a file
from one device to another.
Software Used: ANDROID
Theory:
Bluetooth is a standardized protocol for sending and receiving data via a
2.4GHz wireless link. It’s a secure protocol, and it’s perfect for short-range,
low power, low-cost, wireless transmissions between electronic devices.
These days it feels like everything is wireless, and Bluetooth is a big part of
that wireless revolution.
You’ll find Bluetooth embedded into a great variety of consumer products, like
headsets, video game controllers, or livestock trackers.
In our world of embedded electronics hackery, Bluetooth serves as an excellent
protocol for wirelessly transmitting relatively small amounts of data over a
short range.

Connection Process Creating a Bluetooth connection between two


devices is a multi-step process involving three progressive states:

1. Inquiry – If two Bluetooth devices know absolutely nothing about each other,
one must run an inquiry to try to discover the other. One device sends out the
inquiry request, and any device listening for such a request will respond with its
address, and possibly its name and other information.

2. Paging (Connecting) – Paging is the process of forming a connection between


two Bluetooth devices. Before this connection can be initiated, each device
needs to know the address of the other (found in the inquiry process).

3. Connection – After a device has completed the paging process, it enters the
connection state. While connected, a device can either be actively participating
or it can be put into a low power sleep mode.

• Active Mode – This is the regular connected mode, where the device is
actively transmitting or receiving data.

• Sniff Mode – This is a power-saving mode, where the device is less active.
It’ll sleep and only listen for transmissions at a set interval (eg. every 100ms).
• Hold Mode – Hold mode is a temporary, power-saving mode where a device
sleeps for a defined period and then returns back to active mode when that
interval has passed. The master can command a slave device to hold.

• Park Mode – Park is the deepest of sleep modes. A master can command a
slave to “park”, and that slave will become inactive until the master tells it to
wake back up.

Bonding and Pairing: - When two Bluetooth devices share a special affinity for
each other, they can be bonded together. Bonded devices automatically establish
a connection.

Program Code: -

public class MyBluetoothService {


   private static final String TAG = "MY_APP_DEBUG_TAG";
   private Handler handler; // handler that gets info from Bluetooth
service

   // Defines several constants used when transmitting messages between the
   // service and the UI.
   private interface MessageConstants {
       public static final int MESSAGE_READ = 0;
       public static final int MESSAGE_WRITE = 1;
       public static final int MESSAGE_TOAST = 2;

       // ... (Add other message types here as needed.)


   }

   private class ConnectedThread extends Thread {


       private final BluetoothSocket mmSocket;
       private final InputStream mmInStream;
       private final OutputStream mmOutStream;
       private byte[] mmBuffer; // mmBuffer store for the stream

       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();
           } catch (IOException e) {
               Log.e(TAG, "Error occurred when creating input stream", e);
           }
           try {
               tmpOut = socket.getOutputStream();
           } catch (IOException e) {
               Log.e(TAG, "Error occurred when creating output stream", e);
           }

           mmInStream = tmpIn;
           mmOutStream = tmpOut;
       }

       public void run() {


           mmBuffer = new byte[1024];
           int numBytes; // bytes returned from read()

           // Keep listening to the InputStream until an exception occurs.


           while (true) {
               try {
                   // Read from the InputStream.
                   numBytes = mmInStream.read(mmBuffer);
                   // Send the obtained bytes to the UI activity.
                   Message readMsg = handler.obtainMessage(
                           MessageConstants.MESSAGE_READ, numBytes, -1,
                           mmBuffer);
                   readMsg.sendToTarget();
               } catch (IOException e) {
                   Log.d(TAG, "Input stream was disconnected", e);
                   break;
               }
           }
       }

       // Call this from the main activity to send data to the remote
device.
       public void write(byte[] bytes) {
           try {
               mmOutStream.write(bytes);

               // Share the sent message with the UI activity.


               Message writtenMsg = handler.obtainMessage(
                       MessageConstants.MESSAGE_WRITE, -1, -1, mmBuffer);
               writtenMsg.sendToTarget();
           } catch (IOException e) {
               Log.e(TAG, "Error occurred when sending data", e);

               // Send a failure message back to the activity.


               Message writeErrorMsg =
                     
handler.obtainMessage(MessageConstants.MESSAGE_TOAST);
               Bundle bundle = new Bundle();
               bundle.putString("toast",
                       "Couldn't send data to the other device");
               writeErrorMsg.setData(bundle);
               handler.sendMessage(writeErrorMsg);
           }
       }

       // Call this method from the main activity to shut down the
connection.
       public void cancel() {
           try {
               mmSocket.close();
           } catch (IOException e) {
               Log.e(TAG, "Could not close the connect socket", e);
           }
       }
   }
}
Output: -

You might also like