android.bluetooth.le.ScanSettings Java Examples

The following examples show how to use android.bluetooth.le.ScanSettings. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PeripheralScannerTest.java    From bitgatt with Mozilla Public License 2.0 8 votes vote down vote up
@Test
public void testConnectionAlreadyInMapDisconnectedScanResult() {
    MockScanResultProvider provider = new MockScanResultProvider(10, -167, -40);
    peripheralScanner.addRssiFilter(-10);
    ScanResult result = provider.getAllResults().get(0);
    FitbitBluetoothDevice device = new FitbitBluetoothDevice(result.getDevice());
    GattConnection conn = new GattConnection(device, mockLooper);
    conn.setMockMode(true);
    conn.setState(GattState.DISCONNECTED);
    gatt.getConnectionMap().put(device, conn);
    NoOpGattCallback cb = new NoOpGattCallback() {

        @Override
        public void onBluetoothPeripheralDiscovered(@NonNull GattConnection connection) {
            Assert.assertEquals(conn, connection);
            gatt.unregisterGattEventListener(this);
        }

    };
    gatt.registerGattEventListener(cb);
    peripheralScanner.populateMockScanResultIndividualValue(ScanSettings.CALLBACK_TYPE_FIRST_MATCH, result);
}
 
Example #2
Source File: LollipopScanner.java    From RxCentralBle with Apache License 2.0 7 votes vote down vote up
private void startScan(PublishSubject scanDataSubject) {
  BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  if (adapter != null && adapter.isEnabled()) {
    // Setting service uuid scan filter failing on Galaxy S6 on Android 7.
    // Other devices and versions of Android have additional issues with Scan Filters.
    // Manually filter on scan operation.  Add a dummy filter to avoid Android 8.1+ enforcement
    // of filters during background scanning.
    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
    filters.add(scanFilterBuilder.build());

    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);

    BluetoothLeScanner bleScanner = adapter.getBluetoothLeScanner();
    if (bleScanner != null) {
      bleScanner.startScan(filters, settingsBuilder.build(), scanCallback);
    } else {
      if (RxCentralLogger.isError()) {
        RxCentralLogger.error("startScan - BluetoothLeScanner is null!");
      }

      scanDataSubject.onError(new ConnectionError(SCAN_FAILED));
    }
  } else {
    if (RxCentralLogger.isError()) {
      if (adapter == null) {
        RxCentralLogger.error("startScan - Default Bluetooth Adapter is null!");
      } else {
        RxCentralLogger.error("startScan - Bluetooth Adapter is disabled.");
      }
    }

    scanDataSubject.onError(new ConnectionError(SCAN_FAILED));
  }

}
 
Example #3
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
@Deprecated
static ScanSettings.Builder buildSettings(BleManager mgr, int scanMode, Interval scanReportDelay) {
    final ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(scanMode);

    if( mgr.getNativeAdapter().isOffloadedScanBatchingSupported() )
    {
        final long scanReportDelay_millis = false == Interval.isDisabled(scanReportDelay) ? scanReportDelay.millis() : 0;
        builder.setReportDelay(scanReportDelay_millis);
    }
    else
    {
        builder.setReportDelay(0);
    }
    return builder;
}
 
Example #4
Source File: BluetoothCentralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void stopScanTest() throws Exception {
    application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);
    central.scanForPeripherals();
    verify(scanner).startScan(anyList(), any(ScanSettings.class), any(ScanCallback.class));

    // Grab the scan callback that is used
    Field field = BluetoothCentral.class.getDeclaredField("scanByServiceUUIDCallback");
    field.setAccessible(true);
    ScanCallback scanCallback = (ScanCallback) field.get(central);

    // Stop scan
    central.stopScan();

    // Check if scan is correctly stopped
    verify(scanner).stopScan(scanCallback);

    // Stop scan again
    central.stopScan();

    // Verify that stopScan is not called again
    verify(scanner, times(1)).stopScan(any(ScanCallback.class));
}
 
Example #5
Source File: BluetoothGlucoseMeter.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private void beginScan() {
    if (Build.VERSION.SDK_INT >= 21) {
        if (d) Log.d(TAG, "Preparing for scan...");

        // set up v21 scanner
        mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
        settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();
        filters = new ArrayList<>();

        filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(GLUCOSE_SERVICE)).build());
    }
    // all api versions
    scanLeDevice(true);
}
 
