Turn Bluetooth On/Off in Android Studio
Welcome. Here in this tutorial, you will learn how to Turn Bluetooth On/Off in Android Studio
Turning Bluetooth On/Off Example for Android Studio
Bluetooth is a protocol, which enables gadgets to interface remotely to trade the information with other Bluetooth gadgets.
For the most part, in android applications by utilizing Bluetooth API’s we can actualize Bluetooth functionalities, for example, empower or debilitate a Bluetooth, looking for accessible Bluetooth gadgets, associating with the gadgets and dealing with the information move between gadgets inside the range. We will have to use the BluetoothAdapter class to deal with Bluetooth in Android Studio.
Enabling or Turning On Bluetooth in Android Studio
To turn On Bluetooth, you will have to use startActivityForResult() method and ACTION_REQUEST_ENABLE intent with BluetoothAdapter to request for permission to turn on Bluetooth in your android app. The code is shown below for example:
1 2 3 |
Intent bluetoothIntent; bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(bluetoothIntent, 1); |
Disbling or Turning Off Bluetooth in Android Studio
You can turn off Bluetooth by using disable() method from BluetoothAdapter in Android Studio. The code snippet is shown below:-
1 2 |
BluetoothAdapter bt; bt.disable(); |
Note:- You will also need to add permission in AndroidManifest.xml file for any Bluetooth related work. Simply open the xml file and add this line:-
1 2 3 |
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
A sample image button is shown in the below picture. The button when switched will turn On Bluetooth or turn it Off as the case may be.
The Full Java Code along with the XML layout for creating app like the above example is shown below:-
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
The code for the MainActivity.java
is given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; public class EnableDisableBluetooth extends AppCompatActivity { Switch sw; TextView textview; BluetoothAdapter bt; int i = 1; Intent bluetoothIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_enable_disable_bluetooth); sw = (Switch)findViewById(R.id.switch1); textview = (TextView)findViewById(R.id.textView1); bt = BluetoothAdapter.getDefaultAdapter(); sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { BluetoothEnable(); } else { BluetoothDisable(); } } }); } public void BluetoothEnable(){ bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(bluetoothIntent, i); textview.setText("Bluetooth ON"); } public void BluetoothDisable(){ bt.disable(); textview.setText("Bluetooth OFF"); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
The content_main.xml
is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<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" > <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch1" android:layout_centerHorizontal="true" android:layout_marginBottom="62dp" android:gravity="center" android:text="Bluetooth status show here" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout> |
You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with these other Bluetooth settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed as per your requirements.
Output:
Finally, run this project.
This is how Android Count Down Timer Code look like.
For a more detailed guide, you can also visit android’s official guide below:-
https://developer.android.com/guide/topics/connectivity/bluetooth
Comment down below if you are facing any problems.