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 connection. 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 the Kotlin language. 

Display Bluetooth Paired devices

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: Woking 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
    package="org.geeksforgeeks.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>

chevron_right


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 which 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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>

chevron_right


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. 

Kotlin

filter_none

edit
close

play_arrow

link
brightness_4
code

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")
                    }
                }
            }
        }
    }
}

chevron_right


Output: Run on Physical Device

Note: Some data is masked to maintain privacy.

Display Bluetooth Paired devices




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.