Example #6
Source File: BluetoothCentral.java    From blessed-android with MIT License 6 votes vote down vote up
/**
 * Set the default scanMode.
 *
 * <p>Must be ScanSettings.SCAN_MODE_LOW_POWER, ScanSettings.SCAN_MODE_LOW_LATENCY, ScanSettings.SCAN_MODE_BALANCED or ScanSettings.SCAN_MODE_OPPORTUNISTIC.
 * The default value is SCAN_MODE_LOW_LATENCY.
 *
 * @param scanMode the scanMode to set
 * @return true if a valid scanMode was provided, otherwise false
 */
public boolean setScanMode(int scanMode) {
    if (scanMode == ScanSettings.SCAN_MODE_LOW_POWER ||
            scanMode == ScanSettings.SCAN_MODE_LOW_LATENCY ||
            scanMode == ScanSettings.SCAN_MODE_BALANCED ||
            scanMode == ScanSettings.SCAN_MODE_OPPORTUNISTIC) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.scanSettings = new ScanSettings.Builder()
                    .setScanMode(scanMode)
                    .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                    .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
                    .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
                    .setReportDelay(0L)
                    .build();
        } else {
            this.scanSettings = new ScanSettings.Builder()
                    .setScanMode(scanMode)
                    .setReportDelay(0L)
                    .build();
        }
        return true;
    }
    return false;
}
 
Example #7
Source File: BluetoothCentralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void autoconnectTwice() throws Exception {
    application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);

    BluetoothPeripheral peripheral = mock(BluetoothPeripheral.class);
    when(peripheral.getAddress()).thenReturn("12:23:34:98:76:54");
    when(peripheral.getType()).thenReturn(BluetoothDevice.DEVICE_TYPE_UNKNOWN);
    central.autoConnectPeripheral(peripheral, peripheralCallback);

    verify(peripheral, never()).autoConnect();
    verify(scanner).startScan(anyList(), any(ScanSettings.class), any(ScanCallback.class));

    central.autoConnectPeripheral(peripheral, peripheralCallback);

    verify(peripheral, never()).autoConnect();
}
 
Example #8
Source File: BluetoothLeScannerCompat.java    From AndroidBleManager with Apache License 2.0 6 votes vote down vote up
@Override
public void startScan(BluetoothAdapter adapter, List<ScanFilterCompat> filters, ScanSettingsCompat settings, ScanCallbackCompat callbackCompat) {

    List<ScanFilter> scanFilters = null;
    if (filters != null) {
        scanFilters = new ArrayList<>(filters.size());

        for (ScanFilterCompat filter : filters) {
            scanFilters.add(filter.toApi21());
        }
    }
    if (settings == null) {
        throw new IllegalStateException("Scan settings are null");
    }
    ScanSettings scanSettings = settings.toApi21();
    adapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, registerCallback(callbackCompat));
}
 
Example #9
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
private void discover() {
    List<ScanFilter> filters = new ArrayList<ScanFilter>();

    ScanFilter filter = new ScanFilter.Builder()
            .setServiceUuid( new ParcelUuid(UUID.fromString( getString(R.string.ble_uuid ) ) ) )
            .build();
    filters.add( filter );

    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode( ScanSettings.SCAN_MODE_LOW_LATENCY )
            .build();

    mBluetoothLeScanner.startScan(filters, settings, mScanCallback);

    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mBluetoothLeScanner.stopScan(mScanCallback);
        }
    }, 10000);
}
 
Example #10
Source File: BluetoothGlucoseMeter.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private void beginScan() {
    if (Build.VERSION.SDK_INT >= 21) {
        if (d) Log.d(TAG, "Preparing for scan...");

        // set up v21 scanner
        mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
        settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();
        filters = new ArrayList<>();

        filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(GLUCOSE_SERVICE)).build());
    }
    // all api versions
    scanLeDevice(true);
}
 
Example #11
Source File: EddystoneScannerService.java    From nearby-beacons with MIT License 6 votes vote down vote up
private void startScanning() {
    List<ScanFilter> filters = new ArrayList<>();
    //Filter on just our requested namespaces
    for (String namespace : NAMESPACE_IDS) {
        ScanFilter beaconFilter = new ScanFilter.Builder()
                .setServiceUuid(UID_SERVICE)
                .setServiceData(UID_SERVICE, getNamespaceFilter(namespace),
                        NAMESPACE_FILTER_MASK)
                .build();
        filters.add(beaconFilter);
    }

    //Run in background mode
    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
            .build();

    mBluetoothLeScanner.startScan(filters, settings, mScanCallback);
    if (DEBUG_SCAN) Log.d(TAG, "Scanning started…");
}
 
