Tuesday 3 September 2013

Bluetooth Switch On, Off,Device Discoverable,Scan Remote device

1. Create a Class  with name Bluetooth_Utils

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.widget.Toast;

public class Bluetooth_Utils {

    private static final int REQUEST_ENABLE_BT = 0;
    private static final int REQUEST_DISCOVERABLE_BT = 0;
    private static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   
    public  static void Check_BT_present()
    {
        if (mBluetoothAdapter == null) {
            System.out.println("device not supported");
        }
    }
   
    public static void onBT(Activity activity)
    {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
   
    public static void offBT(Activity activity)
    {
        mBluetoothAdapter.disable();
        CharSequence text = "TURNING_OFF BLUETOOTH";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(activity, text, duration);
        toast.show();
    }
   
    public static void devicediscoverable(Activity activity)
    {
        if (!mBluetoothAdapter.isDiscovering()) {
            CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(activity, text, duration);
            toast.show();
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            activity.startActivityForResult(enableBtIntent,
                    REQUEST_DISCOVERABLE_BT);

        }
    }
   
    public  static void Scan_NearbyBT()
    {
        // Starting the device discovery
        System.out.println("\nStarting discovery...");
        mBluetoothAdapter.startDiscovery();
        System.out.println("\nDone with discovery...");

        // Listing paired devices
        System.out.println("\nDevices Pared:");
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
            System.out.println("\nFound device: " + device.getName());
        }
    }
   
}

2. Use these in your MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        final Button button = (Button) findViewById(R.id.button1);
        final Button button1 = (Button) findViewById(R.id.button2);
        final Button button2 = (Button) findViewById(R.id.button3);
        Bluetooth_Utils.Check_BT_present();
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
           
                Bluetooth_Utils.onBT(MainActivity.this);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Bluetooth_Utils.devicediscoverable(MainActivity.this);
                Bluetooth_Utils.Scan_NearbyBT();
                }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
               
                Bluetooth_Utils.offBT(MainActivity.this);
            }
        });
    }

}

3.Create a layout activity_main

<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="switch 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="make device discoverable and device scan " />

    <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="switch off" />

</RelativeLayout>

4.Add permissions in manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bluetooth.sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Now, are able to Switch On, Off,Device Discoverable,Scan Remote deviceusing bluetooth.


No comments:

Post a Comment