How to Display Bluetooth Paired Devices Programmatically in Android?
Bluetooth’s technology is a high-speed, low-powered wireless technology link designed to connect devices such as phones or other portable equipment. It has a specification (IEEE 802.15.1) for low-power radio communications to link computers, phones, and other network devices over a short distance in a wireless manner. Bluetooth signals cover distances, typically up to 10 meters or 30 feet. Bluetooth supports the waveband of 2.45 GHz and may support up to 721 kbps alongside three voice channels. This waveband has been put aside by international agreement to use Industrial, Scientific, and Medical devices (ISM).rd-compatible with 1.0 devices. Bluetooth is capable of connecting up to “eight devices” at a time. Every device offers a unique 48-bit address from the IEEE 802 standard. The Bluetooth specification defines and supports a variety of Bluetooth network connections. In this way, Bluetooth networking may be a remarkably flexible form of a wireless system for various short-range applications. Through this article, we want to share with you the implementation of an application that displays a list of Bluetooth Paired Devices along with their MAC IDs. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using both Java and Kotlin language.
Step by Step Implementation
To programmatically show a list of Bluetooth Paired devices against our device in Android, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the AndroidManifest.xml file
Go to the AndroidManifest.xml file and add these permissions required by the Bluetooth adapter: BLUETOOTH, BLUETOOTH_ADMIN, and ACCESS_COARSE_LOCATION.
<uses-permission android:name=”android.permission.BLUETOOTH”/>
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
Below is the complete code for the AndroidManifest.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "org.w3wiki.bluetoothpairedlist" > <!--Permissions Required for accessing Bluetooth services--> < uses-permission android:name = "android.permission.BLUETOOTH" /> < uses-permission android:name = "android.permission.BLUETOOTH_ADMIN" /> < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" /> < application android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" 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 > |
Step 3: Working with the activity_main.xml file
Now go to the activity_main.xml file which represents the UI of the application. Create a Layout that will display the Paired list of Bluetooth devices along with their MAC Addresses, and a Button to fetch them. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > <!--Button will perform a task to fetch the list of paired Bluetooth Devices--> < Button android:id = "@+id/btnGet" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:text = "Get Paired Devices" /> <!--A layout to display 2 text views, one consisting of names and the other displaying their mac ids--> < RelativeLayout android:id = "@+id/info" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@id/btnGet" android:layout_centerHorizontal = "true" > <!--Paired devices name--> < TextView android:id = "@+id/nameTv" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> <!--Paired devices mac ID--> < TextView android:id = "@+id/macAddressTv" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_toRightOf = "@id/nameTv" /> </ RelativeLayout > </ RelativeLayout > |
Step 4: Working with the MainActivity file
Go to the MainActivity file, and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.bluetooth.BluetoothAdapter import android.os.Build import android.os.Bundle import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { @RequiresApi (Build.VERSION_CODES.JELLY_BEAN_MR2) override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring the textView for name from the layout file val tvName = findViewById<TextView>(R.id.nameTv) // Declaring the textView for MAC ID from the layout file val tvMac = findViewById<TextView>(R.id.macAddressTv) // Declaring the button from the layout file val btn = findViewById<Button>(R.id.btnGet) // Initializing the Bluetooth Adapter val bAdapter = BluetoothAdapter.getDefaultAdapter() // Button Action when clicked btn.setOnClickListener { // Checks if Bluetooth Adapter is present if (bAdapter == null ) { Toast.makeText(applicationContext, "Bluetooth Not Supported" , Toast.LENGTH_SHORT).show() } else { // Arraylist of all the bonded (paired) devices val pairedDevices = bAdapter.bondedDevices if (pairedDevices.size > 0 ) { for (device in pairedDevices) { // get the device name val deviceName = device.name // get the mac address val macAddress = device.address // append in the two separate views tvName.append( "$deviceName\n" ) tvMac.append( "$macAddress\n" ) } } } } } } |
Java
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Set; public class MainActivity extends AppCompatActivity { TextView tvName, tvMac; Button btn; BluetoothAdapter bAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Declaring the textView for name from the layout file tvName = (TextView) findViewById(R.id.nameTv); // Declaring the textView for MAC ID from the layout file tvMac = (TextView) findViewById(R.id.macAddressTv); // Declaring the button from the layout file btn = (Button) findViewById(R.id.btnGet); // Initializing the Bluetooth Adapter bAdapter = BluetoothAdapter.getDefaultAdapter(); btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // Checks if Bluetooth Adapter is present if (bAdapter == null ) { Toast.makeText(getApplicationContext(), "Bluetooth Not Supported" , Toast.LENGTH_SHORT).show(); } else { // List all the bonded devices(paired) Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices(); if (pairedDevices.size() > 0 ) { for (BluetoothDevice device : pairedDevices) { // get the device name String deviceName = device.getName(); // get the mac address String macAddress = device.getAddress(); // append in the two separate views tvName.append(deviceName + "\n" ); tvMac.append(macAddress + "\n" ); } } } } }); } } |
Output: Run on Physical Device
Note: Some data is masked to maintain privacy.