MeSterious
MeSterious

Reputation: 95

How can I count the number of devices connected via bluetooth?

I want to connect 4 devices via Bluetooth in my app and after 4 devices are connected I want to stop any more devices to get connected.To do this I need to know the number of connected devices.

How can I find the number of connected devices programmatically?

Upvotes: 0

Views: 708

Answers (2)

p_anantha
p_anantha

Reputation: 352

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); Set devices = btAdapter.getBondedDevices();

    if (devices.size() == 0) {
        Log.i(TAG, "  No paired devices : ");
    }
    else if (devices.size() > 1) {

        Log.i(TAG, "  Too many paired devices : " + devices.size() );

        // Loop and dump the paired devices
        for (BluetoothDevice device : devices) {
            Log.i(TAG, "  Device: " + device.getName() + ", " + device);
            Log.i(TAG, "  Address: " + device.getAddress() + ", " + device);
        }
    }
    else {
        retVal = devices.iterator().next().getAddress();
    }

Upvotes: 0

shb
shb

Reputation: 6277

getBondedDevices() returns a set of currently paired devices you can get the number of connected devices from its size()

Set<BluetoothDevice> devices =  bluetoothAdapter.getBondedDevices();
//deviceS.size()

Upvotes: 1

Related Questions