Java Code Examples for no.nordicsemi.android.support.v18.scanner.ScanFilter

The following examples show how to use no.nordicsemi.android.support.v18.scanner.ScanFilter. 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: Bluefruit_LE_Connect_Android_V2   Source File: BleScanner.java    License: MIT License 6 votes vote down vote up
private synchronized void startWithFilters(@Nullable List<ScanFilter> filters) {
    if (!mIsScanning) {
        BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
        ScanSettings settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(kScanReportDelay)
                .setUseHardwareBatchingIfSupported(false).build();
        mScanFilters = filters;
        try {
            scanner.startScan(mScanFilters, settings, mScanCallback);
            mIsScanning = true;
            BleScannerListener listener = mWeakListener.get();
            if (listener != null) {
                listener.onScanStatusChanged(true);
            }
        } catch (IllegalStateException e) {     // Exception if the BT adapter is not on
            Log.d(TAG, "startWithFilters illegalStateExcpetion" + e.getMessage());
        }
    }
}
 
Example 2
/**
 * Starts reconnecting to the device
 */
private void startScan() {
    if (mIsScanning)
        return;

    mIsScanning = true;
    // Scanning settings
    final ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            // Refresh the devices list every second
            .setReportDelay(0)
            // Hardware filtering has some issues on selected devices
            .setUseHardwareFilteringIfSupported(false)
            // Samsung S6 and S6 Edge report equal value of RSSI for all devices. In this app we ignore the RSSI.
            /*.setUseHardwareBatchingIfSupported(false)*/
            .build();

    // Let's use the filter to scan only for Mesh devices
    final List<ScanFilter> filters = new ArrayList<>();
    filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid((MESH_PROXY_UUID))).build());

    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.startScan(filters, settings, scanCallback);
    Log.v(TAG, "Scan started");
    mHandler.postDelayed(mScannerTimeout, 20000);
}
 
Example 3
Source Project: blefun-androidthings   Source File: ScanActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void startLeScan() {
    mScanning = true;

    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setReportDelay(1000)
            .build();
    List<ScanFilter> filters = new ArrayList<>();
    filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(SERVICE_UUID)).build());
    mScanner.startScan(filters, settings, mScanCallback);

    // Stops scanning after a pre-defined scan period.
    mStopScanHandler.postDelayed(mStopScanRunnable, SCAN_TIMEOUT_MS);

    invalidateOptionsMenu();
}
 
Example 4
private void prepareForScan(){
    if(isBleSupported()) {
        final ParcelUuid uuid = NODE_CONFIGURATION_SERVICE;
        mScanFilterList = new ArrayList<>();
        mScanFilterList.add(new ScanFilter.Builder().setServiceUuid(uuid).build());
        mScanner = BluetoothLeScannerCompat.getScanner();

        if (checkIfVersionIsMarshmallowOrAbove()) {
            startLocationModeChangeReceiver();
            connectToGoogleApiClient();
        } else {
            if (!isBleEnabled()) {
                final Intent bluetoothEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(bluetoothEnable, REQUEST_ENABLE_BT);
            } else {
                startLeScan();
            }
        }
    } else {
        showError(getString(R.string.ble_not_supported), false);
    }
}
 
Example 5
Source Project: Bluefruit_LE_Connect_Android_V2   Source File: BleScanner.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
public void startFilteringServiceUuid(ParcelUuid uuid) {

    List<ScanFilter> scanFilters = new ArrayList<>();
    if (uuid != null) {
        scanFilters.add(new ScanFilter.Builder().setServiceUuid(uuid).build());
    }

    startWithFilters(scanFilters);
}
 
Example 6
/**
 * Start scanning for Bluetooth devices.
 *
 * @param filterUuid UUID to filter scan results with
 */
public void startScan(final UUID filterUuid) {
    mFilterUuid = filterUuid;

    if (mScannerStateLiveData.isScanning()) {
        return;
    }

    if (mFilterUuid.equals(BleMeshManager.MESH_PROXY_UUID)) {
        final MeshNetwork network = mMeshManagerApi.getMeshNetwork();
        if (network != null) {
            if (!network.getNetKeys().isEmpty()) {
                mNetworkId = mMeshManagerApi.generateNetworkId(network.getNetKeys().get(0).getKey());
            }
        }
    }

    mScannerStateLiveData.scanningStarted();
    //Scanning settings
    final ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            // Refresh the devices list every second
            .setReportDelay(0)
            // Hardware filtering has some issues on selected devices
            .setUseHardwareFilteringIfSupported(false)
            // Samsung S6 and S6 Edge report equal value of RSSI for all devices. In this app we ignore the RSSI.
            /*.setUseHardwareBatchingIfSupported(false)*/
            .build();

    //Let's use the filter to scan only for unprovisioned mesh nodes.
    final List<ScanFilter> filters = new ArrayList<>();
    filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid((filterUuid))).build());

    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.startScan(filters, settings, mScanCallbacks);
}
 
