1

Came across a problem that sometimes BluetoothLeScanner can't detect anything for about 10 or 20 seconds.

(this problem can be reproduced by running the below code multiple times, scanner will not work for a while)

bluetoothAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
bluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback);

I tried to restart BluetoothLeScanner programmatically and manually turn on and off wifi + Bluetooth but it did not work, always need to wait 10 sec or so until the system detect the BLE signals.

I ended up turning airplane mode on and off, and everything works perfectly (works every time), I don't need to wait, just turn it on and off, and the system will detect the BLE signal.

What I'm trying to do is, make a button, when I click on it, the app will turn airplane mode on and off, but this seems not doable for Android 4.2 later version. Is there any way I can reproduce a similar function that works like airplane mode? (maybe be clear some cache?)

raxabizze
  • 445
  • 1
  • 4
  • 11

1 Answers1

2

Unfortunately turning on/off Airplan mode on Android v4.2 is not supported. Have a look at the links below:-

Is there any reason for using Android 4.2? It is a very outdated OS (10 years old) and Android has improved vastly since then especially when it comes to BLE API and functionality. If you have the option of upgrading to a newer OS, I would recommend that you start with this as your first priority.

If upgrading to a newer OS is not an option, then I would personally try to fix the actual problem instead of finding a workaround. One possibility of this happening is that you are scanning too often, which can sometimes result in no scan results being returned for about 30 seconds. This is a tricky one as the Android OS doesn't return an error - it just doesn't return any results which seems to be similar to what you are seeing. Have a look at this link and search for the string "notable scan errors". I would also look at the Logcat log as you can sometimes find more info there.

Finally, it is possible that this is somehow a problem with the hardware (especially if it is an old device). Try installing nRF Connect and scanning for BLE devices. If the issue exists over there as well, then that proves that it's a hardware/device problem. If the issue doesn't exist over there, then this is a problem with your app.

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
  • 1
    my bad for not being well described, I'm using Android 10. Seems like "Notable scan errors" is what I'm facing, sometimes my app runs "startscan" multiple times, this might be the problem, I'll look into it. Thanks! – raxabizze Jan 26 '22 at 08:05