2

I'm trying to see whether if in addition to the Bluetooth on, paired with any device. To see if the Bluetooth is on, I use the following:

BluetoothAdapter.getDefaultAdapter().isEnabled()

I need to know if that BluetoothAdapter, is paired with any device.

Thank you and I hope your answers

EDIT

If I use:

BluetoothAdapter.getDefaultAdapter().getBondedDevices();

And size() > 0, Does that mean paired? Or are already stored devices?

EDIT

Excuse me, but what I need is not to get the list of paired devices, but if some of those devices that has already been paired at some point is connected to my Smartphone

2 Answers2

3

You can query paired devices using this:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

Before performing device discovery, its worth querying the set of paired devices to see if the desired device is already known. To do so, call getBondedDevices(). This will return a Set of BluetoothDevices representing paired devices. For example, you can query all paired devices and then show the name of each device to the user, using an ArrayAdapter.

As official docs described here and nice sample for simple Bluetooth chat application here.

Tinko
  • 502
  • 1
  • 4
  • 14
  • Yes, that was watching, but not if when you call the `getBondedDevices()` function returns what you are devices that you already have paired / saved, or if the connection is established. Perhaps what you need is not whether this matched but if the mobile `Bluetooth` is on and connected to another device – Sergio Antonio Snchez Camarero Jul 14 '16 at 09:26
  • 1
    This returns only list of paired devices saved on your device so you paired with them before in attempt to send some data. You allowed them to communicate with you, you don't have to discover them again and pair again because you already did it and by this code you can get all neccessary data about these devices in order to communicate with them again (such as MAC address). – Tinko Jul 14 '16 at 09:31
  • 1
    And paired devices you get by this code don't have to be near you or they can have their Bluetooth disabled so this call doesn't give you paired enabled devices near you "waiting" for you to connect to them. It only returns list of devices you communicate before. – Tinko Jul 14 '16 at 09:39
  • Ok @Tinko ,perhaps the function I have to use is getConnectionDevices () to get the list of connected devices and if any (size> 0) then there is not any connected device? – Sergio Antonio Snchez Camarero Jul 14 '16 at 09:45
  • And why do you want to do that? You are managing your connections so you should know all devices you are connected to. And I can't find this method in `BluetoothAdapter` class. – Tinko Jul 14 '16 at 09:51
  • This method is of `BluetoothA2DP` class @Tinko. What I want is to see if it is connected with the `BluetoothHeadset` device to call a function from this device (that if I checked) or from other side – Sergio Antonio Snchez Camarero Jul 14 '16 at 10:01
  • You mean something like [this](http://stackoverflow.com/questions/12509135/how-to-get-bluetooth-connected-devices-using-bluetoothheadset-api)? – Tinko Jul 14 '16 at 10:03
  • Si y no @Tinko, yo necesito saber si hay 1 dispositivo conectado (`BluetoothHeadset`) y de ser así lanzo el reconocimiento de voz desde el main o desde el `BluetoothHeadset` – Sergio Antonio Snchez Camarero Jul 14 '16 at 10:13
  • I think with BluetoothManager.getConectedDevices() to find devices connected, and if to check any particular I look by name -> getName () or by address getAddress(). Thanks @Tinko ofr your help and your replies – Sergio Antonio Snchez Camarero Jul 15 '16 at 07:00
1

Get the already paired devices list use this:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices == null || pairedDevices.size() == 0) {
             Toast.makeText(getApplicationContext(),"No Paired Devices Found",Toast.LENGTH_SHORT).show();
        } else {
            ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();
            list.addAll(pairedDevices);           
        }
Hanzala
  • 1,965
  • 1
  • 16
  • 43
Hari Haran
  • 1,543
  • 4
  • 13
  • 25
  • Ok, but with that list of devices that returns me getBondedDevices () function, I get the list of devices that have already been paired at some point not? I need to know is whether any of these devices that I get on that list is connected to my Smartphone – Sergio Antonio Snchez Camarero Jul 14 '16 at 09:33
  • Ok cool, Once we paired then we know the device name and mac address , Use that mack address to make a Bluetooth socket connection with other device.Kindly refer this link you will get some idea https://androidcookbook.com/Recipe.seam;jsessionid=9B476BA317AA36E2CB0D6517ABE60A5E?recipeId=1665 – Hari Haran Jul 14 '16 at 09:43
  • ok thanks @HariHaran, but what I want is to check if there is connection between the smartphone and the Bluetooth device, do not create the connection between the two devices – Sergio Antonio Snchez Camarero Jul 14 '16 at 09:47