@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void startLeScan(List<ScanFilter> scanFilters, ScanSettings scanSettings, ScanCallback scanCallback) { bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, scanCallback); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void stopLeScan(ScanCallback scanCallback) { final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner == null) { RxBleLog.d("Cannot perform BluetoothLeScanner.stopScan(ScanCallback) because scanner is unavailable (Probably adapter is off)"); // if stopping the scan is not possible due to BluetoothLeScanner not accessible then it is probably stopped anyway return; } bluetoothLeScanner.stopScan(scanCallback); }
@RequiresApi(Build.VERSION_CODES.O) public void stopLeScan(PendingIntent callbackIntent) { bluetoothAdapter.getBluetoothLeScanner().stopScan(callbackIntent); }
@RequiresApi(Build.VERSION_CODES.O) public int startLeScan(List<ScanFilter> scanFilters, ScanSettings scanSettings, PendingIntent callbackIntent) { return bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, callbackIntent); }
@RequiresApi(api = Build.VERSION_CODES.O) void stopAndroidOBackgroundScan() { try { final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null) { LogManager.w(TAG, "Failed to construct a BluetoothAdapter"); } else if (!bluetoothAdapter.isEnabled()) { LogManager.w(TAG, "BluetoothAdapter is not enabled"); } else { BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); if (scanner != null) { scanner.stopScan(getScanCallbackIntent()); } } } catch (SecurityException e) { LogManager.e(TAG, "SecurityException stopping Android O background scanner"); } catch (RuntimeException e) { // Needed to stop a crash caused by internal Android throw. See issue #701 LogManager.e(TAG, "Unexpected runtime exception stopping Android O background scanner", e); } }
private BluetoothLeScanner getScanner() { try { if (mScanner == null) { LogManager.d(TAG, "Making new Android L scanner"); BluetoothAdapter bluetoothAdapter = getBluetoothAdapter(); if (bluetoothAdapter != null) { mScanner = getBluetoothAdapter().getBluetoothLeScanner(); } if (mScanner == null) { LogManager.w(TAG, "Failed to make new Android L scanner"); } } } catch (SecurityException e) { LogManager.w(TAG, "SecurityException making new Android L scanner"); } return mScanner; }
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) {
LogManager.w(TAG, "Failed to start background scan on Android O: BluetoothAdapter is not enabled"); } else { BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); if (scanner != null) { int result = scanner.startScan(filters, settings, getScanCallbackIntent());
@Override public synchronized void initAndCheckEligibility() throws ScannerException { if (Build.VERSION.SDK_INT < ScannerFactory.OS_VERSION_LOLLIPOP) { throw new ScannerException(ScannerException.Type.NOT_SUPPORTED); } if (!Validate.hasBluetoothPermission(context)) { throw new ScannerException(ScannerException.Type.PERMISSION_DENIED); } if (!Validate.hasLocationPermission(context)) { throw new ScannerException(ScannerException.Type.PERMISSION_DENIED); } bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { throw new ScannerException(ScannerException.Type.DISABLED); } bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner == null) { throw new ScannerException(ScannerException.Type.UNKNOWN_ERROR); } }
/** * Must be called after object creation by MainActivity. * * @param btAdapter the local BluetoothAdapter */ public void setBluetoothAdapter(BluetoothAdapter btAdapter) { this.mBluetoothAdapter = btAdapter; mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); }
Context mContext = getBaseContext(); BluetoothAdapter mBluetoothAdapter = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE).getAdapter(); BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); mBluetoothLeScanner.startScan(new ScanCallback() {…. }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void stopBleScan_post21() { if(mBtAdapter!=null && mBtAdapter.getBluetoothLeScanner()!=null) { mBtAdapter.getBluetoothLeScanner().stopScan(mScanCallBack_post21); } }
/** * @return the {@link BluetoothLeScanner} */ @Override @Nullable public BluetoothLeScanner getBluetoothLeScanner() { return this.canAttachBluetoothAdapter() ? this.bluetoothAdapter.getBluetoothLeScanner() : null; }
Context mContext = getBaseContext(); BluetoothAdapter mAdapter = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE).getAdapter(); BluetoothLeScanner mLeScanner = mAdapter.getBluetoothLeScanner(); mLeScanner.startScan(...);
private BluetoothAdapter bluetoothAdapter; private BluetoothAdapter.LeScanCallback leScanCallback; private BluetoothAdapter.LeScanCallback leScanCallback; private ScanCallback scanCallback; private ScanSettings scanSetting; // Check before call the function if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { bluetoothAdapter.getBluetoothLeScanner().startScan(filterList, scanSetting, scanCallback); } else { bluetoothAdapter.startLeScan(leScanCallback); }
@Override public void stopScan(final BleDeviceScanCallback callback) { mBleScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (mBleScanner != null) { mBleScanner.stopScan(mScanCallback); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void startBleScan_post21() { mScanCallBack_post21 = new ScanCallbackBridge(mScanCallBack_pre21); if(mBtAdapter!=null && mBtAdapter.getBluetoothLeScanner()!=null) { ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); List<ScanFilter> noFilter = Collections.emptyList(); mBtAdapter.getBluetoothLeScanner().startScan(noFilter,settings,mScanCallBack_post21); } }
public RuuviTagScanner(RuuviTagListener listener, Context context) { this.listener = listener; scanSettings = new ScanSettings.Builder() .setReportDelay(0) .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); scanner = bluetoothAdapter.getBluetoothLeScanner(); }
public void startScanning() { scanner = adapter.getBluetoothLeScanner(); ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); List<ScanFilter> scanFilters = Arrays.asList( new ScanFilter.Builder() //.setServiceUuid(ParcelUuid.fromString("some uuid")) .build()); myScanCallback = new MyScanCallback(); scanner.startScan(scanFilters, scanSettings, myScanCallback); }
@Test public void testMissingBluetoothPermission() { final BluetoothAdapter bluetoothAdapter = mock(BluetoothAdapter.class); Mockito.doThrow(new SecurityException("Need BLUETOOTH permission: Neither user xxxxx nor current process has android.permission.BLUETOOTH")) .when(bluetoothAdapter).getBluetoothLeScanner(); final DefaultBluetoothFactory defaultBluetoothFactory = mock(DefaultBluetoothFactory.class); when(defaultBluetoothFactory.canAttachBluetoothAdapter()).thenCallRealMethod(); when(defaultBluetoothFactory.getBluetoothLeScanner()).thenCallRealMethod(); when(defaultBluetoothFactory.getBluetoothAdapter()).thenReturn(bluetoothAdapter); Assert.assertEquals(false, defaultBluetoothFactory.canAttachBluetoothAdapter()); Assert.assertEquals(null, defaultBluetoothFactory.getBluetoothLeScanner()); }