Home / Tutorials / Android – Chat Over Bluetooth

Android – Chat Over Bluetooth

Objective

Main objective of this post is to give an idea about how to allow two-way text chat over Bluetooth in android.

Step 1 Get Bluetooth Service

We need the Android Bluetooth service for this tutorial to work. In order to use Bluetooth service, declare BLUETOOTH permission in manifest file.

Now to initiate device discovery and to access Bluetooth setting declare BLUETOOTH_ADMIN permission.

(Note: If we require BLUETOOTH_ADMIN permission, then we have to declare BLUETOOTH permission also.)

Step 2 BluetoothAdapter class

Now to check whether Bluetooth is supported on device or not, we use object of BluetoothAdapter class.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

If this method returns null, then it means that Bluetooth is not supported on the device and so we will close the application.

To know more about BluetoothAdapter class : BluetoothAdapter

 

Step 3 isEnable() Method

To check Bluetooth is enabled or not, we will use isEnabled() method on object of BluetoothAdapter class.

If Bluetooth is disabled then we request the user to enable it. And we perform this action by calling startActivityForResult() with REQUEST_ENABLE_BT action. This will open dialog to enable Bluetooth on the device.

 

an-app-wants-to-turn-on-bluetooth

If the user clicks Allow then the onActivityResult() method receives RESULT_OK and if the user clicks Deny (or due to internal problem in device), RESULT_CANCELED is received. If returned value RESULT_OK is received then we will initiate the chat service.

Step 4 Discover Bluetooth

Now in android, device is not discoverable by default. To make device discoverable, call startActivityForResult() with BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE action. By default device is discoverable for 120 seconds. To set discoverable duration, add EXTRA_DISCOVERABLE_DURATION in intent extra. The maximum value for duration is 360 seconds.

This will open dialog to enable discoverable mode.

an-app-wants-to-make-your-phone-visible-to-other-bluetooth-devices-for-300-seconds

Step 5 Bluetooth Connection

To start the chat, we first need to establish connection with the desired device. And before starting scanning for available devices, we usually get paired devices first in the list.

Above code will return a set of BluetoothDevice objects. The object of BluetoothDevice class gives required information about remote device which is used to establish connection (Explained later).

To start scanning, call the startDiscovery() method of BluetoothAdapter class. The activity which starts scanning must register receiver with BluetoothDevice.ACTION_FOUND action. After completing discovery, system will broadcast BluetoothDevice.ACTION_FOUND intent. This Intent contains extra fields EXTRA_DEVICE and EXTRA_CLASS, representing a BluetoothDevice and a BluetoothClass, respectively.

To know more about BroadcastReceiver : BroadcastReceiver

To register receiver:

Step 6 Pairing devices

To connect two devices, we must implement server side and client side mechanism. One device shall open the server socket and another should initiate the connection. Both are connected when BluetoothSocket is connected on the same RFCOMM channel. During connection procedure android framework automatically shows pairing dialog.

bluetooth-pairing-request

Connection as Server:

  1. Make object of BluetoothServerSocket by calling the listenUsingRfcommWithServiceRecord().
  2. Listening for connection requests by calling accept().
  3. Release server socket by calling close().
 

To know more about BluetoothSocket : BluetoothSocket

Connection as Client:

  1. Create object of BluetoothSocket by calling createRfcommSocketToServiceRecord(UUID) on BluetoothDevice object.
  2. Initiate connection by calling connect().

Step 7 Read and Write Data

  1. After establishing connection successfully, each device has connected BluetoothSocket.
  2. Now one can Read and write data to the streams using read(byte[]) and write(byte[]).

(Note: use android-support-v7-appcompat from your SDK)

About cmadmin

Web Developer & Designer | Android App Developer

Check Also

recover-android-contacts

Recover Contacts Of Android Phone if Lost

Its common problem of losing your contacts from Android phones. Generally it happens by accidentally …

Leave a Reply

Your email address will not be published. Required fields are marked *

Login


Username
Create an Account!
Password
Forgot Password? (close)

Sign Up


Username
Email
Password
Confirm Password
Want to Login? (close)

Forget Password?


Username or Email
(close)
%d bloggers like this:
SHARE
OR
SUBSCRIBE
To get latest new / tutorial / technology / development information subscribe with us.
ARE YOU READY? GET IT NOW!
Lets Get Updated with latest trends & tutorials!
Your Information will never be shared with any third party.
Ready for latest tutorials & tools !
OR SUBSCRIBE WITH