Showing list of all paired devices in Android

In this article, we are going to show the list of all the paired devices while your Bluetooth is turn on in Android.
Submitted by Manu Jemini, on January 30, 2018

First, we learn the method to use BluetoothAdapter, BluetoothDevice and check the all connected devices to our device. In the example below, we are going to use the Bluetooth.BluetoothAdapter and Bluetooth.BluetoothDevice classes to have all the functionality required for BluetoothAdapter. This BluetoothAdapter.getDefaultAdapter() will create an object which can be used to process the current Blutooth.

To implement this in your program you will need to import: android.bluetooth.BluetoothAdapter, android.bluetooth.BluetoothDevice in your java file. Now on your XML File we need a button for switching the mode. For this just add button like this:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of paired devices"
        android:id="@+id/button"
        android:clickable="true"
        android:onClick="Paired_devices_list"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp" />

    <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/button"
        android:layout_marginTop="20dp" />

Now, this button have a onClick attribute which is referring to a function called paired_devices_list, this function is implemented in JAVA file with corresponding logics to check all the current devices connected to our own device and use the Adapter to make a ListView. By this line pairedDevices = B.getBondedDevices(), we will have all the connected devices inside pairedDevices variable. After this we will loop through it and will make a List View out of it. In the end we will make a Toast.

Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

1) Java file:

package com.example.hp.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;


public class MainActivity extends AppCompatActivity {
    BluetoothAdapter B;
    Set<BluetoothDevice> pairedDevices;
    ListView L;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        B=BluetoothAdapter.getDefaultAdapter();
        L=(ListView)findViewById(R.id.listView);
    }
    public void Paired_devices_list(View v){
        pairedDevices = B.getBondedDevices();

        ArrayList list = new ArrayList();

        for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
        Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

        ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

        L.setAdapter(adapter);
    }

}

2) XML file:

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of paired devices"
        android:id="@+id/button"
        android:clickable="true"
        android:onClick="Paired_devices_list"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp" />

    <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/button"
        android:layout_marginTop="20dp" />

</RelativeLayout>

3) Menifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

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

Output Images

Showing list of all paired devices in Android 1

Showing list of all paired devices in Android 2

Showing list of all paired devices in Android 3

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.