Bluetooth in Android-Part II

In my last post, I’ve discussed about how to check whether a device supports Bluetooth and enabling and disabling Bluetooth in a device which supports Bluetooth. In this post let’s see how to make our device discoverable to other Bluetooth devices.

Refer me previous post, Bluetooth in Android-Part I, so that things would be clear to you.

First of all to get the local Bluetooth adapter we use the getDefaultAdapter().

BluetoothAdapter myBTadapter = BluetoothAdapter.getDefaultAdapter();

Now in order to make our device discoverable to others we need to start activity with action intent ACTION_REQUEST_DISCOVERABLE. Though by default the duration to remain discoverable is 120seconds, this can be changed and can give a maximum duration of about 3600 seconds. Now to define a new duration that we desire, we need to add an intent extra named EXTRA_DISCOVERABLE_DURATION.

Just look at the code below:

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);

Here the duration of the device to be discoverable is set as 400 seconds.

In the XML, I’ve added only one button. Just have a look at the XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="@string/hello" />
        android:text="Make your Deivice Discoverable" />

Here is a screen shot of how the UI will look like:

Add the following permissions in your manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

The Whole code:

package com.deepthi.mybluetoothdiscoverable;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyBluetoothDiscoverableActivity extends Activity {
 /** Called when the activity is first created. */
 Button MakeDiscoverable_btn;
 public void onCreate(Bundle savedInstanceState) {
 MakeDiscoverable_btn.setOnClickListener(new OnClickListener() {

 public void onClick(View v) {
 // TODO Auto-generated method stub
 BluetoothAdapter myBTadapter = BluetoothAdapter.getDefaultAdapter();
 Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
 discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
 Toast.makeText(getApplicationContext(), "Now your device is discoverable by others", Toast.LENGTH_LONG).show();

Hope this post was useful.Try this with your real Bluetooth supported android device. 🙂  Check this link for more details about emulator.


, , , , , , ,

  1. #1 by Imran Khan on November 3, 2012 - 12:43 pm

    Thank You very much….
    It was very helpful for me.

  2. #2 by Rashi.. Coorg.. on November 27, 2012 - 3:35 pm

    Thank u.. 🙂

  3. #3 by Tikson Tom on March 25, 2013 - 4:56 pm

    your both posts regarding bluetooth is nice.Do you know how to discover other bluetooth device from our device and send data to that. I had a bluettoth headset my project is to record audio using that headset and we will be able to hear that recorded audio using that headset

  4. #4 by JonaCas on April 3, 2013 - 8:20 pm

    Hi!! Congrats! this is a good tuto of Android BT, quite simple but useful! I would like to know if there is any link for Finding Devices? and InputStream and OutputStream?

    Thanks a lot!

  5. #5 by Laxman Sharma on September 11, 2013 - 10:17 pm

    Look i need to toast a message on state change like if it is connected earlier and then disconnected or vice versa… Tell me a way to listen to the states. I tried earlier but could make it yet…

  6. #6 by Evgeny on December 3, 2013 - 4:35 pm

    where 3-rd and 4-th parts of Bluetooth

  7. #7 by Amal Kainth on April 2, 2014 - 3:55 pm

    helo. thanku for a wonderful tutorial. please can u put codes of connecting with devices..

  8. #8 by miguelalonso01 on April 16, 2014 - 3:43 am

    Hi, I have many doubts in the development of an Android application using a usb / bluetooth device as input data and wifi socket as output. I would like to get help from someone who has experience in Android because I’m a bit lost. can you help me out? I’ll appreciate it a lot. nice day

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: