Java Code Examples for android.bluetooth.le.ScanSettings#Builder

The following examples show how to use android.bluetooth.le.ScanSettings#Builder . 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: 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 2
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 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: 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 5
Source File: M_Util.java    From AsteroidOSSync 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 6
Source File: M_Util.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
public static void startNativeScan(BluetoothAdapter adapter, int scanMode, Interval scanReportDelay, L_Util.ScanCallback listener) {
    final ScanSettings.Builder builder = L_Util.buildSettings(adapter, 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(adapter, scanSettings, listener);
}
 
Example 7
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
static ScanSettings.Builder buildSettings(BluetoothAdapter adapter, int scanMode, Interval scanReportDelay) {
    final ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(scanMode);

    if( adapter.isOffloadedScanBatchingSupported() )
    {
        final long scanReportDelay_millis = false == Interval.isDisabled(scanReportDelay) ? scanReportDelay.millis() : 0;
        builder.setReportDelay(scanReportDelay_millis);
    }
    else
    {
        builder.setReportDelay(0);
    }
    return builder;
}
 
Example 8
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 9
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 10
Source File: M_Util.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
public static void startNativeScan(BluetoothAdapter adapter, int scanMode, Interval scanReportDelay, L_Util.ScanCallback listener) {
    final ScanSettings.Builder builder = L_Util.buildSettings(adapter, 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(adapter, scanSettings, listener);
}
 
Example 11
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
static ScanSettings.Builder buildSettings(BluetoothAdapter adapter, int scanMode, Interval scanReportDelay) {
    final ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(scanMode);

    if( adapter.isOffloadedScanBatchingSupported() )
    {
        final long scanReportDelay_millis = false == Interval.isDisabled(scanReportDelay) ? scanReportDelay.millis() : 0;
        builder.setReportDelay(scanReportDelay_millis);
    }
    else
    {
        builder.setReportDelay(0);
    }
    return builder;
}
 
Example 12
Source File: ScannerFragment.java    From connectivity-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Return a {@link ScanSettings} object set to use low power (to preserve battery life).
 */
private ScanSettings buildScanSettings() {
    ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
    return builder.build();
}
 
Example 13
Source File: LollipopScanManager.java    From react-native-ble-manager with Apache License 2.0 4 votes vote down vote up
@Override
public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options,  Callback callback) {
    ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
    List<ScanFilter> filters = new ArrayList<>();
    
    if (options.hasKey("scanMode")) {
        scanSettingsBuilder.setScanMode(options.getInt("scanMode"));
    }
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (options.hasKey("numberOfMatches")) {
            scanSettingsBuilder.setNumOfMatches(options.getInt("numberOfMatches"));
        }
        if (options.hasKey("matchMode")) {
            scanSettingsBuilder.setMatchMode(options.getInt("matchMode"));
        }
    }

    if (options.hasKey("reportDelay")) {
        scanSettingsBuilder.setReportDelay(options.getInt("reportDelay"));
    }
    
    if (serviceUUIDs.size() > 0) {
        for(int i = 0; i < serviceUUIDs.size(); i++){
ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUIDHelper.uuidFromString(serviceUUIDs.getString(i)))).build();
            filters.add(filter);
            Log.d(bleManager.LOG_TAG, "Filter service: " + serviceUUIDs.getString(i));
        }
    }
    
    getBluetoothAdapter().getBluetoothLeScanner().startScan(filters, scanSettingsBuilder.build(), mScanCallback);
    if (scanSeconds > 0) {
        Thread thread = new Thread() {
            private int currentScanSession = scanSessionId.incrementAndGet();
            
            @Override
            public void run() {
                
                try {
                    Thread.sleep(scanSeconds * 1000);
                } catch (InterruptedException ignored) {
                }
                
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        BluetoothAdapter btAdapter = getBluetoothAdapter();
                        // check current scan session was not stopped
                        if (scanSessionId.intValue() == currentScanSession) {
                            if(btAdapter.getState() == BluetoothAdapter.STATE_ON) {
                                btAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
                            }
                            WritableMap map = Arguments.createMap();
                            bleManager.sendEvent("BleManagerStopScan", map);
                        }
                    }
                });
                
            }
            
        };
        thread.start();
    }
    callback.invoke();
}
 
Example 14
Source File: ScannerFragment.java    From android-BluetoothAdvertisements with Apache License 2.0 4 votes vote down vote up
/**
 * Return a {@link ScanSettings} object set to use low power (to preserve battery life).
 */
private ScanSettings buildScanSettings() {
    ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
    return builder.build();
}
 
Example 15
Source File: BluetoothScanWorker.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
static void startLEScan(final Context context)
{
    //PPApplication.logE("BluetoothScanWorker.startLEScan", "xxx");

    if (BluetoothScanner.bluetoothLESupported(context)) {

        synchronized (PPApplication.bluetoothLEScanMutex) {

            if (bluetooth == null)
                bluetooth = BluetoothAdapter.getDefaultAdapter(); //getBluetoothAdapter(context);

            if (bluetooth != null) {
                if (Permissions.checkLocation(context)) {
                    try {
                        if (BluetoothScanner.bluetoothLEScanner == null)
                            BluetoothScanner.bluetoothLEScanner = bluetooth.getBluetoothLeScanner();

                        ScanSettings.Builder builder = new ScanSettings.Builder();

                        tmpScanLEResults = null;

                        int forceScan = ApplicationPreferences.prefForceOneBluetoothScan;
                        if (forceScan == BluetoothScanner.FORCE_ONE_SCAN_FROM_PREF_DIALOG)
                            builder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
                        else
                            builder.setScanMode(ScanSettings.SCAN_MODE_BALANCED);

                        if (bluetooth.isOffloadedScanBatchingSupported())
                            builder.setReportDelay(ApplicationPreferences.applicationEventBluetoothLEScanDuration * 1000);
                        ScanSettings settings = builder.build();

                        List<ScanFilter> filters = new ArrayList<>();

                        if (bluetooth.isEnabled()) {
                            BluetoothScanner.bluetoothLEScanner.startScan(filters, settings, new BluetoothLEScanCallback21(context));
                            setWaitForLEResults(context, true);
                        }

                        //PPApplication.logE("BluetoothScanWorker.startLEScan", "scanStarted=" + startScan);
                    } catch (Exception e) {
                        PPApplication.recordException(e);
                    }
                }
                setLEScanRequest(context, false);
            }
        }
    }
}