Example #12
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Deprecated
static ScanSettings.Builder buildSettings(BleManager mgr, int scanMode, Interval scanReportDelay) {
    final ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(scanMode);

    if( mgr.getNativeAdapter().isOffloadedScanBatchingSupported() )
    {
        final long scanReportDelay_millis = false == Interval.isDisabled(scanReportDelay) ? scanReportDelay.millis() : 0;
        builder.setReportDelay(scanReportDelay_millis);
    }
    else
    {
        builder.setReportDelay(0);
    }
    return builder;
}
 
Example #13
Source File: MainActivity.java    From bluetooth with Apache License 2.0 6 votes vote down vote up
void discover() {
    mBluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();

    ScanFilter filter = new ScanFilter.Builder()
        .setServiceUuid( new ParcelUuid(UUID.fromString( getString(R.string.ble_uuid ) ) ) )
        .build();
    List<ScanFilter> filters = new ArrayList<ScanFilter>();
    filters.add( filter );

    ScanSettings settings = new ScanSettings.Builder()
        .setScanMode( ScanSettings.SCAN_MODE_LOW_LATENCY )
        .build();
    mBluetoothLeScanner.startScan(filters, settings, mScanCallback);
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mBluetoothLeScanner.stopScan(mScanCallback);
        }
    }, 10000);
}
 
Example #14
Source File: MockLollipopScanner.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
public BleScanCallbackWrapper(IBluetoothGatt bluetoothGatt,
                              List<ScanFilter> filters, ScanSettings settings,
                              WorkSource workSource, ScanCallback scanCallback,
                              List<List<ResultStorageDescriptor>> resultStorages) {
    mBluetoothGatt = bluetoothGatt;
    mFilters = filters;
    mSettings = settings;
    mWorkSource = workSource;
    mScanCallback = scanCallback;
    mScannerId = 0;
    mResultStorages = resultStorages;
}
 
Example #15
Source File: ScannerService.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    Foreground.init(getApplication());
    Foreground.get().addListener(listener);

    foreground = true;
    scanSettings = new ScanSettings.Builder()
            .setReportDelay(0)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build();
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    scanner = bluetoothAdapter.getBluetoothLeScanner();

    if (getForegroundMode()) startFG();
    handler = new Handler();
    handler.post(reStarter);
    bgScanHandler = new Handler();
}
 
Example #16
Source File: MockLollipopScanner.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
private boolean isSettingsAndFilterComboAllowed(ScanSettings settings,
                                                List<ScanFilter> filterList) {
    final int callbackType = settings.getCallbackType();
    // If onlost/onfound is requested, a non-empty filter is expected
    if ((callbackType & (ScanSettings.CALLBACK_TYPE_FIRST_MATCH
        | ScanSettings.CALLBACK_TYPE_MATCH_LOST)) != 0) {
        if (filterList == null) {
            return false;
        }
        for (ScanFilter filter : filterList) {
            if (filter.equals(EMPTY)) {
                return false;
            }
        }
    }
    return true;
}
 
Example #17
Source File: MyApplication.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    instance = this;
    AppHolder.initialize(this);
    //构建自定义实例,需要在EasyBLE.getInstance()之前
    ScanConfiguration scanConfig = new ScanConfiguration()
            .setScanSettings(new ScanSettings.Builder()
                    .setScanMode(ScanSettings.SCAN_MODE_BALANCED)
                    .build())
            .setScanPeriodMillis(15000)
            .setAcceptSysConnectedDevice(true)
            .setOnlyAcceptBleDevice(true);
    EasyBLE ble = EasyBLE.getBuilder().setScanConfiguration(scanConfig)
            .setObserveAnnotationRequired(false)//不强制使用{@link Observe}注解才会收到被观察者的消息,强制使用的话,性能会好一些
            .setMethodDefaultThreadMode(ThreadMode.BACKGROUND)//指定回调方法和观察者方法的默认线程
            .setScannerType(ScannerType.CLASSIC)
            .build();
    ble.setLogEnabled(true);//开启日志打印
    ble.initialize(this);
}
 
