AdSense

AdSense3

Wednesday, 28 October 2015

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
  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.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="18dp"  
  14.         android:layout_marginTop="61dp"  
  15.         android:text="Showing Paired Devices:" />  
  16.   
  17. </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.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <uses-permission android:name="android.permission.BLUETOOTH" />  
  12.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
  13.       
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name="com.example.bluetoothshowpaired.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.     </application>  
  29.   
  30. </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.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import java.util.Set;  
  7. import android.bluetooth.BluetoothAdapter;  
  8. import android.bluetooth.BluetoothDevice;  
  9. import android.content.Intent;  
  10. import android.widget.TextView;  
  11.    
  12. public class MainActivity extends Activity {  
  13.       TextView textview1;  
  14.       private static final int REQUEST_ENABLE_BT = 1;  
  15.       BluetoothAdapter btAdapter;   
  16.        
  17.       /** Called when the activity is first created. */  
  18.       @Override  
  19.       public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.        
  23.         textview1 = (TextView) findViewById(R.id.textView1);  
  24.            
  25.         // Getting the Bluetooth adapter  
  26.         btAdapter = BluetoothAdapter.getDefaultAdapter();  
  27.         textview1.append("\nAdapter: " + btAdapter);  
  28.            
  29.         CheckBluetoothState();  
  30.       }  
  31.            
  32.       /* It is called when an activity completes.*/  
  33.       @Override  
  34.       protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  35.         super.onActivityResult(requestCode, resultCode, data);  
  36.         if (requestCode == REQUEST_ENABLE_BT) {  
  37.           CheckBluetoothState();  
  38.         }  
  39.       }  
  40.        
  41.       @Override  
  42.       protected void onDestroy() {  
  43.         super.onDestroy();  
  44.       }  
  45.        
  46.       private void CheckBluetoothState() {  
  47.         // Checks for the Bluetooth support and then makes sure it is turned on  
  48.         // If it isn't turned on, request to turn it on  
  49.         // List paired devices  
  50.         if(btAdapter==null) {   
  51.           textview1.append("\nBluetooth NOT supported. Aborting.");  
  52.           return;  
  53.         } else {  
  54.           if (btAdapter.isEnabled()) {  
  55.             textview1.append("\nBluetooth is enabled...");  
  56.                
  57.             // Listing paired devices  
  58.             textview1.append("\nPaired Devices are:");  
  59.             Set<BluetoothDevice> devices = btAdapter.getBondedDevices();  
  60.             for (BluetoothDevice device : devices) {  
  61.               textview1.append("\n  Device: " + device.getName() + ", " + device);  
  62.             }  
  63.           } else {  
  64.             //Prompt user to turn on Bluetooth  
  65.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  66.             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  67.           }  
  68.         }  
  69.       }  
  70.            
  71.   
  72.     @Override  
  73.     public boolean onCreateOptionsMenu(Menu menu) {  
  74.         // Inflate the menu; this adds items to the action bar if it is present.  
  75.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  76.         return true;  
  77.     }  
  78.   
  79. }  

No comments:

Post a Comment