'La aplicación está escaneando con demasiada frecuencia' con ScanSettings.SCAN_MODE_OPPORTUNISTIC

Recientemente hemos encontrado un problema en Samsung S8, Android 7.0, que dice, después de un par de pruebas, que la aplicación está escaneando con demasiada frecuencia:

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 

El problema radica definitivamente en esos 6 resultados de la llamada STATE_ON, es la parte del cambio de comportamiento BLE indocumentado, mencionado por primera vez en notas de la versión DP4:

Hemos cambiado el comportamiento de BLE Scanning comenzando en DP4. Evitaremos que las aplicaciones inicien y detengan las exploraciones más de 5 veces en 30 segundos. Para análisis de larga duración, los convertiremos en escaneos oportunistas.

Lo que no obtengo son estos 6 escaneos en menos de 30s incluso si configuro ScanSettings.setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC) .

El código es:

  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 se deriva de ScanCallback. No podemos usar RxAndroidBle # rxBleClient.scanBleSettings (ScanSettings) porque nuestro código está a punto de congelarse y usamos la versión 1.1.0 de la lib.

¿Por qué ScanSettings.setScanMode no altera los resultados de la búsqueda?

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.