Tuesday, 9 June 2015

Turn ON and OFF Bluetooth and check current state of Bluetooth

No comments
In this android example we write code for turn ON or OFF Bluetooth and check current state of Bluetooth is currently ON or OFF.

CREATE NEW ANDROID PROJECT

Step 1: Write code into activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/turnON"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="64dp"
        android:src="@drawable/bluetoothoff" />

    <TextView
        android:id="@+id/Bluetooth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:layout_weight="1"
        android:text="bluetooth ON OFF"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/discoverable"
        android:layout_below="@+id/turnON"
        android:layout_marginTop="24dp"
        android:text="make discover"
        android:textSize="18sp" />

    <ImageButton
        android:id="@+id/discoverable"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_alignLeft="@+id/Bluetooth"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="15dp"
        android:src="@drawable/bluetoothdiscoveroff" />


</RelativeLayout>


Step 2: Write code into MainActivity.java

package dev.androidapplink.bluetoothapp;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {

// define bluetooth control
ImageButton turnONOFF;
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// load bluetooth control
turnONOFF = (ImageButton) findViewById(R.id.turnON);
final ImageButton discoverable = (ImageButton) findViewById(R.id.discoverable);

cheackBluetoothStastus();

// Implement click event and set image according to status and make
// bluetooth on off
// working like toggle button
turnONOFF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (!bluetooth.isEnabled()) {

turnONOFF.setImageResource(R.drawable.bluetoothon);

startActivityForResult(new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE), 0);

} else {
bluetooth.disable();
turnONOFF.setImageResource(R.drawable.bluetoothoff);
discoverable
.setImageResource(R.drawable.bluetoothdiscoveroff);
}

}
});
discoverable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Implement click event and set image according to status
if (!bluetooth.isDiscovering()) {

discoverable
.setImageResource(R.drawable.bluetoothdiscoveroff);
turnONOFF.setImageResource(R.drawable.bluetoothon);
startActivityForResult(new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 0);
discoverable
.setImageResource(R.drawable.bluetoothdiscoveron);

}
}
});

}

// Check bluetooth status and set image according to
private void cheackBluetoothStastus() {
// TODO Auto-generated method stub

if (bluetooth.isEnabled()) {
turnONOFF.setImageResource(R.drawable.bluetoothon);

} else {
turnONOFF.setImageResource(R.drawable.bluetoothoff);

}
}

}

NOTE: Give below permission in Androidmanifest.xml
Its allow to access Bluetooth settings.

 <uses-permission android:name="android.permission.BLUETOOTH" />

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Step 3: Run Your Project:

Turn on Bluetooth



Bluetooth is on:

Make Discover:

Discovering for 120 seconds:



No comments :

Post a Comment

Follow me Share