@@ -249,73 +249,63 @@ private ScanCallbackWrapperLollipop(final boolean offloadedBatchingSupported,
249249
250250 @ Override
251251 public void onScanResult (final int callbackType , final android .bluetooth .le .ScanResult nativeScanResult ) {
252- handler .post (new Runnable () {
253- @ Override
254- public void run () {
255- final BluetoothLeScannerImplLollipop scannerImpl =
256- (BluetoothLeScannerImplLollipop ) BluetoothLeScannerCompat .getScanner ();
257- final ScanResult result = scannerImpl .fromNativeScanResult (nativeScanResult );
258- handleScanResult (callbackType , result );
259- }
252+ handler .post (() -> {
253+ final BluetoothLeScannerImplLollipop scannerImpl =
254+ (BluetoothLeScannerImplLollipop ) BluetoothLeScannerCompat .getScanner ();
255+ final ScanResult result = scannerImpl .fromNativeScanResult (nativeScanResult );
256+ handleScanResult (callbackType , result );
260257 });
261258 }
262259
263260 @ Override
264261 public void onBatchScanResults (final List <android .bluetooth .le .ScanResult > nativeScanResults ) {
265- handler .post (new Runnable () {
266- @ Override
267- public void run () {
268- // On several phones the onBatchScanResults is called twice for every batch.
269- // Skip the second call if came to early.
270- final long now = SystemClock .elapsedRealtime ();
271- if (lastBatchTimestamp > now - scanSettings .getReportDelayMillis () + 5 ) {
272- return ;
273- }
274- lastBatchTimestamp = now ;
275-
276- final BluetoothLeScannerImplLollipop scannerImpl =
277- (BluetoothLeScannerImplLollipop ) BluetoothLeScannerCompat .getScanner ();
278- final List <ScanResult > results = scannerImpl .fromNativeScanResults (nativeScanResults );
279- handleScanResults (results );
262+ handler .post (() -> {
263+ // On several phones the onBatchScanResults is called twice for every batch.
264+ // Skip the second call if came to early.
265+ final long now = SystemClock .elapsedRealtime ();
266+ if (lastBatchTimestamp > now - scanSettings .getReportDelayMillis () + 5 ) {
267+ return ;
280268 }
269+ lastBatchTimestamp = now ;
270+
271+ final BluetoothLeScannerImplLollipop scannerImpl =
272+ (BluetoothLeScannerImplLollipop ) BluetoothLeScannerCompat .getScanner ();
273+ final List <ScanResult > results = scannerImpl .fromNativeScanResults (nativeScanResults );
274+ handleScanResults (results );
281275 });
282276 }
283277
284278 @ Override
285279 public void onScanFailed (final int errorCode ) {
286- handler .post (new Runnable () {
287- @ Override
288- @ RequiresPermission (allOf = {Manifest .permission .BLUETOOTH_ADMIN , Manifest .permission .BLUETOOTH })
289- public void run () {
290- // We were able to determine offloaded batching and filtering before we started scan,
291- // but there is no method checking if callback types FIRST_MATCH and MATCH_LOST
292- // are supported. We get an error here it they are not.
293- if (scanSettings .getUseHardwareCallbackTypesIfSupported ()
294- && scanSettings .getCallbackType () != ScanSettings .CALLBACK_TYPE_ALL_MATCHES ) {
295- // On Nexus 6 with Android 6.0 (MPA44G, M Pre-release 3) the errorCode = 5 (SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES)
296- // On Pixel 2 with Android 9.0 the errorCode = 4 (SCAN_FAILED_FEATURE_UNSUPPORTED)
297-
298- // This feature seems to be not supported on your phone.
299- // Let's try to do pretty much the same in the code.
300- scanSettings .disableUseHardwareCallbackTypes ();
301-
302- final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat .getScanner ();
303- try {
304- scanner .stopScan (scanCallback );
305- } catch (final Exception e ) {
306- // Ignore
307- }
308- try {
309- scanner .startScanInternal (filters , scanSettings , scanCallback , handler );
310- } catch (final Exception e ) {
311- // Ignore
312- }
313- return ;
280+ handler .post (() -> {
281+ // We were able to determine offloaded batching and filtering before we started scan,
282+ // but there is no method checking if callback types FIRST_MATCH and MATCH_LOST
283+ // are supported. We get an error here it they are not.
284+ if (scanSettings .getUseHardwareCallbackTypesIfSupported ()
285+ && scanSettings .getCallbackType () != ScanSettings .CALLBACK_TYPE_ALL_MATCHES ) {
286+ // On Nexus 6 with Android 6.0 (MPA44G, M Pre-release 3) the errorCode = 5 (SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES)
287+ // On Pixel 2 with Android 9.0 the errorCode = 4 (SCAN_FAILED_FEATURE_UNSUPPORTED)
288+
289+ // This feature seems to be not supported on your phone.
290+ // Let's try to do pretty much the same in the code.
291+ scanSettings .disableUseHardwareCallbackTypes ();
292+
293+ final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat .getScanner ();
294+ try {
295+ scanner .stopScan (scanCallback );
296+ } catch (final Exception e ) {
297+ // Ignore
314298 }
315-
316- // else, notify user application
317- handleScanError (errorCode );
299+ try {
300+ scanner .startScanInternal (filters , scanSettings , scanCallback , handler );
301+ } catch (final Exception e ) {
302+ // Ignore
303+ }
304+ return ;
318305 }
306+
307+ // else, notify user application
308+ handleScanError (errorCode );
319309 });
320310 }
321311 };
0 commit comments