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

The following examples show how to use android.bluetooth.le.ScanFilter#Builder . These examples are extracted from open source projects. 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 Project: RxCentralBle   File: LollipopScanner.java    License: 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
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScanFilter createScanFilter() {
    ScanFilter.Builder builder = new ScanFilter.Builder()
            .setDeviceAddress(deviceAddress)
            .setDeviceName(deviceName);

    if (serviceUuid != null) {
        builder.setServiceUuid(serviceUuid, serviceUuidMask);
    }
    if (serviceDataUuid != null) {
        builder.setServiceData(serviceDataUuid, serviceData, serviceDataMask);
    }
    if (manufacturerId >= 0) {
        builder.setManufacturerData(manufacturerId, manufacturerData, manufacturerDataMask);
    }

    return builder.build();
}
 
Example 3
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScanFilter toApi21() {
    ScanFilter.Builder builder = new ScanFilter.Builder();
    if (mDeviceName != null) {
        builder.setDeviceName(mDeviceName);
    }
    if (mServiceUuid != null) {
        builder.setServiceUuid(mServiceUuid, mServiceUuidMask);
    }
    if (mDeviceAddress != null) {
        builder.setDeviceAddress(mDeviceAddress);
    }
    if (mServiceDataUuid != null) {
        builder.setServiceData(mServiceDataUuid, mServiceData, mServiceDataMask);
    }
    if (mManufacturerId < 0) {
        builder.setManufacturerData(mManufacturerId, mManufacturerData, mManufacturerDataMask);
    }
    return builder.build();
}
 
Example 4
/**
 * Return a List of {@link ScanFilter} objects to filter by Service UUID.
 */
private List<ScanFilter> buildScanFilters() {
    List<ScanFilter> scanFilters = new ArrayList<>();

    ScanFilter.Builder builder = new ScanFilter.Builder();
    // Comment out the below line to see all BLE devices around you
    builder.setServiceUuid(Constants.Service_UUID);
    scanFilters.add(builder.build());

    return scanFilters;
}
 
Example 5
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 6
/**
 * Return a List of {@link ScanFilter} objects to filter by Service UUID.
 */
private List<ScanFilter> buildScanFilters() {
    List<ScanFilter> scanFilters = new ArrayList<>();

    ScanFilter.Builder builder = new ScanFilter.Builder();
    // Comment out the below line to see all BLE devices around you
    builder.setServiceUuid(Constants.Service_UUID);
    scanFilters.add(builder.build());

    return scanFilters;
}
 
Example 7
public List<ScanFilter> createScanFiltersForBeaconParsers(List<BeaconParser> beaconParsers) {
    List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    // for each beacon parser, make a filter expression that includes all its desired
    // hardware manufacturers
    for (BeaconParser beaconParser: beaconParsers) {
        List<ScanFilterData> sfds = createScanFilterDataForBeaconParser(beaconParser);
        for (ScanFilterData sfd: sfds) {
            ScanFilter.Builder builder = new ScanFilter.Builder();
            if (sfd.serviceUuid != null) {
                // Use a 16 bit service UUID in a 128 bit form
                String serviceUuidString = String.format("0000%04X-0000-1000-8000-00805f9b34fb", sfd.serviceUuid);
                String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
                ParcelUuid parcelUuid = ParcelUuid.fromString(serviceUuidString);
                ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString);
                if (LogManager.isVerboseLoggingEnabled()) {
                    LogManager.d(TAG, "making scan filter for service: "+serviceUuidString+" "+parcelUuid);
                    LogManager.d(TAG, "making scan filter with service mask: "+serviceUuidMaskString+" "+parcelUuidMask);
                }
                builder.setServiceUuid(parcelUuid, parcelUuidMask);
            }
            else {
                builder.setServiceUuid(null);
                builder.setManufacturerData((int) sfd.manufacturer, sfd.filter, sfd.mask);
            }
            ScanFilter scanFilter = builder.build();
            if (LogManager.isVerboseLoggingEnabled()) {
                LogManager.d(TAG, "Set up a scan filter: "+scanFilter);
            }
            scanFilters.add(scanFilter);
        }
    }
    return scanFilters;
}
 
Example 8
public List<ScanFilter> createWildcardScanFilters() {
    List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    ScanFilter.Builder builder = new ScanFilter.Builder();
    scanFilters.add(builder.build());
    return scanFilters;
}