I am runnning an app or service with a active Bluetooth LE scanner and showing scan results on Log console. If I lock the phone in a table and not touching anymore. After a time it stops, and it doesn't give me more scan results.
If I press power button and the screen wake up it gives me more scan results. If i lock again the screen or wait to lock automatically it stops and not give me more scan results.
I test with service and an app that give me more results by Log and see the app is running but scanner LE stops and no give me more results while the screen is lock.
I have the app in "no optimized battery" for doze mode. I test forcing by command introducing the phone en doze mode and work fine it give me scan results.
In my Nexus 5 with Android 7.1.1 pass when running APP and lock the screen and not touch anymore the phone. The time is 30 minutes. The phone is in a table alone, only connected with microusb to see the log in android studio.
In other Moto G2 with android 7.1 pass exactly but the time is between 20 minutes and 40 minutes, it is more aleatory. The phone is in a table alone, only connected with microusb to see the log in android studio.
For have running well again, I need to force close the app manually and restart, otherwise only works when screen is active and no give me more results when screen is locked.
This is used for beacon results, first I use Android Beacon Library for this purpose and the result was the same.
I think it is a problem of android bluetooth component, because I have the same result with the Android Beacon Library or if I implement my own BLE Scanner, but I don't know how to solve this.
Are any form to use Bluetooth Scanner LE always running in Android when the phone is much time in lock state??
Thanks in advance.
Best regards.
Android 7.0 introduced a BLE scan timeout, where any scan running for 30 minutes or more is effectively stopped automatically and only resumed "opportunistically" which essentially means that if another process does a scan, it can get the results as well.
You can see this by setting up code to start a Bluetooth LE scan and leave it running indefinitely. After exactly 30 minutes, the scan will stop, and you will see entries like this in LogCat:
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: clientIf set to scan opportunisticly: 6
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue=1
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - ScanSetting Scan mode=-1 mLastConfiguredScanSetting=2
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue emtpy, scan stopped
06-11 19:00:22.849 5123 5147 D BtGatt.ScanManager: stop scan
You can see the code that does this in the AOSP source here:
https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-7.0.0_r1/src/com/android/bluetooth/gatt/ScanManager.java#72
A workaround for this is to not keep scans going that long. You can simply stop them and restart them periodically.
Related
I am trying to perform BLE scans in a foreground service (Android 12) after some start scan end stop scan I receive the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED, I cannot figure out what is the reason of the error.
I do startScan and stopScan in sequence (the scan is a long-running scan)
The app target SDK 32 so I have the new Bluetooth permissions.
This is the complete scenario:
I open the app and I start the foreground service
I close the application (the foreground service is still running)
The foreground service scan and connect to my device and then I stop scanning
I turn off my BLE device and the foreground service start to scan again
I turn on my ble device and the foreground service detect it and connect to it.
Repeat step 4 and suddenly the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED show.
I also notice this error in the logcat App 'com.sample.xxx' is scanning too much clientif (I look into AOSP source code and seems that this string is specific of Xiaomi ROM)
https://github.com/appersiano/TestingBackgroundConnection
P.S. Tested on Xiaomi Mi11 Lite 5g
How can I solve?
you should not scan more than 5 times in 30s.
GattService:
This constant defines the time window an app can scan multiple times.
Any single app can scan up to |NUM_SCAN_DURATIONS_KEPT| times during
this window.
Once they reach this limit, they must wait until their earliest recorded scan exits this window.
static final int NUM_SCAN_DURATIONS_KEPT = 5;
static final long EXCESSIVE_SCANNING_PERIOD_MS = 30 * 1000;
I use android phone to detect a beacon using startScan method, the interval time of beacon adversing is 800ms, but I got the beacon data from callback of startScan every 3~10 seconds. My expectation is I can get the data every 1 second.
Below is my codes. So is there any other ways to get my expectation. Or someone can explain why the interval time is not the same as the adversing time ?
ScanSettings.Builder builder = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
// scan filters has been set the right value.
bluetoothLeScanner.startScan(scanFilters, builder.build(), scanCallback);
Nothing about the code shown is a problem. I can confirm your expectation is met with equivalent code in other apps on other devices. This suggests a problem with :
Your BLE device not advertising as often as you expect
Your Android phone (possible but unlikely)
Some other aspect of your code that you did not show in your question.
Is you BLE device a beacon? If so, I suggest:
Install BeaconScope on the same phone, scan for your beacon, then check the PPS rating (Packets Per Second -- scan for at least 30 seconds until the measurement stabilizes). If you see a value of 0.1-0.3 then the problem is NOT with your code and must be (1) or (2). If you see a much higher PPS value, the problem is with some other aspect of your code not shown.
Use a second Android phone with BeaconScope as a transmitter and transmit a beacon at the 10 Hz default rate. Does your app get a packet every 100-200ms? If so, you have confirmed the problem is with your BLE device not transmitting as much as you expect.
If your BLE device is not a beacon you can at least perform the second test.
It's recommended to use 'SCAN_MODE_LOW_LATENCY' mode only hen the application is running in the foreground.
Incase you are in background, you can run a sticky foreground service and set a periodic frequency of 1 sec for your service.
Also scanning Ble for every 1 sec might be extremely heavy for the app.
Hello I am poor at English.
I use Android Beacon Library
implementation 'org.altbeacon:android-beacon-library:2.15.2'
My Android application gradle imformation
compileSdkVersion 28
minSdkVersion 21
targetSdkVersion 28
My Beacon manager information
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.setForegroundScanPeriod(1100L);
beaconManager.setBackgroundScanPeriod(1100L);
beaconManager.setForegroundBetweenScanPeriod(0);
beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.enableForegroundServiceScanning(notificationcompatBuilder_Scannging.build(),456);
beaconManager.setEnableScheduledScanJobs(false);
BeaconManager.setDebug(true);
regionBootstrap = new RegionBootstrap(this, myBeaconRegion);
Error
I want to develop all day beacon scanner application.
So I use regionBootstrap to scan in background.
It looked like it was working.
However time passed and Exit Event suddenly occurred and Enter Event occurred shortly after.
I think scan does not work for a few seconds and I have conducted a test on this.
Error Test 1
one smartphone , two beacons
Error1
One beacon signal is not detected for more than 10 seconds while another beacon signal is detected.
So I think scan work well and it is a problem of beacon.
Error Test 2
two smartphone , two beacons
smartphone 1
enter image description here
smartphone 2
enter image description here
In smartphone 1 Logcat, Also One beacon signal is not detected for more than 10 seconds while another beacon signal is detected.
In smartphone 2 Logcat, Unscanned beacon is being scanned at the same time.
So beacon does not problem and I can't find reason.
Error Test 3
Added test
two smart phone
My beacon transmit the signal about once every three seconds.
In smart phone 1, download the other beacon application which has beacon signal transmit and transmit at 1Hz.
In smart phone 2, by using my application, watch the signal.
enter image description here
The beacon signal of smart phone 1 increase markedly, but the same problem arose.
Does anyone know about this matter?
I want a help...
Please
I suspect that the beacons you are using to test are not transmitting frequently enough. Manufacturers often reduce the transmission rate to save battery. When the transmission rate gets below 1Hz, this can cause problems, because not 100% of beacon packets transmitted are received by any phone. Radio noise often causes packets to be missed. If one device misses a few packets in a row (it happens sometimes) it causes a region exit event.
The solution is to increase the frequency at which the beacons transmit. With some manufacturers this is configurable. With others, it is not. If it is not configurable, you may need to buy different beacons
You can test if this is the problem by using an Android beacon simulator app on one of the devices. Try my BeaconScope app, which will advertise at 10Hz by default.
This library return result only when a specific event is triggered, not for all beacon scanned.. if you need this I suggest you to use the standart android standard beacon library, if you want to know more about this and how to implements comment this post and I'll edit it with the implementation
I'm looking for help finding a workaround for an issue that appears to be a problem with a recent security update for the Samsung S5 and Note 4 devices causing BLE device to not connect. The update was distributed by Verizon in early August and the Security Update date is: July 1, 2017 with a build number ending in DQF2. We are unable to connect to our BLE devices from the phones that have this specific update.
The BLE connection is made after scanning and finding the device. The scan works fine and devices are found normally but the connection goes nowhere. It's done like this:
BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
device.connectGatt(context, true, btGattCallback);
However, I do not get any update from the callback is never notified of a connection with autoConnect = true, and with autoConnect = false, the callback only gets a connection update saying it disconnected (never gets to a connected state).
Before this update the connection was very reliable but fails 100% of the time since that update. Even a hard reset of the phone and BLE device and it still won't connect as well.
I've experimented with different things like making the connection on the main thread, ensuring that BLE scanning has stopped before connecting (we do this already but I added some extra delay as well in case of a race condition). I've added delays in different parts and experimented with both autoConnect on and off and no luck so far.
This definitely seems to be an issue with Samsung and I can see that the FitBit forums are lit up with users having this issue and while our product doesn't have as large of a base as FitBit it is effecting many people.
The only thing interesting I could find from logcat included:
08-12 14:11:17.848 4412-4602/? D/bt_upio: upio_set : pio 0 action 2, polarity 1
08-12 14:11:17.848 4412-4602/? D/bt_upio: BT_WAKE is asserted already
08-12 14:11:17.848 4412-4597/? D/BtGatt.GattService: [GSIM LOG]: increaseScanCount 4 Battery : com.myapp.internal.debug : 7
08-12 14:11:17.858 4412-4556/? D/bt_btif_gattc: btif_gattc_update_properties BLE device name=Axon-X81001976 len=14 dev_type=2
08-12 14:11:17.858 4412-4556/? E/bt_btif: property type:241, len:0 is invalid
08-12 14:11:17.858 4412-4556/? E/bt_btif_dm: ### ASSERT : system/bt/main/../btif/src/btif_dm.c line 4655 failed to save remote device manufacturer (1) ###
Other related threads:
VZW S5 thread
VZW Note 5 thread
Samsung Developer thread
Square contactless reader thread
Has anyone identified a way to make valid BLE connections on these devices?
We are making an app that uses BLE advertisements to create first a list of our industrial devices. Then of course something else happens but this is not relevant for the question. What is important is that we need to receive BLE advertisements continuously.
There is a service-like object that starts scan ble, waits about 2 seconds then stops it.
After 500 milliseconds it starts again.
This continues endless.
Here below the code snippet to start the scan, for completeness, just standard code, as one can see:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
bluetoothLeScanner.startScan(null, new ScanSettings.Builder().
setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(0)
.build(), bleScannerCallback);
ok = true;
} else {
ok = mBluetoothAdapter.startLeScan(bleScannerCallbackPreLollipop);
}
return ok;
the bleScannerCallback implements the callback to receive the onScanResult().
What I'm experiencing right now is:
with some device, for example Samsung S6 with Android 5.1.1, within each scan cycle all devices in the proximity are received, i.e. onScanResult() runs with all BLE advertisements.
With some other device, like Tablet Nexus 9 with Android 7.0, everything runs fine for about 20 seconds, then for about 17/18 seconds NO advertisement is delivered through onScanResult(), then again ok for about 20 seconds, then again a break for about 17/18 seconds.
This process repeats infinitely, even with some regularity, it does not even seem random...
Is this a known issue?
It seems to appear specially with Android >= 6, our Samsung S6 with Android 5.1.1 runs all the times perfectly.
Obviously all the required Android permissions are in place, elsewhere it would not even run once.
It seems to me more a "feature" on the Android device to save some battery, unfortunately we need to scan continuously, at least in some condition, not really 24/7 but even not with about 20 seconds break every 20 seconds.
In the case this is the way it shall go in the future, does anyone know any other solution that allows to scan BLE devices continuously? Independently on the Android version...anyway the newer versions like >= 6.