TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Android bluetooth list paired devices example

Android bluetooth list paired devices example with examples of Activity and Intent, Fragments, Menu, Service, alarm manager, storage, sqlite, xml, json, multimedia, speech, web service, telephony, animation and graphics

<< Back to ANDROID

Android Bluetooth List Paired Devices Example

The getBoundedDevices() method of BluetoothAdapter class provides a set containing list of all paired or bounded bluetooth devices.

In this example, we are checking if the bluetooth is turned off, if yes then turn it on and list all the paired devices.

activity_main.xml

Drag one textview from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
<RelativeLayout xmlns:androclass="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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="61dp"
        android:text="Showing Paired Devices:" />

</RelativeLayout>

Provide Permission

You need to provide following permissions in AndroidManifest.xml file.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

The full code of AndroidManifest.xml file is given below.

File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:androclass="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothshowpaired"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bluetoothshowpaired.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

Let's write the code to provide the list of paired (bounded) bluetooth devices.

File: MainActivity.java
package com.example.bluetoothshowpaired;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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<BluetoothDevice> devices = btAdapter.getBondedDevices();
	        for (BluetoothDevice device : devices) {
	          textview1.append("\n  Device: " + device.getName() + ", " + device);
	        }
	      } else {
	        //Prompt user to turn on Bluetooth
	        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
	        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
	      }
	    }
	  }
	     

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}



You need to run it on the real device (e.g. mobile) to test the application.







Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf