Blog Details

img
Design

How do I find paired Bluetooth devices on Android

Spoke Right / 18 Nov, 2023

To return a set of paired or bonded BluetoothDevice objects, the getBoundedDevices() method of BluetoothAdapter class is used.

Example:

In the below example, we are demonstrating the use of the BluetoothAdapter class to check if the bluetooth is turned off. Here, we are turning the bluetooth on, if it is off and then we are listing all the paired devices.

activity_main.xml:

In the activity_main.xml file, we will drag a Textview from the pallet.



 
    
 

AndroidManifest.xml:

In the AndroidManifest.xml file, we will provide the below permissions:

Syntax:


File: AndroidManifest.xml:



    
    
 
    
        
            
                
 
                
            
        
    
 

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to provide the list of paired or bounded bluetooth devices.

package com.example.radioapp;
 
import android.os.Bundle;
import android.app.Activity;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    TextView textview1;
    private static final int REQUEST_ENABLE_BT = 1;
    BluetoothAdapter btAdapter;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textview1 = (TextView) findViewById(R.id.textView1);
 
        // Getting the Bluetooth adapter
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        textview1.append("\nAdapter: " + btAdapter);
 
        CheckBluetoothState();
    }
 
    /* It is called when an activity completes.*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT) {
            CheckBluetoothState();
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
 
    private void CheckBluetoothState() {
        // Checks for the Bluetooth support and then makes sure it is turned on
        // If it isn't turned on, request to turn it on
        // List paired devices
        if(btAdapter==null) {
            textview1.append("\nBluetooth NOT supported. Aborting.");
            return;
        } else {
            if (btAdapter.isEnabled()) {
                textview1.append("\nBluetooth is Enabled.");
 
                // Listing paired devices
                textview1.append("\nPaired Devices are:");
                Set devices = btAdapter.getBondedDevices();
                for (BluetoothDevice device : devices) {
                    textview1.append("\n  Device Name: " + device.getName());
                }
            } else {
                //Prompt user to turn on Bluetooth
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0