Example usage for android.bluetooth BluetoothAdapter startLeScan

List of usage examples for android.bluetooth BluetoothAdapter startLeScan

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter startLeScan.

Prototype

@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) 

Source Link

Document

Starts a scan for Bluetooth LE devices, looking for devices that advertise given services.

Usage

From source file:net.emilymaier.movebot.HeartFragment.java

@Override
@SuppressWarnings("deprecation")
public void onClick(View view) {
    Log.d("HeartFragment", "Scan button clicked");
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        new NoBluetoothDialogFragment().show(act.getSupportFragmentManager(), "bluetooth");
        return;//from   w  w w  .j av  a 2  s.  c  o  m
    }
    if (!bluetoothAdapter.isEnabled()) {
        new BluetoothDisabledDialogFragment().show(act.getSupportFragmentManager(), "disabled");
        return;
    }
    if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        new NoLeDialogFragment().show(act.getSupportFragmentManager(), "le");
        return;
    }
    Log.d("HeartFragment", "Starting scan");
    heartScan.setClickable(false);
    heartScan.setText("Scanning...");
    for (HeartDevice device : heartDevices) {
        device.bluetoothGatt.disconnect();
        device.bluetoothGatt.close();
    }
    heartDevices.clear();
    heartListAdapter.notifyDataSetChanged();
    bluetoothAdapter.startLeScan(new UUID[] { UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb") }, this);
    final HeartFragment t = this;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d("HeartFragment", "Stopping scan");
            bluetoothAdapter.stopLeScan(t);
            heartScan.setText("Scan");
            heartScan.setClickable(true);
            heartListAdapter.notifyDataSetChanged();
        }
    }, 5000);
}