Example #18
Source File: AmazonFreeRTOSManager.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
private void scanLeDevice(long duration) {
    if (mScanning) {
        Log.d(TAG, "Scanning is already in progress.");
        return;
    }
    // Stops scanning after a pre-defined scan period.
    if (duration != 0) {
        mScanHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                stopScanDevices();
            }
        }, duration);
    }
    Log.i(TAG, "Starting ble device scan");
    mScanning = true;

    ScanSettings scanSettings = new ScanSettings.Builder().build();
    mBluetoothLeScanner.startScan(mScanFilters, scanSettings, mScanCallback);
}
 
Example #19
Source File: MbsEnums.java    From mobly-bundled-snippets with Apache License 2.0 5 votes vote down vote up
private static RpcEnum buildBleScanResultCallbackTypeEnum() {
    RpcEnum.Builder builder = new RpcEnum.Builder();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return builder.build();
    }
    builder.add("CALLBACK_TYPE_ALL_MATCHES", ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        builder.add("CALLBACK_TYPE_FIRST_MATCH", ScanSettings.CALLBACK_TYPE_FIRST_MATCH);
        builder.add("CALLBACK_TYPE_MATCH_LOST", ScanSettings.CALLBACK_TYPE_MATCH_LOST);
    }
    return builder.build();
}
 
Example #20
Source File: RileyLinkBLEScanActivity.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void prepareForScanning() {
    // https://developer.android.com/training/permissions/requesting.html
    // http://developer.radiusnetworks.com/2015/09/29/is-your-beacon-app-ready-for-android-6.html
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.rileylink_scanner_ble_not_supported, Toast.LENGTH_SHORT).show();
    } else {
        // Use this check to determine whether BLE is supported on the device. Then
        // you can selectively disable BLE-related features.
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // your code that requires permission
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    PERMISSION_REQUEST_COARSE_LOCATION);
        }

        // Ensures Bluetooth is available on the device and it is enabled. If not,
        // displays a dialog requesting user permission to enable Bluetooth.
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this, R.string.rileylink_scanner_ble_not_enabled, Toast.LENGTH_SHORT).show();
        } else {

            // Will request that GPS be enabled for devices running Marshmallow or newer.
            if (!LocationHelper.isLocationEnabled(this)) {
                LocationHelper.requestLocationForBluetooth(this);
            }

            mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
            settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
            filters = Arrays.asList(new ScanFilter.Builder().setServiceUuid(
                    ParcelUuid.fromString(GattAttributes.SERVICE_RADIO)).build());

        }
    }

    // disable currently selected RL, so that we can discover it
    RileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect);
}
 
Example #21
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
static void startScan(BluetoothAdapter adapter, ScanSettings scanSettings, ScanCallback listener) {
    m_UserScanCallback = listener;
    // Add a last ditch check to make sure the adapter isn't null before trying to start the scan.
    // We check in the task, but by the time we reach this method, it could have been shut off
    // Either the adapter, or the scanner object may be null, so we check it here
    if (adapter == null || adapter.getBluetoothLeScanner() == null)
    {
        m_callback.onScanFailed(android.bluetooth.le.ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
        return;
    }
    adapter.getBluetoothLeScanner().startScan(null, scanSettings, m_callback);
}
 
Example #22
Source File: NewBleDeviceAdapterImpl.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void startScan(final BleDeviceScanCallback callback) {
    mCallback = callback;

    List<ScanFilter> filters = new ArrayList<ScanFilter>();
    if (mServiceUuids != null && mServiceUuids.length > 0) {
        for (UUID uuid : mServiceUuids) {
            ScanFilter filter = new ScanFilter.Builder().setServiceUuid(
                    new ParcelUuid(uuid)).build();
            filters.add(filter);
        }
    }
    ScanSettings.Builder builder = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        builder.setMatchMode(ScanSettings.MATCH_MODE_STICKY);
    }
    ScanSettings settings = builder.build();

    mBleScanner = mBluetoothAdapter.getBluetoothLeScanner();
    if (mBleScanner != null) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mBleScanner.startScan(filters, settings, mScanCallback);
        } else {
            // Unless required permissions were acquired, scan does not start.
            if (BleUtils.isBLEPermission(mContext)) {
                mBleScanner.startScan(filters, settings, mScanCallback);
            }
        }
    }
}
 
