0

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
  • Do you have got location services turned on on your device? On some (most) Android devices to get scan results you need: BLE permission, location permission, BLE on, location services on. Also, can you see BLE devices in your system settings? Do they get discovered after you get back from the settings to your app? – Mohru Dec 11 '20 at 22:06
  • Also, location permissions are runtime permissions. You need to request them in the code using `Activity.requestPermissions`. – Mohru Dec 11 '20 at 22:08
  • Before I register or even start discovering, I check if bluetooth is on and if the app has Coarse Location permission. Still not working –  Dec 11 '20 at 22:44
  • Just to be sure, do you have got also location services turned on (in the device settings)? Permissions are not enough. – Mohru Dec 12 '20 at 17:08
  • See https://stackoverflow.com/questions/33045581/location-needs-to-be-enabled-for-bluetooth-low-energy-scanning-on-android-6-0 or https://issuetracker.google.com/issues/148429135 – Mohru Dec 12 '20 at 18:32

1 Answers1

0

The Bluetooth related actions of mobile phones have nothing to do with net. Your problems are generally caused by the lack of authorized location permissions. When targetsdkversion 23 or above, you need to dynamically request mobile location permission“ android.permission.ACCESS_ FINE_ Location "can search for Bluetooth devices nearby. Of course, you can reduce the targetsdkversion version of the project to below 23. In this way, you do not need to apply for permission dynamically. Here are some good dynamic permission application open source library, I hope to help you: https://github.com/googlesamples/easypermissions https://github.com/permissions-dispatcher/PermissionsDispatcher

jacksinrow
  • 49
  • 4
  • I know. Before I even start discovering, I am checking if the app has these permissions granted. The app has location permission granted while the app is in use. But Bluetooth searching does absolutely not work?? –  Dec 12 '20 at 13:43