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?
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.");
}
Make sure bluetooth state is enabled before starting your Activity
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}
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