How to check and turn on the device Bluetooth in Android?

In this article, we are going to learn about the Bluetooth and we will check our device’s Bluetooth whether it is on or not, if not we are going to learn how to turn it on?
Submitted by Manu Jemini, on February 06, 2018

Now, we learn the method to use BluetoothAdapter and check the current Bluetooth status of. In the example below, we are going to use the Bluetooth.BluetoothAdapter class to have all the functionality required for BluetoothAdapter. This BluetoothAdapter.getDefaultAdapter() will create an object which can be used to process the current Bluetooth.

To implement this in your program you will need to import: android.bluetooth.BluetoothAdapter in your java file. Now on your XML File, we need a button for switching the mode. For this just add a button like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Check whether your BLUETOOTH is ON or OFF"
	android:id="@+id/button"
	android:clickable="true"
	android:onClick="check"
	android:layout_marginTop="150dp"
	android:layout_marginLeft="20dp"
	android:layout_marginRight="20dp" />

Now, this button has an onClick attribute which is referring to a function called check, this function is implemented in JAVA file with corresponding logic to check the current Bluetooth mode and use the Toast class to make a Text.

Now in the function, we first check if the Bluetooth is on or off. If it does not enable we will use this line to turn the Bluetooth on.

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

After that just call a Toast.

1) Java file:

package com.example.hp.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
	BluetoothAdapter B;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		B=BluetoothAdapter.getDefaultAdapter();
	}

	public void check(View v){
		if (!B.isEnabled()) {
			Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(turnOn, 0);
			Toast.makeText(getApplicationContext(), "Your Bluetooth is OFF we are turning it ON",Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(getApplicationContext(), "Bluetooth is already on", Toast.LENGTH_LONG).show();
		}
	}
}

2) XMLfile:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check whether your BLUETOOTH is ON or OFF"
        android:id="@+id/button"
        android:clickable="true"
        android:onClick="check"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" />

</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Output

Android - check if bluetooth is on or not 1
Android - check if bluetooth is on or not 2

Android - check if bluetooth is on or not 3


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.