Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getbondeddevices() not returning paired bluetooth devices

I am trying to enumerate all paired bluetooth devices with my device. In settings I can view the paired devices, but the following code does not return any items:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDeviceSet = bluetoothAdapter.getBondedDevices();

I have seen this and other posts that use this method, but I cannot seem to get it to work.

I have the following permissions in Manifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Furthermore, if I put one of the paired devices into discovery mode, and scan, then the device comes back as paired. If I check:

device.getBondState() == BluetoothDevice.BOND_BONDED

from the scan, it returns true.

What am I doing wrong or not understanding?

like image 637
Roy Hinkley Avatar asked Feb 12 '23 03:02

Roy Hinkley


2 Answers

Your code is completely correct. I have the exact same thing in my app and I never got any complain from any user that this feature does not work. Please check for other parts of your app. Below is the snippet of my app that does the same thing, and I have the same permission as you described.

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

if (pairedDevices.size() > 0) {
    findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
    for (BluetoothDevice device : pairedDevices) {
        mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
} else {
    mPairedDevicesArrayAdapter.add("No Paired Device.");
}
like image 153
Kasra Avatar answered Feb 15 '23 10:02

Kasra


Make sure bluetooth state is enabled before starting your Activity

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.enable();
}
like image 27
denispr00 Avatar answered Feb 15 '23 10:02

denispr00