no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat Java Examples

The following examples show how to use no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat. 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   Author: adafruit   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
Source Project: Android-nRF-Mesh-Library   Author: NordicSemiconductor   File: NrfMeshRepository.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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: mcumgr-android   Author: JuulLabs-OSS   File: ScannerViewModel.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Start scanning for Bluetooth LE devices.
 */
public void startScan() {
    if (mScannerStateLiveData.isScanning()) {
        return;
    }

    // Scanning settings
    final ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setLegacy(false)
            .setReportDelay(500)
            .setUseHardwareBatchingIfSupported(false)
            .build();

    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.startScan(null, settings, scanCallback);
    mScannerStateLiveData.scanningStarted();
}
 
Example #4
Source Project: Android-nRF-Blinky   Author: NordicSemiconductor   File: ScannerViewModel.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Start scanning for Bluetooth devices.
 */
public void startScan() {
	if (scannerStateLiveData.isScanning()) {
		return;
	}

	// Scanning settings
	final ScanSettings settings = new ScanSettings.Builder()
			.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
			.setReportDelay(500)
			.setUseHardwareBatchingIfSupported(false)
			.build();

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	scanner.startScan(null, settings, scanCallback);
	scannerStateLiveData.scanningStarted();
}
 
Example #5
Source Project: Android-nRF-BLE-Joiner   Author: NordicSemiconductor   File: MainActivity.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #6
Source Project: Android-nRF-Toolbox   Author: NordicSemiconductor   File: DevicesAdapter.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void startLeScan() {
	// Scanning is disabled when we are connecting or connected.
	if (connectingPosition >= 0)
		return;

	if (scanning) {
		// Extend scanning for some time more
		handler.removeCallbacks(stopScanTask);
		handler.postDelayed(stopScanTask, SCAN_DURATION);
		return;
	}

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	final ScanSettings settings = new ScanSettings.Builder().setReportDelay(1000).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
	scanner.startScan(null, settings, scanCallback);

	// Setup timer that will stop scanning
	handler.postDelayed(stopScanTask, SCAN_DURATION);
	scanning = true;
	notifyItemChanged(devices.size());
}
 
Example #7
Source Project: Bluefruit_LE_Connect_Android_V2   Author: adafruit   File: BleScanner.java    License: MIT License 5 votes vote down vote up
public synchronized void stop() {
    if (mIsScanning) {
        BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
        scanner.stopScan(mScanCallback);
        mIsScanning = false;
        BleScannerListener listener = mWeakListener.get();
        if (listener != null) {
            listener.onScanStatusChanged(false);
        }
    }
}
 
Example #8
Source Project: Android-nRF-Mesh-Library   Author: NordicSemiconductor   File: ScannerRepository.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 #9
Source Project: Android-nRF-Mesh-Library   Author: NordicSemiconductor   File: NrfMeshRepository.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * stop scanning for bluetooth devices.
 */
private void stopScan() {
    mHandler.removeCallbacks(mScannerTimeout);
    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.stopScan(scanCallback);
    mIsScanning = false;
}
 
Example #10
Source Project: Android-nRF-Beacon-for-Eddystone   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 #11
Source Project: Android-nRF-Beacon-for-Eddystone   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Stop scan if user tap Cancel button
 */
private void stopScan() {
	if (mIsScanning) {
		mScanButton.setText(R.string.scanner_action_scan);

		final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
		scanner.stopScan(scanCallback);

		mIsScanning = false;
	}
}
 
Example #12
Source Project: Android-nRF-Blinky   Author: NordicSemiconductor   File: ScannerViewModel.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Stop scanning for bluetooth devices.
 */
public void stopScan() {
	if (scannerStateLiveData.isScanning() && scannerStateLiveData.isBluetoothEnabled()) {
		final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
		scanner.stopScan(scanCallback);
		scannerStateLiveData.scanningStopped();
	}
}
 
Example #13
Source Project: Android-nRF-Toolbox   Author: NordicSemiconductor   File: DevicesAdapter.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void stopLeScan() {
	if (!scanning)
		return;

	final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
	scanner.stopScan(scanCallback);

	handler.removeCallbacks(stopScanTask);
	scanning = false;
	notifyItemChanged(devices.size());
}
 
Example #14
Source Project: Android-nRF-Toolbox   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 #15
Source Project: Android-nRF-Toolbox   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Stop scan if user tap Cancel button
 */
private void stopScan() {
	if (scanning) {
		scanButton.setText(R.string.scanner_action_scan);

		final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
		scanner.stopScan(scanCallback);

		scanning = false;
	}
}
 
Example #16
Source Project: Android-nRF-Beacon   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 #17
Source Project: Android-nRF-Beacon   Author: NordicSemiconductor   File: ScannerFragment.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Stop scan if user tap Cancel button
 */
private void stopScan() {
	if (mIsScanning) {
		mScanButton.setText(R.string.scanner_action_scan);

		final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
		scanner.stopScan(scanCallback);

		mIsScanning = false;
	}
}
 
Example #18
Source Project: Android-nRF-Mesh-Library   Author: NordicSemiconductor   File: ScannerRepository.java    License: BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * stop scanning for bluetooth devices.
 */
public void stopScan() {
    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.stopScan(mScanCallbacks);
    mScannerStateLiveData.scanningStopped();
}
 
Example #19
Source Project: mcumgr-android   Author: JuulLabs-OSS   File: ScannerViewModel.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Stop scanning for Bluetooth LE devices.
 */
public void stopScan() {
    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.stopScan(scanCallback);
    mScannerStateLiveData.scanningStopped();
}