This is a simple and powerful bluetooth app.Using this you can turn_on bluetooth,start discoverable with a simple touch,even turn_off your bluetooth.This will not work on emulator since bluetooth support is not added.
Java source code
package com.example.bluetooth;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVERABLE_BT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView out=(TextView)findViewById(R.id.out);
final Button button = (Button) findViewById(R.id.button1);
final Button button1 = (Button) findViewById(R.id.button2);
final Button button2 = (Button) findViewById(R.id.button3);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
out.append("device not supported");
}
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (!mBluetoothAdapter.isDiscovering()) {
// out.append("MAKING YOUR DEVICE DISCOVERABLE");
Context context = getApplicationContext();
CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mBluetoothAdapter.disable();
// out.append("TURN_OFF BLUETOOTH");
Context context = getApplicationContext();
CharSequence text = "TURNING_OFF BLUETOOTH";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, 15);
toast.show();
}
});
}
}
Xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher"
tools:context=".MainActivity" >
<TextView android:text="" android:id="@+id/out" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="49dp"
android:text="TURN_ON" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="28dp"
android:text="TURN_OFF" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bluetooth.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


Download.apk file
http://www.fileserve.com/file/23GKYwT/bth.zip

Hi, I am getting the infamous "Application has stopped unexpectedly. Please try again." dialog when I run this code on my android device. And since the emulator does not support bluetooth there is no way to debug and find out where the code is crashing. I have the necessary permissions ie BLUETOOTH_ADMIN and BLUETOOTH. It would be great if you could help me out.
ReplyDeletePlease let me know on which android device ur using this code.
ReplyDeletei ma also getting the same error
ReplyDeleteLeave ur mail id to support you. I ll try this code and post the .apk file today.
ReplyDeleteThere are no errors in the above code. I have tested it on my phone(HTC desire V) . So please test only on any android device other than emulator. I have also uploaded .apk file for testing purpose. enjoy friends.
ReplyDeleteThank you. It is very simple and easy one and I am able to run the application perfectly. But I want to have a program from you that will enable user to turn on bluetooth and chat with the other Android device over bluetooth.
ReplyDeleteI have got the error in getMenuInflater().inflate(R.menu.activity_main, menu);
ReplyDeleteI have got the error in this statement.....
ReplyDeletegetMenuInflater().inflate(R.menu.activity_main, menu);
Thanks, great example!
ReplyDeleteANKUR. I hope your trying to create menu using method getMenuInflater(). When you press the menu button you will get this error.Because it will search for menu file. Which has to be located in /res/menu . You can implement in xml instead.
ReplyDeletePlease refer to this example.(http://www.java2s.com/Code/Android/UI/GetMenutitle.htm).
Hello ANUKUR. Now i have removed the method getMenuInflater().
ReplyDeleteTry the same code . It will work with out any error.
Hello Bhushan, I'm Krunal...I'm working on an android project.. I tried running this app but got the error...
ReplyDelete"Application has stopped unexpectedly"
Thanks in advance...
my id is krunal_717@yahoo.co.in
Thank you Bhushan......nice tutorial....it helped a lot...
ReplyDeleteThanks once again.....
hii......the code is ok......and its working.....plz dont try to run it on ur emulator....run in smartphones...it will work....thanku sir...thanks alot.......
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi its great Bhushan. I m new to android world. Please help me with a code that will automatically enable bluetooth n receive a string from another bluetooth device.. Thank u in advance...
ReplyDeletemy id is rishiskeshav91@gmail.com.
please help its urgent
hello sir i m T.Y.B.Sc.It student, i want to make the project on Android Bluetooth chat application....in this application those who is having the my app could communicate with each other through the msgs chat...what requirement for this app?
ReplyDeleteHELLO HUNK. First start with server and client connectivity. In chat service you will be using UUID . UUID is an immutable representation of a 128-bit universally unique identifier UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); . After that you should get the device list. Reference links
ReplyDelete1.(http://developer.android.com/reference/java/util/UUID.html).
2.(http://developer.android.com/guide/topics/connectivity/bluetooth.html)
thank you bhushan sir, its nice to see these code. but i just wnat to use bluetooth connectivity to a mono bluetooth handfree to play media music on it. since there is no option to play music via bluetooth in default android music player like other as in nokia or other mobile OS. can i integrate this functionality to a media player? there is only one free app BTmono avail in play store to do this task. my Email id is jawedkhan0405@gmail.com. aiting for your reply
ReplyDeleteHello Jawed. Please go through the below steps. This helps you to play audio using bluetooth.
ReplyDeleteAudio over Bluetooth
An accessory that connects with Android over Bluetooth can use an Advanced Audio Distribution Profile (A2DP) connection stream music for playback. Playing audio over a Bluetooth with A2DP is supported on Android 1.5 (API Level 3) and higher. An Android user can connect to an accessory that supports this profile using the system Settings > Bluetooth and play music directly to the accessory without the need for a secondary application.
Note: If you want to provide a custom application for output to your audio accessory, note that the Android 3.0 (API Level 11) allows applications to operate an A2DP connection using the BluetoothA2dp class.
Next steps
To get started on building an audio accessory that uses a Bluetooth connection:
Select a hardware platform or build an hardware device that can support Bluetooth communications and the A2DP connection profile.
Review the ADK 2012 firmware source code (/adk2012/board/library/ADK2/), which includes an example implementation of an audio playback accessory using a Bluetooth connection.
Note: The ADK 2012 source code includes an open source Bluetooth stack that is built for the Texas Instruments CC2564 chip, but can work with any Bluetooth chip that implements a standard Host/Controller Interface (HCI).