Thursday 21 March 2013

Android Bluetooth UUID Sample.

This program uses Bluetooth Adapter to get default adapter.
Broadcast Intent(ACTION_REQUEST_ENABLE) method is used to turn on Bluetooth.
We can get UUID(Universally unique identifier) and MAC address from text view.
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

(Bluetooth Adapter 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 .is Enabled().Even after this if the method returns false.
We can request blue tooth 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 start Discovery() .

This application has to register a BroadcastReceiver for ACTION_FOUND intent.
In order 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 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() { 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>

3 comments:

  1. I am not sure where you are getting the UUIDs in this program

    ReplyDelete
  2. I am getting it here. out.append("\nFound device: " + device.getName()+ "\n" +"\nDevice name"+ device.getAddress());

    ReplyDelete
  3. Harrah's Cherokee Casino - MapYRO
    Information and Reviews 당진 출장마사지 about 안양 출장샵 Harrah's Cherokee 동해 출장안마 Casino in Murphy, NC. 전라북도 출장마사지 문경 출장샵 Map, Map and Directions. Directions, Reviews. Hotel. Casino Information

    ReplyDelete