List all bluetooth devices android

Android Bluetooth List Paired Devices with Examples

In android, Bluetooth is a communication network protocol, which allows devices to connect wirelessly to exchange the data with other Bluetooth devices.

Generally, in android applications by using Bluetooth API’s we can implement Bluetooth functionalities, such as enable or disable a Bluetooth, searching for available Bluetooth devices, connecting with the devices and managing the data transfer between devices within the range.

In android, we can perform Bluetooth related activities by using BluetoothAdapter class in our applications. To know more about BluetoothAdapter, check this Android Bluetooth with Examples.

Android Bluetooth List Paired Devices

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.

// Get paired devices.
Set pairedDevices = bAdapter.getBondedDevices();
if (pairedDevices.size() > 0 ) // There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
>
>

If you observe above code, we are getting the Bluetooth paired devices name and mac address by using BluetoothDevice object.

As we discussed in previous tutorial Android Bluetooth with Examples, we need to set Bluetooth permissions in our android manifest file like show below to use Bluetooth features in our android applications.

Following is the example of getting the list of available Bluetooth paired devices on button click in android applications.

Android List Bluetooth Paired Devices Example

Create a new android application using android studio and give names as BluetoothListPairedDevicesExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.

activity_main.xml

< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= «match_parent»
android :layout_height= «match_parent»
android :orientation= «vertical» >
< Button
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :id= «@+id/btnGet»
android :text= «Get Paired Devices»
android :layout_marginLeft= «130dp»
android :layout_marginTop= «200dp»/>
< ListView
android :id= «@+id/deviceList»
android :layout_width= «match_parent»
android :layout_height= «wrap_content» >

Now open your main activity file MainActivity.java from \java\com.tutlane.bluetoothexample path and write the code like as shown below

MainActivity.java

package com.tutlane.bluetoothlistpaireddevicesexample;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity private ListView lstvw ;
private ArrayAdapter aAdapter ;
private BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button btn = (Button)findViewById(R.id. btnGet );
btn.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) if ( bAdapter == null ) Toast.makeText(getApplicationContext(), «Bluetooth Not Supported» ,Toast. LENGTH_SHORT ).show();
>
else Set pairedDevices = bAdapter .getBondedDevices();
ArrayList list = new ArrayList();
if (pairedDevices.size()> 0 ) for (BluetoothDevice device: pairedDevices) String devicename = device.getName();
String macAddress = device.getAddress();
list.add( «Name: » +devicename+ «MAC Address: » +macAddress);
>
lstvw = (ListView) findViewById(R.id. deviceList );
aAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout. simple_list_item_1 , list);
lstvw .setAdapter( aAdapter );
>
>
>
>);
>
>

Читайте также:  Boom audio bluetooth device manager

If you observe above code, we are getting the Bluetooth paired devices name and mac address by using BluetoothDevice object.

As discussed, we need to set Bluetooth permissions in android manifest file (AndroidManifest.xml) to access Bluetooth features in android applications. Now open android manifest file (AndroidManifest.xml) and write the code like as shown below

AndroidManifest.xml

If you observe above code, we added required Bluetooth permissions in manifest file to access Bluetooth features in android applications.

Output of Android Bluetooth List Paired Devices Example

When we run the above program in the android studio we will get the result as shown below.

Android Bluetooth List Paired Devices Example Result

When we click on Get Paired Devices button, we will get list of paired Bluetooth devices in our android application.

This is how we can get Bluetooth paired devices list in android applications based on our requirements.