Example #23
Source File: RuuviTagScanner.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RuuviTagScanner(RuuviTagListener listener, Context context) {
    this.listener = listener;
    this.context = context;

    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();

}
 
Example #24
Source File: BluetoothScan.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(21)
private void scanLeDeviceLollipop(final boolean enable) {
    if (enable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            lollipopScanner = bluetooth_adapter.getBluetoothLeScanner();
        }
        if(lollipopScanner != null) {
            Log.d(TAG, "Starting scanner 21");
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    is_scanning = false;
                    if(bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
                        lollipopScanner.stopScan(mScanCallback);
                    }
                    invalidateOptionsMenu();
                }
            }, SCAN_PERIOD);
            ScanSettings settings = new ScanSettings.Builder()
                    .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                    .build();
            is_scanning = true;
            lollipopScanner.startScan(null, settings, mScanCallback);
        } else {
            try {
                scanLeDevice(true);
            } catch (Exception e) {
                Log.e(TAG, "Failed to scan for ble device", e);
            }
        }
    } else {
        is_scanning = false;
        if(bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
            lollipopScanner.stopScan(mScanCallback);
        }
    }
    invalidateOptionsMenu();
}
 
Example #25
Source File: DeviceServicesActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
private void startScanLeDevice() {
    ScanFilter macaddress = new ScanFilter.Builder().setDeviceAddress(reconnectaddress).build();
    ArrayList<ScanFilter> filters = new ArrayList<>();
    filters.add(macaddress);

    ScanSettings settings = new ScanSettings.Builder().build();
    bluetoothLeScanner.startScan(filters, settings, reScanCallback);

    Log.d("startScanLeDevice", "Scan Started");
}
 
Example #26
Source File: ThrottledLollipopScanner.java    From RxCentralBle with Apache License 2.0 5 votes vote down vote up
private void startScan(int scanMode) {
  BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  if (adapter != null && adapter.isEnabled()) {
    // Add a dummy filter to avoid Android 8.1+ enforcement of filters during background isScanning.
    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
    filters.add(scanFilterBuilder.build());

    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    settingsBuilder.setScanMode(scanMode);

    BluetoothLeScanner bleScanner = adapter.getBluetoothLeScanner();
    if (bleScanner != null) {
      bleScanner.startScan(filters, settingsBuilder.build(), scanCallback);
    } else {
      if (RxCentralLogger.isError()) {
        RxCentralLogger.error("startScan - BluetoothLeScanner is null!");
      }

      getErrorSubject().onError(new ConnectionError(SCAN_FAILED));
    }
  } else {
    if (RxCentralLogger.isError()) {
      if (adapter == null) {
        RxCentralLogger.error("startScan - Default Bluetooth Adapter is null!");
      } else {
        RxCentralLogger.error("startScan - Bluetooth Adapter is disabled.");
      }
    }

    getErrorSubject().onError(new ConnectionError(SCAN_FAILED));
  }
}
 
Example #27
Source File: M_Util.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public static void startNativeScan(BleManager mgr, int scanMode, Interval scanReportDelay, L_Util.ScanCallback listener) {
    final ScanSettings.Builder builder = L_Util.buildSettings(mgr, scanMode, scanReportDelay);

    builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
    builder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE);
    builder.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT);

    final ScanSettings scanSettings = builder.build();

    L_Util.startScan(mgr, scanSettings, listener);
}
 
Example #28
Source File: Wrappers.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public void startScan(
        List<ScanFilter> filters, int scanSettingsScanMode, ScanCallbackWrapper callback) {
    ScanSettings settings =
            new ScanSettings.Builder().setScanMode(scanSettingsScanMode).build();

    ForwardScanCallbackToWrapper callbackForwarder =
            new ForwardScanCallbackToWrapper(callback);
    mCallbacks.put(callback, callbackForwarder);

    mScanner.startScan(filters, settings, callbackForwarder);
}
 
Example #29
Source File: Manager.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);

    }
}
 
Example #30
Source File: TimeAttendantFastFragment.java    From iBeacon-Android with Apache License 2.0 5 votes vote down vote up
private void settingBlueTooth() {
    // init BLE
    btManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
    btAdapter = btManager.getAdapter();

    if (Build.VERSION.SDK_INT >= 21) {
        mLEScanner = btAdapter.getBluetoothLeScanner();
        settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();
        filters = new ArrayList<>();
    }
}