Scan Continuously for Bluetooth Low Energy(BLE) Devices - android

I want to continuously scan for BLE Devices but as many posts from the internet and many answers from StackOverflow, starting with Android N, Android prevents an app from stopping and starting BLE scan more than 5 times in a window of 30 seconds.
I am not concerned about battery usage as this is for a specific purpose and will be continuously powered by cable and it is not for a normal end user.
I am not able to find the working solution so far.
is there any workaround/trick to achieve this with reliability?

Do you really have to stop and start scan that often? Your title says you want to scan continuously.
Also if you perform a filtered scan you can run it for how long time as you want.

Related

Android: Why is BluetoothLeScanner sometimes not discovering any BLE device?

I'm using BluetoothLeScanner to scan for BLE devices, which I start using:
startScan(null, settings.getScanSettings(), scanCallback);
Every once in a while, a call to startScan() does not discover any BLE device, even though some devices are actually advertizing (They are discovered using nRF Connect app).
When this happens, the next call to stopScan() and startScan() does discover devices.
I know that since Android 7, the number of start and stop scans is restricted to 5 times in a window of 30 seconds. However, I've noticed the problem sometimes also occurs if no stopScan()/startScan() has been called for more than 30 secs.
Has anyone experienced such issue ?
Could it be something else my app is doing while starting the scanner that could the scanning to fail ?
I have this problem using Android 10 and lower versions.
Android phone limited BLE scan duration to save energy. So you can not scan BLE advertising for a long time. It is system limitation so APP layer have no way to change that.
In older phone (maybe under 4.4) do not set the limitation, but it will make the phone getting hot.
If you want to scan BLE advertisement for a long time, change your design is a better way.

Android background BLE beacon scanning

I want to create an android application with background scanning of BLE beacon (like Eddystone or ibeacon). All I need is the detection of a beacon with a specific ID on my phone. I do not need distance between devices.
Is there any beacon with the ability to send signals through cca 3 walls in a building (or 1 flood up/down) in an area with a radius around 30m?
Can my app scan in the background every few seconds all day? (or just when Bluetooth is on, obviously...)
I tried some solutions, but android changed some politics about background running apps, or bluetooth receiver (to detect, when bluetooth is on). My solution sometimes works even 7 hours, but when I restart phone, the app is not restarted. I want a full background run, not foreground service with notification.
Thank you!
Two points:
All bluetooth beacon formats use the same radio transport mechanism. Radio power is limited by international regulatory bodies making transmitters weak.
The laws of physics decide how well they will travel between floors of a building, which will vary based on construction materials, but results will generally be poor. Again, this will be the same for all beacon formats.
Android 8+ limits detections of BLE in the background without a foreground service. You can do at most one scan every 10-25 minutes (this is randomized) when the phone is not in deep sleep mode. the only alternative is a foreground service with an ever present notification.
I realize these are not the answers you want to hear, but it is important to recognize and accept real-world limitations that exist and come up with creative solutions that work within these limitations.

It's possible for a mobile app connect to a BLE device and keep the connection alive for hours in background?

We have a device that need to be started at night, the device is started thanks to a bluetooth command sent by a mobile application. It's possible to send this command while the app is in the background and keep monitoring the device (i.e. receiving data), for example, for 30 minutes?
If it's possible what would be the ways to implement this feature in iOS and Android?
Update: Think about a sleep tracking device; the problem shouldn't be to scan all night because the user must connect the device before go to bed (and eventually try to automatically reconnect to a known device, without the need to scan). I'm asking if it's possible to stay connected and send/receive data while, for example, updating an internal database all night.
Several thing to take into account about Android BLE system development, read this for more information: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
BLE Scanning has an abuse prevention since Android 7. What does this means?
Prevention for an app stopping and starting BLE scans more than 5
times in a window of 30 seconds.
Long-running scans are converted into opportunistic scans. (30 minutes).
About the background process depends on what type of service you want. You can start a foreground service with a notification or you can start a Job which runs always in the background with no notification.
If you can be more precise on how the system is going to work I can give a more detailed explanation.

How to scan all bluetooth devices every second (or less)

I'm trying to scan for Bluetooth devices using the Bluetooth discovery method. At the moment I have implemented something similar to the following (top answer): How to periodically scan for bluetooth devices on android
However, as mentioned in the top answer this only scans every 12 seconds where I need this scan time to be reduced down to < 1s~. Can't seem to find much useful information online.
The Bluetooth modules I'm scanning for do support BLE, if that helps.
12 seconds is a default scanning interval and could be stopped by the cancelDiscovery(), so call this function after 1s of scanning.

Why does WiFi Scanning on Android take different times with different phones

android.net.wifi.WifiManager has the startScan() method to perform a passive scan of the WiFi channel and when the scanning is completed onReceive() method is called to access the WiFi channel readings.
However, as this webpage shows, which I have also confirmed with my own code implementation, the passive scanning of the WiFi channels take different times with different phones. Sometimes, some platforms are even around 10 times slower..
I would like to know what causes the phones to use so much time. Is it the driver? Is it some energy saving features? Or none of them and something very different is the reason?
The article gives you a hint:
Passive scans are slower to perform, because the device needs to
listen on every channel for some period of time, waiting for
broadcast beacons. Beacon frames are transmitted by APs periodically
to announce the presence of a wireless LAN. Beacon frames contain all
the information about its network. This approach consumes less energy,
since the radio doesn't use transceiver, but only the receiver. It
also takes more time to finish, since it has to listen on every
channel.
Some period of time is different for each device. If you listen for too short a time on a channel, you might miss a beacon frame. Too long and it might take a while to enumerate all the available APs when the user first scans a new location.
Furthermore, I didn't see actual details on how those results were generated. One might imagine a smart algorithm would use a longer listen time when first in a new location but switch to shorter ones after it's been there for a while.

Categories

Resources