@TargetApi(18)
    public void notifyScannedDevice(BluetoothDevice device, BluetoothAdapter.LeScanCallback scanner) {
        int oldSize, newSize;

        oldSize = distinctBluetoothAddresses.size();

        synchronized(distinctBluetoothAddresses) {
            distinctBluetoothAddresses.add(device.getAddress());
        }

        newSize = distinctBluetoothAddresses.size();
        if (oldSize != newSize && newSize % 100 == 0) {
            LogManager.d(TAG, "Distinct Bluetooth devices seen: %s", distinctBluetoothAddresses.size());
        }
        if (distinctBluetoothAddresses.size()  > getCrashRiskDeviceCount()) {
            if (PREEMPTIVE_ACTION_ENABLED && !recoveryInProgress) {
                LogManager.w(TAG, "Large number of Bluetooth devices detected: %s Proactively "
                        + "attempting to clear out address list to prevent a crash",
                        distinctBluetoothAddresses.size());
                LogManager.w(TAG, "Stopping LE Scan");
                BluetoothAdapter.getDefaultAdapter().stopLeScan(scanner);
                startRecovery();
                processStateChange();
            }
        }
    }
@ TargetApi ( 18 ) public void notifyScannedDevice ( BluetoothDevice device , BluetoothAdapter . LeScanCallback scanner ) { int oldSize , newSize ; oldSize = distinctBluetoothAddresses . size ( ) ; synchronized ( distinctBluetoothAddresses ) { distinctBluetoothAddresses . add ( device . getAddress ( ) ) ; } newSize = distinctBluetoothAddresses . size ( ) ; if ( oldSize != newSize && newSize % 100 == 0 ) { LogManager . d ( TAG , "Distinct Bluetooth devices seen: %s" , distinctBluetoothAddresses . size ( ) ) ; } if ( distinctBluetoothAddresses . size ( ) > getCrashRiskDeviceCount ( ) ) { if ( PREEMPTIVE_ACTION_ENABLED && ! recoveryInProgress ) { LogManager . w ( TAG , "Large number of Bluetooth devices detected: %s Proactively " + "attempting to clear out address list to prevent a crash" , distinctBluetoothAddresses . size ( ) ) ; LogManager . w ( TAG , "Stopping LE Scan" ) ; BluetoothAdapter . getDefaultAdapter ( ) . stopLeScan ( scanner ) ; startRecovery ( ) ; processStateChange ( ) ; } } }
Call this method from your BluetoothAdapter.LeScanCallback method.
Doing so is optional, but if you do, this class will be able to count the number of
distinct Bluetooth devices scanned, and prevent crashes before they happen.

This works very well if the app containing this class is the only one running bluetooth
LE scans on the device, or it is constantly doing scans (e.g. is in the foreground for
extended periods of time.)

This will not work well if the application using this class is only scanning periodically
(e.g. when in the background to save battery) and another application is also scanning on
the same device, because this class will only get the counts from this application.

Future augmentation of this class may improve this by somehow centralizing the list of
unique scanned devices.

@param device