To exchange data with other devices wirelessly, Bluetooth is used. The Bluetooth framework supported by the Android platform allows a device to send or receive data between two different devices. The Bluetooth API of Android is used to perform these tasks and many more:
- To scan Bluetooth devices
- To connect and transfer data from and to other devices
- To manage multiple connections
Android Bluetooth API:
The interfaces classes to work with Bluetooth are included in the android.bluetooth package. These are:
- BluetoothAdapter
- BluetoothDevice
- BluetoothSocket
- BluetoothServerSocket
- BluetoothClass
- BluetoothProfile
- BluetoothProfile.ServiceListener
- BluetoothHeadset
- BluetoothA2dp
- BluetoothHealth
- BluetoothHealthCallback
- BluetoothHealthAppConfiguration
BluetoothAdapter class:
To perform the fundamental tasks, like to initiate a device discovery, to query a list of paired or bonded devices, to create a BluetoothServerSocket instance to listen for connection requests, etc, the BluetoothAdapter class is used.
Constants of BluetoothAdapter class:
There are various constants provided by the BluetoothAdapter class. Some of these constants are:
- String ACTION_REQUEST_ENABLE
- String ACTION_REQUEST_DISCOVERABLE
- String ACTION_DISCOVERY_STARTED
- String ACTION_DISCOVERY_FINISHED
Methods of BluetoothAdapter class:
The BluetoothAdapter class contains various methods. Some of the important methods of the BluetoothAdapter class are:
- static synchronized BluetoothAdapter getDefaultAdapter(): Used to return the instance of BluetoothAdapter.
- boolean enable(): Used to enable the Bluetooth adapter, if it is disabled.
- boolean isEnabled(): Used to return true if the Bluetooth adapter is enabled.
- boolean disable(): Used to disable the Bluetooth adapter, if it is enabled.
- String getName(): Used to return the name of the Bluetooth adapter.
- boolean setName(String name): Used to change the Bluetooth name.
- int getState(): Used to return the current state of the local Bluetooth adapter.
- Set<BluetoothDevice> getBondedDevices(): Used to return a set of paired or bonded BluetoothDevice objects.
- boolean startDiscovery(): Used to start the discovery process.
Example: To enable, disable, and make discoverable Bluetooth programmatically:
activity_main.xml:
In the activity_main.xml file, we will drag a Textview and three buttons from the pallet.
<?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" tools:context=".MainActivity" android:transitionGroup="true"> <TextView android:text="Bluetooth Example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bluetooth" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#db5a6b" android:textSize="35dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn On" android:id="@+id/button" android:layout_below="@+id/textView" android:clickable="true" android:onClick="on" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get visible" android:onClick="visible" android:id="@+id/button2" android:layout_alignBottom="@+id/button" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="List devices" android:onClick="list" android:id="@+id/button3" android:layout_below="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="turn off" android:onClick="off" android:id="@+id/button4" android:layout_below="@+id/button3" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_below="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Paired devices:" android:id="@+id/textView2" android:textColor="#db5a6b" android:textSize="25dp" android:layout_below="@+id/button4" android:layout_alignLeft="@+id/listView" android:layout_alignStart="@+id/listView" /> </RelativeLayout> |
AndroidManifest.xml:
In the AndroidManifest.xml file, we will provide the below permissions:
Syntax:
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
File: AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.radioapp"> <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:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Activity class:(File: MainActivity.java)
In the MainActivity.java file, we will write the code to enable, disable, and to make the Bluetooth discoverable.
package com.example.radioapp; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.Set; public class MainActivity extends Activity { Button b1,b2,b3,b4; private BluetoothAdapter BA; private Set<BluetoothDevice>pairedDevices; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button) findViewById(R.id.button); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); BA = BluetoothAdapter.getDefaultAdapter(); lv = (ListView)findViewById(R.id.listView); } public void on(View v){ if (!BA.isEnabled()) { Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0); Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show(); } } public void off(View v){ BA.disable(); Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show(); } public void visible(View v){ Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(getVisible, 0); } public void list(View v){ pairedDevices = BA.getBondedDevices(); ArrayList list = new ArrayList(); for(BluetoothDevice bt : pairedDevices) list.add(bt.getName()); Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show(); final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); } } |
Output: