You are on page 1of 5

Android Bluetooth UUID Sample.

This program uses BluetoothAdapter to get default adapter.


Broadcast Intent(ACTION_REQUEST_ENABLE) method is used to turnon
bluetooth.
We can get UUID(Universally unique identifier) and MAC address from textview.
Before setting up bluetooth. We need to verify that the bluetooth is supported by the
device only then bluetooth turns on.
To turn on bluetooth the bluetooth adapter is required
(BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}.
By calling static method .getDefaultAdapter();will return a bluetooth adapter that
represents the device.
After getting the bluetooth adapter. We have to enable bluetooth .
This can be done by calling .isEnabled().Even after this if the method returns false.
We can request bluetooth to be enabled by calling
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Once bluetooth is enabled .
We can start device discovery .getBondedDevices this will get list of connected
devices.
To start discovering devices just call startDiscovery() .
This application has to register a BroadcastReceiver for ACTION_FOUND intent.
Inorder to receive information about all the connected devices.
Finally Out.Append method is used to list the devices and address of the devices.

JAVA PROGRAM
package com.example.bluetoothlist;
import java.util.Set;
import
import
import
import
import
import
import

android.os.Bundle;
android.app.Activity;
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.content.DialogInterface;
android.content.DialogInterface.OnClickListener;
android.content.Intent;

import
import
import
import
import
import

android.view.Menu;
android.view.View;
android.widget.Button;
android.widget.ListView;
android.widget.TextView;
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>
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>

You might also like