Search in sources :

Example 16 with ScanResult

use of android.bluetooth.le.ScanResult in project android_packages_apps_Settings by crdroidandroid.

the class AnomalyActions method doUnoptimizedBleScan.

private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
    ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    // perform ble scanning
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
        return;
    }
    BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bleScanner == null) {
        Log.e(TAG, "Cannot access BLE scanner");
        return;
    }
    ScanCallback scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.v(TAG, "called onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.v(TAG, "called onScanFailed");
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.v(TAG, "called onBatchScanResults");
        }
    };
    bleScanner.startScan(null, scanSettings, scanCallback);
    try {
        Thread.sleep(durationMs);
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
    }
    bleScanner.stopScan(scanCallback);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 17 with ScanResult

use of android.bluetooth.le.ScanResult in project robolectric by robolectric.

the class ShadowBluetoothLeScannerTest method setUp.

@Before
public void setUp() throws Exception {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (RuntimeEnvironment.getApiLevel() < M) {
        // On SDK < 23, bluetooth has to be in STATE_ON in order to get a BluetoothLeScanner.
        shadowOf(adapter).setState(BluetoothAdapter.STATE_ON);
    }
    bluetoothLeScanner = adapter.getBluetoothLeScanner();
    ParcelUuid serviceUuid = new ParcelUuid(UUID.fromString("12345678-90AB-CDEF-1234-567890ABCDEF"));
    byte[] serviceData = new byte[] { 0x01, 0x02, 0x03 };
    scanFilters = Collections.singletonList(new ScanFilter.Builder().setServiceUuid(serviceUuid).setServiceData(serviceUuid, serviceData).build());
    scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setReportDelay(0).build();
    scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult scanResult) {
        }

        @Override
        public void onScanFailed(int errorCode) {
        }
    };
    pendingIntent = PendingIntent.getBroadcast(ApplicationProvider.getApplicationContext(), 0, new Intent("SCAN_CALLBACK"), 0);
}
Also used : ParcelUuid(android.os.ParcelUuid) ScanSettings(android.bluetooth.le.ScanSettings) ScanResult(android.bluetooth.le.ScanResult) ScanFilter(android.bluetooth.le.ScanFilter) ScanCallback(android.bluetooth.le.ScanCallback) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) BluetoothAdapter(android.bluetooth.BluetoothAdapter) Before(org.junit.Before)

Example 18 with ScanResult

use of android.bluetooth.le.ScanResult in project android_packages_apps_Settings by SudaMod.

the class AnomalyActions method doUnoptimizedBleScan.

private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
    ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    // perform ble scanning
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
        return;
    }
    BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bleScanner == null) {
        Log.e(TAG, "Cannot access BLE scanner");
        return;
    }
    ScanCallback scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.v(TAG, "called onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.v(TAG, "called onScanFailed");
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.v(TAG, "called onBatchScanResults");
        }
    };
    bleScanner.startScan(null, scanSettings, scanCallback);
    try {
        Thread.sleep(durationMs);
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
    }
    bleScanner.stopScan(scanCallback);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 19 with ScanResult

use of android.bluetooth.le.ScanResult in project android-beacon-library by AltBeacon.

the class StartupBroadcastReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    LogManager.d(TAG, "onReceive called in startup broadcast receiver");
    if (Build.VERSION.SDK_INT < 18) {
        LogManager.w(TAG, "Not starting up beacon service because we do not have API version 18 (Android 4.3).  We have: %s", Build.VERSION.SDK_INT);
        return;
    }
    BeaconManager beaconManager = BeaconManager.getInstanceForApplication(context.getApplicationContext());
    if (beaconManager.isAnyConsumerBound() || beaconManager.getScheduledScanJobsEnabled() || beaconManager.getIntentScanStrategyCoordinator() != null) {
        // e.g. ScanSettings.CALLBACK_TYPE_FIRST_MATCH
        int bleCallbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1);
        if (bleCallbackType != -1) {
            LogManager.d(TAG, "Passive background scan callback type: " + bleCallbackType);
            LogManager.d(TAG, "got Android O background scan via intent");
            // e.g.  ScanCallback.SCAN_FAILED_INTERNAL_ERROR
            int errorCode = intent.getIntExtra(BluetoothLeScanner.EXTRA_ERROR_CODE, -1);
            if (errorCode != -1) {
                LogManager.w(TAG, "Passive background scan failed.  Code; " + errorCode);
            }
            ArrayList<ScanResult> scanResults = intent.getParcelableArrayListExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT);
            if (beaconManager.getIntentScanStrategyCoordinator() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                beaconManager.getIntentScanStrategyCoordinator().processScanResults(scanResults);
            } else if (beaconManager.getScheduledScanJobsEnabled() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ScanJobScheduler.getInstance().scheduleAfterBackgroundWakeup(context, scanResults);
            }
        } else if (intent.getBooleanExtra("wakeup", false)) {
            LogManager.d(TAG, "got wake up intent");
        } else {
            LogManager.d(TAG, "Already started.  Ignoring intent: %s of type: %s", intent, intent.getStringExtra("wakeup"));
        }
    } else {
        LogManager.d(TAG, "No consumers are bound.  Ignoring broadcast receiver.");
    }
}
Also used : BeaconManager(org.altbeacon.beacon.BeaconManager) ScanResult(android.bluetooth.le.ScanResult)

