From d420cc2044c8c7c20b0a00e45ba7286d8de1a0a4 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Tue, 23 Jun 2015 13:15:46 +0200 Subject: [PATCH] Scanner bug fixed for Lollipop. --- .../nordicsemi/android/dfu/DfuBaseService.java | 11 ++++++++--- .../android/dfu/scanner/BootloaderScanner.java | 2 +- .../dfu/scanner/BootloaderScannerLollipop.java | 16 ++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java b/dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java index 41ebed4..d913e64 100644 --- a/dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java +++ b/dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java @@ -1740,9 +1740,13 @@ public abstract class DfuBaseService extends IntentService { * We could have save the fact of jumping as a parameter of the service but it ma be that some Android devices must first scan a device before connecting to it. * It a device with the address+1 has never been detected before the service could have failed on connection. */ - sendLogBroadcast(LOG_LEVEL_VERBOSE, "Scanning for the DFU bootloader..."); + sendLogBroadcast(LOG_LEVEL_VERBOSE, "Scanning for the DFU Bootloader..."); final String newAddress = BootloaderScannerFactory.getScanner().searchFor(mDeviceAddress); - sendLogBroadcast(LOG_LEVEL_INFO, "The Bootloader found (" + newAddress + ")"); + if (newAddress != null) + sendLogBroadcast(LOG_LEVEL_INFO, "DFU Bootloader found with address " + newAddress); + else { + sendLogBroadcast(LOG_LEVEL_INFO, "DFU Bootloader not found. Trying the same address..."); + } /* * The current service instance has uploaded the Soft Device and/or Bootloader. @@ -1753,7 +1757,8 @@ public abstract class DfuBaseService extends IntentService { newIntent.fillIn(intent, Intent.FILL_IN_COMPONENT | Intent.FILL_IN_PACKAGE); newIntent.putExtra(EXTRA_FILE_MIME_TYPE, MIME_TYPE_ZIP); // ensure this is set (e.g. for scripts) newIntent.putExtra(EXTRA_FILE_TYPE, TYPE_APPLICATION); // set the type to application only - newIntent.putExtra(EXTRA_DEVICE_ADDRESS, newAddress); + if (newAddress != null) + newIntent.putExtra(EXTRA_DEVICE_ADDRESS, newAddress); newIntent.putExtra(EXTRA_PART_CURRENT, mPartCurrent + 1); newIntent.putExtra(EXTRA_PARTS_TOTAL, mPartsTotal); startService(newIntent); diff --git a/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScanner.java b/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScanner.java index 4ff4aaa..47c422e 100644 --- a/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScanner.java +++ b/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScanner.java @@ -37,7 +37,7 @@ public interface BootloaderScanner { /** * After the buttonless jump from the application mode to the bootloader mode the service will wait this long for the advertising bootloader (in milliseconds). */ - public final static long TIMEOUT = 2000l; // ms + public final static long TIMEOUT = 5000l; // ms /** The bootloader may advertise with the same address or one with the last byte incremented by this value. F.e. 00:11:22:33:44:55 -> 00:11:22:33:44:56. FF changes to 00. */ public final static int ADDRESS_DIFF = 1; diff --git a/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScannerLollipop.java b/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScannerLollipop.java index bbd20e0..b562fd8 100644 --- a/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScannerLollipop.java +++ b/dfu/src/main/java/no/nordicsemi/android/dfu/scanner/BootloaderScannerLollipop.java @@ -22,14 +22,10 @@ package no.nordicsemi.android.dfu.scanner; -import java.util.ArrayList; -import java.util.List; - import android.annotation.TargetApi; import android.bluetooth.BluetoothAdapter; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; -import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.os.Build; @@ -81,11 +77,15 @@ public class BootloaderScannerLollipop extends ScanCallback implements Bootloade final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); final BluetoothLeScanner scanner = adapter.getBluetoothLeScanner(); - final List filters = new ArrayList<>(); - filters.add(new ScanFilter.Builder().setDeviceAddress(mDeviceAddress).build()); - filters.add(new ScanFilter.Builder().setDeviceAddress(mDeviceAddressIncremented).build()); + /* + * Scanning with filters does not work on Nexus 9 (Android 5.1). No devices are found and scanner terminates on timeout. + * We will match the device address in the callback method instead. It's not like it should be, but at least it works. + */ + //final List filters = new ArrayList<>(); + //filters.add(new ScanFilter.Builder().setDeviceAddress(mDeviceAddress).build()); + //filters.add(new ScanFilter.Builder().setDeviceAddress(mDeviceAddressIncremented).build()); final ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); - scanner.startScan(filters, settings, this); + scanner.startScan(/*filters*/ null, settings, this); try { synchronized (mLock) {