Search in sources :

Example 1 with BleScanException

use of com.polidea.rxandroidble2.exceptions.BleScanException in project xDrip by NightscoutFoundation.

the class Ob1G5CollectionService method onScanFailure.

// Failed result from our bluetooth scan
private synchronized void onScanFailure(Throwable throwable) {
    if (throwable instanceof BleScanException) {
        final String info = handleBleScanException((BleScanException) throwable);
        lastScanError = info;
        UserError.Log.d(TAG, info);
        if (((BleScanException) throwable).getReason() == BleScanException.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED) {
            if (JoH.ratelimit("bluetooth-cannot-register", 120)) {
                if (Pref.getBooleanDefaultFalse("automatically_turn_bluetooth_on")) {
                    UserError.Log.wtf(TAG, "Android bluetooth appears broken - attempting to turn off and on");
                    JoH.niceRestartBluetooth(xdrip.getAppContext());
                } else {
                    UserError.Log.e(TAG, "Cannot reset bluetooth due to preference being disabled");
                }
            }
        }
        if (((BleScanException) throwable).getReason() == BleScanException.BLUETOOTH_DISABLED) {
            // Attempt to turn bluetooth on
            if (JoH.ratelimit("bluetooth_toggle_on", 30)) {
                UserError.Log.d(TAG, "Pause before Turn Bluetooth on");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                // 
                }
                if (WholeHouse.isRpi()) {
                    UserError.Log.e(TAG, "Trying to turn off/on bluetooth");
                    JoH.niceRestartBluetooth(xdrip.getAppContext());
                } else {
                    UserError.Log.e(TAG, "Trying to Turn Bluetooth on");
                    JoH.setBluetoothEnabled(xdrip.getAppContext(), true);
                }
            }
        }
        // TODO count scan duration
        stopScan();
        // Note that this is not reached on timeout only failure
        if (minimize_scanning) {
            UserError.Log.d(TAG, "Preparing to wake up at next expected time");
            prepareToWakeup();
        } else {
            backoff_automata();
        }
    }
}
Also used : BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) SpannableString(android.text.SpannableString)

Example 2 with BleScanException

use of com.polidea.rxandroidble2.exceptions.BleScanException in project xDrip-plus by jamorham.

the class BackgroundScanReceiver method onReceive.

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    android.util.Log.d("BackgroundScanReceiver", "GOT SCAN INTENT!! " + action);
    if (action != null && action.equals(ACTION_NAME)) {
        String caller = intent.getStringExtra(CALLING_CLASS);
        if (caller == null)
            caller = this.getClass().getSimpleName();
        // TODO by class name?
        final BackgroundScanner backgroundScanner = RxBleProvider.getSingleton().getBackgroundScanner();
        try {
            final List<ScanResult> scanResults = backgroundScanner.onScanResultReceived(intent);
            final String matchedMac = scanResults.get(0).getBleDevice().getMacAddress();
            final String matchedName = scanResults.get(0).getBleDevice().getName();
            final boolean calledBack = processCallbacks(caller, matchedMac, matchedName, SCAN_FOUND_CALLBACK);
            UserError.Log.d(caller, "Scan results received: " + matchedMac + " " + scanResults);
            if (!calledBack) {
                try {
                    // bit of an ugly fix to system wide persistent nature of background scans and lack of proper support for one hit over various android devices
                    backgroundScanner.stopBackgroundBleScan(// must match original
                    PendingIntent.getBroadcast(// must match original
                    xdrip.getAppContext(), // must match original
                    142, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                } catch (Exception e) {
                // 
                }
            }
        } catch (NullPointerException | BleScanException exception) {
            UserError.Log.e(caller, "Failed to scan devices" + exception);
        }
    }
// ignore invalid actions
}
Also used : ScanResult(com.polidea.rxandroidble2.scan.ScanResult) BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) BackgroundScanner(com.polidea.rxandroidble2.scan.BackgroundScanner) BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) RequiresApi(android.support.annotation.RequiresApi)

Example 3 with BleScanException

use of com.polidea.rxandroidble2.exceptions.BleScanException in project xDrip by NightscoutFoundation.

