Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'App is scanning too frequently' with ScanSettings.SCAN_MODE_OPPORTUNISTIC

I noticed an issue on Samsung S8, Android 7.0 (upd. This also happens on Android 7.0: Samsung S7, Nexus 5x) which tells (after couple of tests) that app is scanning too frequently:

08-14 12:44:20.693 25329-25329/com.my.app D/BluetoothAdapter: startLeScan(): null 08-14 12:44:20.695 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.696 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.698 25329-25329/com.my.app D/BluetoothLeScanner: Start Scan 08-14 12:44:20.699 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.700 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.700 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.701 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON 08-14 12:44:20.703 4079-4093/? D/BtGatt.GattService: registerClient() - UUID=dbaafee1-caf1-4482-9025-b712f000eeab 08-14 12:44:20.807 4079-4204/? D/BtGatt.GattService: onClientRegistered() - UUID=dbaafee1-caf1-4482-9025-b712f000eeab, clientIf=5, status=0 08-14 12:44:20.808 25329-25342/com.my.app D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0 08-14 12:44:20.809 4079-7185/? D/BtGatt.GattService: start scan with filters 08-14 12:44:20.811 4079-7185/? D/BtGatt.GattService: getScanSettings  08-14 12:44:20.812 4079-7185/? D/BtGatt.GattService: Is it foreground application = true 08-14 12:44:20.812 4079-7185/? D/BtGatt.GattService: not a background application 08-14 12:44:20.817 4079-7185/? E/BtGatt.GattService: App 'com.my.app' is scanning too frequently 

The problem definitely lies in those 6 STATE_ON call results, it's the part of undocumented BLE behavior change, first mentioned in DP4 release notes:

We’ve changed the BLE Scanning behavior starting in DP4. We’ll prevent applications from starting and stopping scans more than 5 times in 30 seconds. For long running scans, we’ll convert them into opportunistic scans.

What I don't get are the 6 scans in less than 30 seconds even if I set: ScanSettings.setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC).

The code is:

List<ScanFilter> filters = new ArrayList<>(); ScanSettings scanSettings = new ScanSettings.Builder()     .setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC)     .build(); bluetoothAdapter.getBluetoothLeScanner().startScan(filters, scanSettings, recoderScanCallback); //events from the log happen before this log is printed Log.i("test", " started!"); return recoderScanCallback.scanResultObservable().map((ScanResult record) -> {     //never gets here     Log.i("test", " result!"); }); 

RecorderScanCallback is derived from ScanCallback. We cannot use RxAndroidBle#rxBleClient.scanBleSettings(ScanSettings) because our code is about to freeze and we use 1.1.0 version of the lib.

Why does ScanSettings.setScanMode not alter results of the search?

like image 996
Margarita Litkevych Avatar asked Aug 14 '17 19:08

Margarita Litkevych


1 Answers

Android 7 prevents scan start-stops more than 5 times in 30 seconds. The bad side is, it doesn't return an error, instead just prints a log. The app thinks the scan is started but it's not actually started back at the ble stack. Also it converts long running scans to opportunistic scan with an intent of preventing abusive apps. The duration is 30 minutes for a long running scan.

These changes are undocumented, mentioned in this post: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983

like image 169
Safa Kadir Avatar answered Oct 14 '22 08:10

Safa Kadir