Example 7
/**
 * Scan for 5 seconds and then stop scanning when a BluetoothLE device is found then mLEScanCallback is activated This will perform regular scan for custom BLE Service UUID and then filter out
 * using class ScannerServiceParser
 */
private void startScan() {
	// Since Android 6.0 we need to obtain either Manifest.permission.ACCESS_COARSE_LOCATION or Manifest.permission.ACCESS_FINE_LOCATION to be able to scan for
	// Bluetooth LE devices. This is related to beacons as proximity devices.
	// On API older than Marshmallow the following code does nothing.
	if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
		// When user pressed Deny and still wants to use this functionality, show the rationale
		if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) && mPermissionRationale.getVisibility() == View.GONE) {
			mPermissionRationale.setVisibility(View.VISIBLE);
			return;
		}

		requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_REQ_CODE);
		return;
	}

	// Hide the rationale message, we don't need it anymore.
	if (mPermissionRationale != null)
		mPermissionRationale.setVisibility(View.GONE);

	mAdapter.clearDevices();
	mScanButton.setText(R.string.scanner_action_cancel);

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	final ScanSettings settings = new ScanSettings.Builder()
			.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).setUseHardwareFilteringIfSupported(false).build();
	final List<ScanFilter> filters = new ArrayList<>();
	filters.add(new ScanFilter.Builder().setServiceUuid(mUuid).build());
	scanner.startScan(filters, settings, scanCallback);

	mIsScanning = true;
	mHandler.postDelayed(new Runnable() {
		@Override
		public void run() {
			if (mIsScanning) {
				stopScan();
			}
		}
	}, SCAN_DURATION);
}
 
Example 8
/**
 * Scan for 5 seconds and then stop scanning when a BluetoothLE device is found then lEScanCallback
 * is activated This will perform regular scan for custom BLE Service UUID and then filter out.
 * using class ScannerServiceParser
 */
private void startScan() {
	// Since Android 6.0 we need to obtain Manifest.permission.ACCESS_FINE_LOCATION to be able to scan for
	// Bluetooth LE devices. This is related to beacons as proximity devices.
	// On API older than Marshmallow the following code does nothing.
	if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
		// When user pressed Deny and still wants to use this functionality, show the rationale
		if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), Manifest.permission.ACCESS_FINE_LOCATION) && permissionRationale.getVisibility() == View.GONE) {
			permissionRationale.setVisibility(View.VISIBLE);
			return;
		}

		requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_REQ_CODE);
		return;
	}

	// Hide the rationale message, we don't need it anymore.
	if (permissionRationale != null)
		permissionRationale.setVisibility(View.GONE);

	adapter.clearDevices();
	scanButton.setText(R.string.scanner_action_cancel);

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	final ScanSettings settings = new ScanSettings.Builder()
			.setLegacy(false)
			.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).build();
	final List<ScanFilter> filters = new ArrayList<>();
	filters.add(new ScanFilter.Builder().setServiceUuid(uuid).build());
	scanner.startScan(filters, settings, scanCallback);

	scanning = true;
	handler.postDelayed(() -> {
		if (scanning) {
			stopScan();
		}
	}, SCAN_DURATION);
}
 
Example 9
/**
 * Scan for 5 seconds and then stop scanning when a BluetoothLE device is found then mLEScanCallback is activated This will perform regular scan for custom BLE Service UUID and then filter out
 * using class ScannerServiceParser
 */
private void startScan() {
	// Since Android 6.0 we need to obtain either Manifest.permission.ACCESS_COARSE_LOCATION or Manifest.permission.ACCESS_FINE_LOCATION to be able to scan for
	// Bluetooth LE devices. This is related to beacons as proximity devices.
	// On API older than Marshmallow the following code does nothing.
	if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
		// When user pressed Deny and still wants to use this functionality, show the rationale
		if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) && mPermissionRationale.getVisibility() == View.GONE) {
			mPermissionRationale.setVisibility(View.VISIBLE);
			return;
		}

		requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_REQ_CODE);
		return;
	}

	// Hide the rationale message, we don't need it anymore.
	if (mPermissionRationale != null)
		mPermissionRationale.setVisibility(View.GONE);

	mAdapter.clearDevices();
	mScanButton.setText(R.string.scanner_action_cancel);

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	final ScanSettings settings = new ScanSettings.Builder()
			.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).setUseHardwareFilteringIfSupported(false).build();
	final List<ScanFilter> filters = new ArrayList<>();
	filters.add(new ScanFilter.Builder().setServiceUuid(mUuid).build());
	scanner.startScan(filters, settings, scanCallback);

	mIsScanning = true;
	mHandler.postDelayed(new Runnable() {
		@Override
		public void run() {
			if (mIsScanning) {
				stopScan();
			}
		}
	}, SCAN_DURATION);
}