How To Handle Bluetooth Settings From Your Application

This is a sample activity which shows How to enable and disable bluetooth settings from your application. Last post published in this forum is What is DDMS and how it works.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a Project bluetoothdemo.
2.) Add following permissions in AndroidManifest :

 

<!--?xml version="1.0" encoding="utf-8"?-->
package="com.app.bluetoothdemo"
android:versionCode="1"
android:versionName="1.0">

 

 

3.) Put the following code snippet in res/layout/main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->

 

 

4.) Get the BluetoothAdapter, The BluetoothAdapter is required for any and all Bluetooth activity. For example:

 

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null)
{
// Device does not support Bluetooth
}

 

 

5.) Next, you need to ensure that Bluetooth is enabled. Call isEnabled() to check whether Bluetooth is currently enable. For example :

 

if (!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 5);
}

 

 

6.) If you would like to make the local device discoverable to other devices, call startActivityForResult(Intent, int) with the ACTION_REQUEST_DISCOVERABLE action Intent. For example :

 

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

 

 

7.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. bluetoothdemo. Enter following information:
Project name: bluetoothdemo
Build Target: Android APIs 2.1
Application name: bluetoothdemo
Package name: com.app. bluetoothdemo
Create Activity: bluetoothdemo

On Clicking Finish bluetoothdemo code structure is generated with the necessary Android Packages being imported along with bluetoothdemo.java. bluetoothdemo class will look like following:

 

package com.app.bluetoothdemo;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class bluetoothdemo extends Activity implements OnClickListener {

ToggleButton tb;
BluetoothAdapter mBluetoothAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tb = (ToggleButton) findViewById(R.id.toggleButton1);
tb.setOnClickListener(this);
}

public void onClick(View v) {
switch(v.getId())
{
case R.id.toggleButton1 :
if((tb).isChecked())
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 5);
}

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
else
{
Toast myToast = Toast.makeText(getApplicationContext(), "Bluetooth turned off", Toast.LENGTH_SHORT);
myToast.show();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (mBluetoothAdapter.isEnabled()) {
if (mBluetoothAdapter != null) {
mBluetoothAdapter.disable();
}
}
}
}
}
}

 

 

Output – The final output:

Leave a Comment:

14 comments
Article » Use of SQLite in Android says August 19, 2011

[…] This is a sample program which shows usage of SQLite in android application for saving data. This application has two buttons in the main menu one for saving information and another for showing all saved information. Last blog published in this forum is How to handle bluetooth settings from your application. […]

Reply
satish says October 18, 2011

hi i copied this code and pasted. the program gets compiled and show the xml window. but when i click the toggle button the applications show the error msg and it closes. can u help pls in finding the error as i need this code for my mini project. am using 2.2 apk8

Reply
deepa says January 11, 2012

sir,i am getting null pointer exception and unable enable bluetooth

Reply
ravi says March 12, 2012

sir
how to send a database file by blue-tooth in android program and also how to receive that file ,please send me the code or example of this program asap.
thanks in advance

Reply
ravi says March 12, 2012

sir

when i am using this code in my program it is showing froce close
please send me any working code of all the bluetooth functionality with sending and receiving also.

thanks

Reply
arun says April 20, 2012

I am getting null pointer exception

Reply
jubi says May 2, 2012

very good

Reply
Deepthi G says May 7, 2012

Hi ,

I’m a newbie in android. Your tutorials are really great.. Do you have any idea about how to connect scanned devices using android Bluetooth..?
Thanks in Advance

Deepthi

Reply
programmer says September 19, 2012

this code is not running on an emulator…. !!!!

Reply
    Gina says September 27, 2012

    The emulator has not bluetooth connection. You have to do it on a phone.

    Reply
      programmer says September 27, 2012

      ok i’ll try this

      Reply
Duke says September 28, 2012

Thanks. I got this working on eclipse. will run this with my android device later

Reply
Duke says September 28, 2012

worked with the android device. thank you so much

Reply
Deepak Sharma says November 30, 2012

Sorry friends..But this code will only works in android emulator.

Reply
Add Your Reply