Search code examples
androidlistviewbluetooth

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


Solution

  • 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));