Example usage for android.bluetooth.le BluetoothLeScanner startScan

List of usage examples for android.bluetooth.le BluetoothLeScanner startScan

Introduction

In this page you can find the example usage for android.bluetooth.le BluetoothLeScanner startScan.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public int startScan(@Nullable List<ScanFilter> filters, @Nullable ScanSettings settings,
        @NonNull PendingIntent callbackIntent) 

Source Link

Document

Start Bluetooth LE scan using a PendingIntent .

Usage

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * setPeriod? ? ? 1 ? ? scan // www  .ja  va2  s  . co  m
 * @param enable
 * @param setPeriod
 */
public void scanLeDevicePeriodically(final boolean enable, final boolean setPeriod) {
    mIsBleScanPeriodic = setPeriod;

    Log.d(TAG, "mIsBleScanPeriodic = " + mIsBleScanPeriodic);

    Log.d(TAG, "scanLeDevicePeriodically enabled = " + enable);
    /**
     * You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetooth LE devices with startLeScan().
     * Caution: Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources.
            
     * Additional : On LG Nexus 4 with Android 4.4.2 startDiscovery() finds Bluetooth LE devices.
     *       On Samsung Galaxy S3 with Android 4.3 startDiscovery() doesn't find Bluetooth LE devices.
     */
    if (enable) {
        if (mIsLeScanning) {
            Log.d(TAG, "Already scanning enabled...");
            return;
        }
        mBleServiceHandler.removeMessages(HANDLER_MSG_EXPIRED_SCAN_PERIOD);
        mBleServiceHandler.removeMessages(HANDLER_MSG_EXPIRED_SCAN_WAIT_PERIOD);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BluetoothLeScanner leScanner = mBluetoothAdapter.getBluetoothLeScanner();
            if (leScanner != null) {
                leScanner.startScan(Collections.<ScanFilter>emptyList(),
                        new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(),
                        mLeScanLollipopCallback);

            }
        } else {
            // deprecated api 21.
            mBluetoothAdapter.startLeScan(mLeScanKitkatCallback);
        }
        // Stops scanning after a pre-defined scan period.

        long scanTime = SCAN_PERIOD;
        if (!mIsBatteryPlugged) {
            scanTime *= 2; // default = 10 sec, unplugged = 20sec
        }
        Log.d(TAG, "Set.. scan time ms = " + scanTime);
        mBleServiceHandler.sendEmptyMessageDelayed(HANDLER_MSG_EXPIRED_SCAN_PERIOD, scanTime);
        mIsLeScanning = true;
    } else {
        if (!mIsLeScanning) {
            Log.d(TAG, "Already scanning stopped...");
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                BluetoothLeScanner leScanner = mBluetoothAdapter.getBluetoothLeScanner();
                if (leScanner != null) {
                    leScanner.stopScan(mLeScanLollipopCallback);

                }
            } else {
                // deprecated api 21.
                mBluetoothAdapter.stopLeScan(mLeScanKitkatCallback);
            }
        }
        mBleServiceHandler.removeMessages(HANDLER_MSG_EXPIRED_SCAN_PERIOD);
        mBleServiceHandler.removeMessages(HANDLER_MSG_EXPIRED_SCAN_WAIT_PERIOD);

        long waitTime = SCAN_WAIT_PERIOD;
        if (!mIsBatteryPlugged) {
            waitTime = SCAN_WAIT_UNPLUGGED_PERIOD; // default = 290 sec, unplugged =  30.
        }

        if (mIsBleScanPeriodic) {
            Log.d(TAG, "Set.. scan wait time ms = " + waitTime);
            mBleServiceHandler.sendEmptyMessageDelayed(HANDLER_MSG_EXPIRED_SCAN_WAIT_PERIOD, waitTime);
        }
        mIsLeScanning = false;
    }
}