This program uses BluetoothAdapter to get default adapter. Broadcast Intent(
ACTION_REQUEST_ENABLE) is used to turnon bluetooth.
We can get UUID and MAC address from textview.
JAVA PROGRAM
package com.example.bluetoothlist;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Bluetoothlist extends Activity implements OnClickListener, android.view.View.OnClickListener {
//private static final int REQUEST_ENABLE_BT = 0;
TextView out;
Button turnon;
Button list;
Button turnoff;
int REQUEST_ENABLE_BT = 0;
BluetoothAdapter mBluetoothAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetoothlist);
turnon=(Button)findViewById(R.id.button1);
list=(Button)findViewById(R.id.list);
turnoff=(Button)findViewById(R.id.turnoff);
out = (TextView) findViewById(R.id.add);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
if (mBluetoothAdapter == null) {
// out.append("device not supported");
}
turnon.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);
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
out.append("\nFound device: " + device);
}
}
}
});
list.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
//Menu mArrayAdapter = null;
out.append("\nFound device: " + device.getName()+ "\n" +"\nDevice name"+ device.getAddress());
}
}
});
turnoff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mBluetoothAdapter.disable();
}
});}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}}
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Bluetoothlist extends Activity implements OnClickListener, android.view.View.OnClickListener {
//private static final int REQUEST_ENABLE_BT = 0;
TextView out;
Button turnon;
Button list;
Button turnoff;
int REQUEST_ENABLE_BT = 0;
BluetoothAdapter mBluetoothAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetoothlist);
turnon=(Button)findViewById(R.id.button1);
list=(Button)findViewById(R.id.list);
turnoff=(Button)findViewById(R.id.turnoff);
out = (TextView) findViewById(R.id.add);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
if (mBluetoothAdapter == null) {
// out.append("device not supported");
}
turnon.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);
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
out.append("\nFound device: " + device);
}
}
}
});
list.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
//Menu mArrayAdapter = null;
out.append("\nFound device: " + device.getName()+ "\n" +"\nDevice name"+ device.getAddress());
}
}
});
turnoff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mBluetoothAdapter.disable();
}
});}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}}
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"
tools:context=".Bluetoothlist" >
<Button
android:id="@+id/list"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:text="List" />
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="52dp"
android:text="List of devices" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/list"
android:layout_centerHorizontal="true"
android:onClick="showMessage"
android:text="TURN ON BT" />
<Button
android:id="@+id/turnoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_below="@+id/list"
android:layout_toRightOf="@+id/list"
android:text="TURN OFF" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Bluetoothlist" >
<Button
android:id="@+id/list"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:text="List" />
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="52dp"
android:text="List of devices" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/list"
android:layout_centerHorizontal="true"
android:onClick="showMessage"
android:text="TURN ON BT" />
<Button
android:id="@+id/turnoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_below="@+id/list"
android:layout_toRightOf="@+id/list"
android:text="TURN OFF" />
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothlist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.bluetoothlist.Bluetoothlist"
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>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothlist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.bluetoothlist.Bluetoothlist"
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>













