I just cannot get Android to scan for Bluetooth devices. I've tried several different approaches, but none of them work. Here is my latest code.
// Create a BroadcastReceiver for ACTION_FOUND.
devicesFoundReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
devicesListArray.add(deviceName + "\n" + deviceHardwareAddress);
arrayAdapter.notifyDataSetChanged();
}
}
};
I register the receiver like this:
_thisContext.registerReceiver(devicesFoundReceiver, filter);
My manifest has these permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I call
bluetoothAdapter.startDiscovery();
OnReceive on the broadcastreceiver NEVER fires. I'm tired of looking at bajillion different websites, some with the same approach, other's don't. But NONE of them work. What am I doing wrong?
Here is the SDK version for reference:
minSdkVersion 26
targetSdkVersion 30