the class BackgroundScanReceiver method onReceive.

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    android.util.Log.d("BackgroundScanReceiver", "GOT SCAN INTENT!! " + action);
    if (action != null && action.equals(ACTION_NAME)) {
        String caller = intent.getStringExtra(CALLING_CLASS);
        if (caller == null)
            caller = this.getClass().getSimpleName();
        // TODO by class name?
        final BackgroundScanner backgroundScanner = RxBleProvider.getSingleton().getBackgroundScanner();
        try {
            final List<ScanResult> scanResults = backgroundScanner.onScanResultReceived(intent);
            final String matchedMac = scanResults.get(0).getBleDevice().getMacAddress();
            final String matchedName = scanResults.get(0).getBleDevice().getName();
            final boolean calledBack = processCallbacks(caller, matchedMac, matchedName, SCAN_FOUND_CALLBACK);
            UserError.Log.d(caller, "Scan results received: " + matchedMac + " " + scanResults);
            if (!calledBack) {
                try {
                    // bit of an ugly fix to system wide persistent nature of background scans and lack of proper support for one hit over various android devices
                    backgroundScanner.stopBackgroundBleScan(// must match original
                    PendingIntent.getBroadcast(// must match original
                    xdrip.getAppContext(), // must match original
                    142, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                } catch (Exception e) {
                // 
                }
            }
        } catch (NullPointerException | BleScanException exception) {
            UserError.Log.e(caller, "Failed to scan devices" + exception);
        }
    }
// ignore invalid actions
}
Also used : ScanResult(com.polidea.rxandroidble2.scan.ScanResult) BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) BackgroundScanner(com.polidea.rxandroidble2.scan.BackgroundScanner) BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) RequiresApi(android.support.annotation.RequiresApi)

Example 4 with BleScanException

use of com.polidea.rxandroidble2.exceptions.BleScanException in project xDrip-plus by jamorham.

the class Ob1G5CollectionService method onScanFailure.

// Failed result from our bluetooth scan
private synchronized void onScanFailure(Throwable throwable) {
    if (throwable instanceof BleScanException) {
        final String info = handleBleScanException((BleScanException) throwable);
        lastScanError = info;
        UserError.Log.d(TAG, info);
        if (((BleScanException) throwable).getReason() == BleScanException.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED) {
            if (JoH.ratelimit("bluetooth-cannot-register", 120)) {
                if (Pref.getBooleanDefaultFalse("automatically_turn_bluetooth_on")) {
                    UserError.Log.wtf(TAG, "Android bluetooth appears broken - attempting to turn off and on");
                    JoH.niceRestartBluetooth(xdrip.getAppContext());
                } else {
                    UserError.Log.e(TAG, "Cannot reset bluetooth due to preference being disabled");
                }
            }
        }
        if (((BleScanException) throwable).getReason() == BleScanException.BLUETOOTH_DISABLED) {
            // Attempt to turn bluetooth on
            if (JoH.ratelimit("bluetooth_toggle_on", 30)) {
                UserError.Log.d(TAG, "Pause before Turn Bluetooth on");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                // 
                }
                if (WholeHouse.isRpi()) {
                    UserError.Log.e(TAG, "Trying to turn off/on bluetooth");
                    JoH.niceRestartBluetooth(xdrip.getAppContext());
                } else {
                    UserError.Log.e(TAG, "Trying to Turn Bluetooth on");
                    JoH.setBluetoothEnabled(xdrip.getAppContext(), true);
                }
            }
        }
        // TODO count scan duration
        stopScan();
        // Note that this is not reached on timeout only failure
        if (minimize_scanning) {
            UserError.Log.d(TAG, "Preparing to wake up at next expected time");
            prepareToWakeup();
        } else {
            backoff_automata();
        }
    }
}
Also used : BleScanException(com.polidea.rxandroidble2.exceptions.BleScanException) SpannableString(android.text.SpannableString)

Aggregations

BleScanException (com.polidea.rxandroidble2.exceptions.BleScanException)4 RequiresApi (android.support.annotation.RequiresApi)2 SpannableString (android.text.SpannableString)2 BackgroundScanner (com.polidea.rxandroidble2.scan.BackgroundScanner)2 ScanResult (com.polidea.rxandroidble2.scan.ScanResult)2