I'm creating an app and I have a problem with getting all bluetooth paired devices. Here is my DeviceListActivity that is supposed to get and put all the devices in a listview.
public class DeviceListActivity extends AppCompatActivity {
private BluetoothAdapter mBtAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_list);
ArrayAdapter<String> pairedDeviceArrayAdapter = new ArrayAdapter<>(this, R.layout.device_name);
ListView pairedDevicesListView = findViewById(R.id.paired_devices);
pairedDevicesListView.setAdapter(pairedDeviceArrayAdapter);
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)
{
pairedDeviceArrayAdapter.add(device.getName()+"
"+device.getAddress());
}
}
else
{
pairedDeviceArrayAdapter.add("no paired devices "+pairedDevices.size());
}
}
Unfortunately no devices are recognized and it goes directly in the else condition ("no paired devices" is displayed). My phone, near my laptop, with the Bluetooth enable is not recognized and I don't know why. I also verified that the android studio phone emulator had the bluetooth enable. Furthermore i have added those two permissions in the manifest file :
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
I don't know where the issue should come from.