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!!!
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.
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.
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.
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));
Find list of Nearby Bluetooth Devices:
Find Screenshot for the same.
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:
Done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With