Источник

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 pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList listview = new ArrayList(Arrays.asList(pairedDevices.toString())); setListAdapter(new ArrayAdapter(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.

6 Answers 6

You should change your code as below:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = mBluetoothAdapter.getBondedDevices(); List s = new ArrayList(); for(BluetoothDevice bt : pairedDevices) s.add(bt.getName()); setListAdapter(new ArrayAdapter(this, R.layout.list, s)); 

yea. Thanks. only need to add that i changed List s = new ArrayList(); to ArrayList s = new ArrayList(); it works great now.

have the same error after your update. did the same change i did before and it works now. this was the error it gave me befor my fix: The type List is not generic; it cannot be parameterized with arguments

The implementation for list item click listener totally depends on you. You may definitely extend AdapterView.OnItemClickListener to make Bluetooth connection to the device shown in list. Use listView.setOnItemClickListener to provide your custom click listener.

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 ArrayListmDeviceList = new ArrayList(); 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(context, android.R.layout.simple_list_item_1, mDeviceList)); > > >; 

activity_main.xml:

Читайте также:  Беспроводная автомобильная зарядка bluetooth

Manifest file:

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:

Do you know how can I connect it with a click? I mean if I have a list with all of my paired devices do you know how to connect it with 1 click?

I am trying this code.It is provide all paired devices. How can I get ReadyToPair device?Is there any way to achieve this thing?

ListPairedDevicesActivity.java

import java.util.Set; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListPairedDevicesActivity extends ListActivity < @Override protected void onCreate(Bundle savedInstanceState) < // TODO Auto-generated method stub super.onCreate(savedInstanceState); ArrayAdapterbtArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) < for (BluetoothDevice device : pairedDevices) < String deviceBTName = device.getName(); String deviceBTMajorClass = getBTMajorDeviceClass(device .getBluetoothClass() .getMajorDeviceClass()); btArrayAdapter.add(deviceBTName + "\n" + deviceBTMajorClass); >> setListAdapter(btArrayAdapter); > private String getBTMajorDeviceClass(int major) < switch(major)< case BluetoothClass.Device.Major.AUDIO_VIDEO: return "AUDIO_VIDEO"; case BluetoothClass.Device.Major.COMPUTER: return "COMPUTER"; case BluetoothClass.Device.Major.HEALTH: return "HEALTH"; case BluetoothClass.Device.Major.IMAGING: return "IMAGING"; case BluetoothClass.Device.Major.MISC: return "MISC"; case BluetoothClass.Device.Major.NETWORKING: return "NETWORKING"; case BluetoothClass.Device.Major.PERIPHERAL: return "PERIPHERAL"; case BluetoothClass.Device.Major.PHONE: return "PHONE"; case BluetoothClass.Device.Major.TOY: return "TOY"; case BluetoothClass.Device.Major.UNCATEGORIZED: return "UNCATEGORIZED"; case BluetoothClass.Device.Major.WEARABLE: return "AUDIO_VIDEO"; default: return "unknown!"; >> @Override protected void onListItemClick(ListView l, View v, int position, long id) < // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); >> 

AndroidBluetooth.java

import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class AndroidBluetooth extends Activity < private static final int REQUEST_ENABLE_BT = 1; private static final int REQUEST_PAIRED_DEVICE = 2; /** Called when the activity is first created. */ Button btnListPairedDevices; TextView stateBluetooth; BluetoothAdapter bluetoothAdapter; @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); btnListPairedDevices = (Button)findViewById(R.id.listpaireddevices); stateBluetooth = (TextView)findViewById(R.id.bluetoothstate); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); CheckBlueToothState(); btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener); >private void CheckBlueToothState()< if (bluetoothAdapter == null)< stateBluetooth.setText("Bluetooth NOT support"); >else< if (bluetoothAdapter.isEnabled())< if(bluetoothAdapter.isDiscovering())< stateBluetooth.setText("Bluetooth is currently in device discovery process."); >else < stateBluetooth.setText("Bluetooth is Enabled."); btnListPairedDevices.setEnabled(true); >>else < stateBluetooth.setText("Bluetooth is NOT Enabled!"); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); >> > private Button.OnClickListener btnListPairedDevicesOnClickListener = new Button.OnClickListener()< @Override public void onClick(View arg0) < // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(AndroidBluetooth.this, ListPairedDevicesActivity.class); startActivityForResult(intent, REQUEST_PAIRED_DEVICE); >>; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < // TODO Auto-generated method stub if(requestCode == REQUEST_ENABLE_BT)< CheckBlueToothState(); >if (requestCode == REQUEST_PAIRED_DEVICE) < if(resultCode == RESULT_OK)< >> > > 

