Example usage for android.bluetooth.le ScanSettings SCAN_MODE_LOW_LATENCY

List of usage examples for android.bluetooth.le ScanSettings SCAN_MODE_LOW_LATENCY

Introduction

In this page you can find the example usage for android.bluetooth.le ScanSettings SCAN_MODE_LOW_LATENCY.

Prototype

int SCAN_MODE_LOW_LATENCY

To view the source code for android.bluetooth.le ScanSettings SCAN_MODE_LOW_LATENCY.

Click Source Link

Document

Scan using highest duty cycle.

Usage

From source file:ch.ethz.inf.vs.a1.fabischn.ble.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // TODO when bluetooth is off, concurrently the onCreate keeps running.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();// w  w  w  .j  ava2  s  . c  om
        return;
    }

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // TODO onActivityResult, check if Cancelled or not, then continue or leave
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        // REQUEST_ENABLE_BT will be returned as requestCode from onActivityResult
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // TODO change this if onActivity Result is used
    // Continues scanning if enabled, else toast and finish
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    // TODO lookup the correct idiom for this, look at onRequestPermissionsResult
    // We need this for Android 6.0+
    if (checkLocationPermission()) {

    } else {
        ActivityCompat.requestPermissions(this,
                new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 1);
    }

    if (mBluetoothAdapter != null) {
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    }
    ScanFilter.Builder filterBuilder = new ScanFilter.Builder();
    mScanFilter = filterBuilder.setDeviceName("Smart Humigadget").build();
    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    mScanSettings = settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            //                .setMatchMode(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            //                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            .build();
    mScanFilters = new ArrayList<ScanFilter>();
    mScanFilters.add(mScanFilter);

}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

@Override
public void onResume() {
    super.onResume();

    MovementProfile.status = 0;//from  www .j a  va  2  s .  c om

    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(QuickstartPreferences.REGISTRATION_READY));
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(QuickstartPreferences.REGISTRATION_GENERATING));
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEET_ENABLE_BT);
    } else {
        if (Build.VERSION.SDK_INT >= 21) {
            mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
            settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
        }
    }
}

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

/**
 * setPeriod? ? ? 1 ? ? scan /*from   w  w  w .ja  va  2 s . com*/
 * @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;
    }
}