Search in sources :

Example 1 with BackgroundScanner

use of com.polidea.rxandroidble2.scan.BackgroundScanner 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 2 with BackgroundScanner

use of com.polidea.rxandroidble2.scan.BackgroundScanner 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)

Aggregations

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