AndroidManifest.xml

Источник

Android bluetooth get connected devices

How can I get a list of all connected bluetooth devices for Android regardless of profile? Alternatively, I see that you can get all connected devices for a specific profile via BluetoothManager.getConnectedDevices. And I guess I could see which devices are connected by listening for connections/disconnections via ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED. seems error prone. But I’m wondering if there’s a simpler way to get the list of all connected bluetooth devices.

Читайте также:  Bluetooth stream audio to android

you’re right in that just listening to acl connected / disconnect is problematic because it can occur while your app is not running or listening for the broadcasts

3 Answers 3

To see a complete list, this is a 2-step operation:

To get a list of, and iterate, the currently paired devices:

Set pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices(); if (pairedDevices.size() > 0) < for (BluetoothDevice d: pairedDevices) < String deviceName = d.getName(); String macAddress = d.getAddress(); Log.i(LOGTAG, "paired device: " + deviceName + " at " + macAddress); // do what you need/want this these list items >> 

Discovery is a little bit more of a complex operation. To do this, you’ll need to tell the BluetoothAdapter to start scanning/discovering. As it finds things, it sends out Intents that you’ll need to receive with a BroadcastReceiver.

First, we’ll set up the receiver:

private void setupBluetoothReceiver() < BroadcastRecevier btReceiver = new BroadcastReciver() < @Override public void onReceive(Context context, Intent intent) < handleBtEvent(context, intent); >>; IntentFilter eventFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); // this is not strictly necessary, but you may wish // to know when the discovery cycle is done as well eventFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); myContext.registerReceiver(btReceiver, eventFilter); > private void handleBtEvent(Context context, Intent intent) < String action = intent.getAction(); Log.d(LOGTAG, "action received: " + action); if (BluetoothDevice.ACTION_FOUND.equals(action)) < BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i(LOGTAG, "found device: " + device.getName()); >else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) < Log.d(LOGTAG, "discovery complete"); >> 

Now all that is left is to tell the BluetoothAdapter to start scanning:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); // if already scanning . cancel if (btAdapter.isDiscovering()) < btAdapter.cancelDiscovery(); >btAdapter.startDiscovery(); 

Источник

Obtaining a List of Available Bluetooth Devices on Android

In this question, @nhoxbypass provides this method for the purpose of adding found Bluetooth devices to a list:

private BroadcastReceiver myReceiver = new BroadcastReceiver() < @Override public void onReceive(Context context, Intent intent) < Message msg = Message.obtain(); String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action))< //Found, add to a device list >> >; 

However, I do not understand how a reference to the found device can be obtained, how can this be done? I do not have permission to comment on the original question, so I have chosen to extend it here.

2 Answers 2

In order to receive information about each device discovered, your application must register a BroadcastReceiver for the ACTION_FOUND intent. The system broadcasts this intent for each device. The intent contains the extra fields EXTRA_DEVICE and EXTRA_CLASS, which in turn contain a BluetoothDevice and a BluetoothClass, respectively.

This sample code is included as well:

@Override protected void onCreate(Bundle savedInstanceState) < . // Register for broadcasts when a device is discovered. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); >// Create a BroadcastReceiver for ACTION_FOUND. private final BroadcastReceiver mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) < // Discovery has found a device. Get the BluetoothDevice // object and its info from the Intent. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String deviceName = device.getName(); String deviceHardwareAddress = device.getAddress(); // MAC address >> >; 

If you are working with Bluetooth on Android, I suggest to read that guide carefully. Then read it one more time 😉

Источник

Оцените статью
Adblock
detector