Example 20 with ScanResult

use of android.bluetooth.le.ScanResult in project android-beacon-library by AltBeacon.

the class BluetoothMedic method runScanTest.

/**
 * Starts up a brief blueooth scan with the intent of seeing if it results in an error condition
 * indicating the bluetooth stack may be in a bad state.
 *
 * If the failure error code matches a pattern known to be associated with a bad bluetooth stack
 * state, then the bluetooth stack is turned off and then back on after a short delay in order
 * to try to recover.
 *
 * @return false if the test indicates a failure indicating a bad state of the bluetooth stack
 */
@SuppressWarnings({ "unused", "WeakerAccess" })
@RequiresApi(21)
public boolean runScanTest(final Context context) {
    initializeWithContext(context);
    this.mScanTestResult = null;
    LogManager.i(TAG, "Starting scan test");
    final long testStartTime = System.currentTimeMillis();
    if (this.mAdapter != null) {
        final BluetoothLeScanner scanner = this.mAdapter.getBluetoothLeScanner();
        final ScanCallback callback = new ScanCallback() {

            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);
                BluetoothMedic.this.mScanTestResult = true;
                LogManager.i(BluetoothMedic.TAG, "Scan test succeeded");
                try {
                    scanner.stopScan(this);
                }// caught if bluetooth is off here
                 catch (IllegalStateException e) {
                /* do nothing */
                }
            }

            public void onBatchScanResults(List<ScanResult> results) {
                super.onBatchScanResults(results);
            }

            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
                LogManager.d(BluetoothMedic.TAG, "Sending onScanFailed event");
                BluetoothMedic.this.processMedicAction("onScanFailed", errorCode);
                if (errorCode == 2) {
                    LogManager.w(BluetoothMedic.TAG, "Scan test failed in a way we consider a failure");
                    BluetoothMedic.this.sendScreenNotification("scan failed", "bluetooth not ok");
                    BluetoothMedic.this.mScanTestResult = false;
                } else {
                    LogManager.i(BluetoothMedic.TAG, "Scan test failed in a way we do not consider a failure");
                    BluetoothMedic.this.mScanTestResult = true;
                }
            }
        };
        if (scanner != null) {
            try {
                scanner.startScan(callback);
                while (this.mScanTestResult == null) {
                    LogManager.d(TAG, "Waiting for scan test to complete...");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                    /* do nothing */
                    }
                    if (System.currentTimeMillis() - testStartTime > 5000L) {
                        LogManager.d(TAG, "Timeout running scan test");
                        break;
                    }
                }
                scanner.stopScan(callback);
            } catch (IllegalStateException e) {
                LogManager.d(TAG, "Bluetooth is off.  Cannot run scan test.");
            } catch (NullPointerException e) {
                // Needed to stop a crash caused by internal NPE thrown by Android.  See issue #636
                LogManager.e(TAG, "NullPointerException. Cannot run scan test.", e);
            }
        } else {
            LogManager.d(TAG, "Cannot get scanner");
        }
    }
    LogManager.d(TAG, "scan test complete");
    return this.mScanTestResult == null || this.mScanTestResult;
}
Also used : BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

ScanResult (android.bluetooth.le.ScanResult)20 ScanCallback (android.bluetooth.le.ScanCallback)17 ScanSettings (android.bluetooth.le.ScanSettings)14 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)13 BluetoothAdapter (android.bluetooth.BluetoothAdapter)8 List (java.util.List)8 ScanFilter (android.bluetooth.le.ScanFilter)6 ScanRecord (android.bluetooth.le.ScanRecord)6 ParcelUuid (android.os.ParcelUuid)6 ArrayList (java.util.ArrayList)6 RequiresPermission (android.annotation.RequiresPermission)5 RemoteException (android.os.RemoteException)5 UUID (java.util.UUID)5 TargetApi (android.annotation.TargetApi)3 BluetoothDevice (android.bluetooth.BluetoothDevice)3 ActiveBluetoothDevice (com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 RequiresApi (androidx.annotation.RequiresApi)1 BeaconManager (org.altbeacon.beacon.BeaconManager)1