Example usage for android.bluetooth BluetoothAdapter startDiscovery

List of usage examples for android.bluetooth BluetoothAdapter startDiscovery

Introduction

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

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startDiscovery() 

Source Link

Document

Start the remote device discovery process.

Usage

From source file:Main.java

public static void startBtSearch() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.startDiscovery();
}

From source file:org.envirocar.app.application.service.DeviceInRangeService.java

protected void startWithDelay(long d) {
    if (backgroundService != null && backgroundService.getServiceState() == ServiceState.SERVICE_STARTED) {
        return;/* w w  w . j a va2  s .  c o  m*/
    }

    discoveryEnabled = true;

    discoveryRunnable = new Runnable() {
        @Override
        public void run() {
            if (!discoveryEnabled) {
                return;
            }

            logger.info("starting device discovery...");
            Intent intent = new Intent(AbstractBackgroundServiceStateReceiver.SERVICE_STATE);
            intent.putExtra(AbstractBackgroundServiceStateReceiver.SERVICE_STATE,
                    ServiceState.SERVICE_DEVICE_DISCOVERY_RUNNING);
            sendBroadcast(intent);

            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if (adapter != null) {
                if (adapter.isDiscovering()) {
                    adapter.cancelDiscovery();
                }
                adapter.startDiscovery();
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                logger.warn(e.getMessage(), e);
            }

            if (!discoveryEnabled) {
                /*
                 * we are done, we found the device
                 */
                return;
            }

            /*
             * update the target time and send a broadcast
             */
            intent = new Intent(AbstractBackgroundServiceStateReceiver.SERVICE_STATE);
            intent.putExtra(AbstractBackgroundServiceStateReceiver.SERVICE_STATE,
                    ServiceState.SERVICE_DEVICE_DISCOVERY_PENDING);
            sendBroadcast(intent);

            targetSystemTime = System.currentTimeMillis() + DISCOVERY_PERIOD;

            /*
             * re-schedule ourselves
             */
            invokeDiscoveryRunnable(DISCOVERY_PERIOD);
        }

    };

    if (d > 0) {
        Intent intent = new Intent(AbstractBackgroundServiceStateReceiver.SERVICE_STATE);
        intent.putExtra(AbstractBackgroundServiceStateReceiver.SERVICE_STATE,
                ServiceState.SERVICE_DEVICE_DISCOVERY_PENDING);
        sendBroadcast(intent);
    }

    targetSystemTime = System.currentTimeMillis() + d;

    /*
     * do the actual invoking
     */
    invokeDiscoveryRunnable(d);
}