
What is Bluetooth in Android?
Bluetooth is a way to send or receive data between two different devices. It is exchange data with other devices wirelessly. Bluetooth is communication network protocol, which allow a devices to connect wirelessly to exchange the data with other Bluetooth devices.
Android provides Bluetooth API to perform these different operations.
Scan for other Bluetooth devices .
connect and transfer data from and to other devices.
Multiple Connection Manage. Get the list of paired devices.
What is Bluetooth Adapter Class in Android ?
BluetoothAdapter class is a perform fundamental tasks such as initiate device discovery, query a list of paired (bonded) devices, create a BluetoothServerSocket instance to listen for connection requests etc.
Bluetooth Adapter Class use to Constants String.
- String ACTION_REQUEST_ENABLE
- String ACTION_REQUEST_DISCOVERABLE
- String ACTION_DISCOVERY_STARTED
- String ACTION_DISCOVERY_FINISHED
Methods of BluetoothAdapter Class
static synchronized BluetoothAdapter getDefaultAdapter() : In this Method is used to return the instance of BluetoothAdapter.
boolean enable() : In this Method is used to Enable the Bluetooth adapter when if it disabled.
boolean isEnabled() : In this Method is used to return true/false if return true the bluetooth adapter is enabled.
boolean disable() : In this Method is used to Disable the Bluetooth when if it is enable.
String getName() : In this Method is used to Return the method if Bluetooth name.
boolean setName(String name) : In this Method is used to Change the Name of bluetooth.
int getState() : In this Method is used to Return the Current State of bluetooth adapter.
Set<BluetoothDevice> getBondedDevices() : In this Method is used to returns a set of paired (bonded) BluetoothDevice objects.
boolean startDiscovery() : In this Method is used to Start the Discovery.
Example of Bluetooth Enable/Disable In Android
Declared Permission in AndroidManifest.xml File Follow this Code.
1 2 |
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
Create activity_main.xml File Follow this Code.
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btn_on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Bluetooth ON" android:layout_marginTop="20dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/btn_off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Bluetooth OFF" android:layout_marginTop="20dp" android:layout_marginRight="10dp" android:layout_alignParentRight="true"/> </RelativeLayout> |
Create MainActivity.java File Follow this code.
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.example.bluetoothdemo.activities; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.bluetoothdemo.R; import java.util.ArrayList; import java.util.Set; public class MainActivity extends AppCompatActivity { private Button btn_on,btn_off; private BluetoothAdapter mBluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_on = findViewById(R.id.btn_on); btn_off = findViewById(R.id.btn_off); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); btn_on.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mBluetoothAdapter == null){ Toast.makeText(MainActivity.this, "Bluetooth Does Not Support this Device", Toast.LENGTH_SHORT).show(); }else{ if (!mBluetoothAdapter.isEnabled()){ Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,BLUETOOTH_REQ_CODE); } } } }); btn_off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.disable(); } } }); } } |
Above the Example of How to Enable And Disable Bluetooth In Android.In this Example of Create Two button is On And Off When you can click on button to enable the bluetooth and then click on Off button to Disable the Bluetooth in Android.