Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the bluetooth devices as a list?

I am trying to get my bonded bluetooth devices but I can get it as a long string instead of list.

This is my code:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList<String> listview =  new ArrayList<String>(Arrays.asList(pairedDevices.toString())); setListAdapter(new ArrayAdapter<String>(this, R.layout.list, listview)); 

I am getting something like this: [00:23:7F:1c, f0:09:f1:b4:b0]. And its all in one line. How can I change it to be in a list and not all in one line?

Also, how can I get the friendly names of the devices and not these numbers?

Thanks!!!

like image 691
roiberg Avatar asked May 29 '12 08:05

roiberg


People also ask

How do I get a list of Bluetooth devices?

Swipe down from the top of the screen. Touch and hold Bluetooth . If your accessory is listed under "Available media devices," next to your device's name, tap Settings . If no accessories are listed under "Previously connected devices," tap See all.

How do I get a list of Bluetooth devices on my Android phone?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.

What do you mean by Bluetooth listed 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.


2 Answers

You should change your code as below:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();  List<String> s = new ArrayList<String>(); for(BluetoothDevice bt : pairedDevices)    s.add(bt.getName());  setListAdapter(new ArrayAdapter<String>(this, R.layout.list, s)); 
like image 84
waqaslam Avatar answered Sep 30 '22 03:09

waqaslam


Find list of Nearby Bluetooth Devices:

Find Screenshot for the same.

enter image description here

MainActivity.java:

public class MainActivity extends ActionBarActivity {      private ListView listView;     private ArrayList<String> mDeviceList = new ArrayList<String>();     private BluetoothAdapter mBluetoothAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          listView = (ListView) findViewById(R.id.listView);          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();         mBluetoothAdapter.startDiscovery();          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);         registerReceiver(mReceiver, filter);      }       @Override     protected void onDestroy() {         unregisterReceiver(mReceiver);         super.onDestroy();     }      private final BroadcastReceiver mReceiver = new BroadcastReceiver() {         public void onReceive(Context context, Intent intent) {             String action = intent.getAction();             if (BluetoothDevice.ACTION_FOUND.equals(action)) {                 BluetoothDevice device = intent                         .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                 mDeviceList.add(device.getName() + "\n" + device.getAddress());                 Log.i("BT", device.getName() + "\n" + device.getAddress());                 listView.setAdapter(new ArrayAdapter<String>(context,                         android.R.layout.simple_list_item_1, mDeviceList));             }         }     }; 

activity_main.xml:

<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"     tools:context="com.example.bluetoothdemo.MainActivity" >      <ListView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/listView"/>  </RelativeLayout> 

Manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.bluetoothdemo"     android:versionCode="1"     android:versionName="1.0" >      <uses-sdk         android:minSdkVersion="8"         android:targetSdkVersion="21" />      <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=".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> 

Note:

Please make sure that you ask Location permission to your user and turn GPS On.

Reason: From Android 6.0 you need Location permission for Bluetooth Discovery.

More reference:

  1. https://developer.android.com/guide/topics/connectivity/bluetooth
  2. https://getlief.zendesk.com/hc/en-us/articles/360007600233-Why-does-Android-require-Location-Permissions-for-Bluetooth-

Done

like image 24
Hiren Patel Avatar answered Sep 30 '22 01:09

Hiren Patel