Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

List Paired Devices


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
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <TextView
  7.         android:id=“@+id/textView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“18dp”
  13.         android:layout_marginTop=“61dp”
  14.         android:text=“Showing Paired Devices:” />
  15. </RelativeLayout>

Provide Permission

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

  1. <uses-permission android:name=“android.permission.BLUETOOTH” />
  2. <uses-permission android:name=“android.permission.BLUETOOTH_ADMIN” />

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

File: AndroidManifest.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <manifest xmlns:androclass=“http://schemas.android.com/apk/res/android”
  3.     package=“com.example.bluetoothshowpaired”
  4.     android:versionCode=“1”
  5.     android:versionName=“1.0” >
  6.     <uses-sdk
  7.         android:minSdkVersion=“8”
  8.         android:targetSdkVersion=“17” />
  9.     <uses-permission android:name=“android.permission.BLUETOOTH” />
  10.     <uses-permission android:name=“android.permission.BLUETOOTH_ADMIN” />
  11.     <application
  12.         android:allowBackup=“true”
  13.         android:icon=“@drawable/ic_launcher”
  14.         android:label=“@string/app_name”
  15.         android:theme=“@style/AppTheme” >
  16.         <activity
  17.             android:name=“com.example.bluetoothshowpaired.MainActivity”
  18.             android:label=“@string/app_name” >
  19.             <intent-filter>
  20.                 <action android:name=“android.intent.action.MAIN” />
  21.                 <category android:name=“android.intent.category.LAUNCHER” />
  22.             </intent-filter>
  23.         </activity>
  24.     </application>
  25. </manifest>

Activity class

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

File: MainActivity.java
  1. package com.example.bluetoothshowpaired;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import java.util.Set;
  6. import android.bluetooth.BluetoothAdapter;
  7. import android.bluetooth.BluetoothDevice;
  8. import android.content.Intent;
  9. import android.widget.TextView;
  10. public class MainActivity extends Activity {
  11.       TextView textview1;
  12.       private static final int REQUEST_ENABLE_BT = 1;
  13.       BluetoothAdapter btAdapter;
  14.       /** Called when the activity is first created. */
  15.       @Override
  16.       public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         textview1 = (TextView) findViewById(R.id.textView1);
  20.         // Getting the Bluetooth adapter
  21.         btAdapter = BluetoothAdapter.getDefaultAdapter();
  22.         textview1.append(“\nAdapter: “ + btAdapter);
  23.         CheckBluetoothState();
  24.       }
  25.       /* It is called when an activity completes.*/
  26.       @Override
  27.       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  28.         super.onActivityResult(requestCode, resultCode, data);
  29.         if (requestCode == REQUEST_ENABLE_BT) {
  30.           CheckBluetoothState();
  31.         }
  32.       }
  33.       @Override
  34.       protected void onDestroy() {
  35.         super.onDestroy();
  36.       }
  37.       private void CheckBluetoothState() {
  38.         // Checks for the Bluetooth support and then makes sure it is turned on
  39.         // If it isn’t turned on, request to turn it on
  40.         // List paired devices
  41.         if(btAdapter==null) {
  42.           textview1.append(“\nBluetooth NOT supported. Aborting.”);
  43.           return;
  44.         } else {
  45.           if (btAdapter.isEnabled()) {
  46.             textview1.append(“\nBluetooth is enabled…”);
  47.             // Listing paired devices
  48.             textview1.append(“\nPaired Devices are:”);
  49.             Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
  50.             for (BluetoothDevice device : devices) {
  51.               textview1.append(“\n  Device: “ + device.getName() + “, “ + device);
  52.             }
  53.           } else {
  54.             //Prompt user to turn on Bluetooth
  55.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  56.             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  57.           }
  58.         }
  59.       }
  60.     @Override
  61.     public boolean onCreateOptionsMenu(Menu menu) {
  62.         // Inflate the menu; this adds items to the action bar if it is present.
  63.         getMenuInflater().inflate(R.menu.activity_main, menu);
  64.         return true;
  65.     }
  66. }

download this android example

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

Comments

comments