Example usage for android.bluetooth BluetoothAdapter isDiscovering

List of usage examples for android.bluetooth BluetoothAdapter isDiscovering

Introduction

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

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH)
public boolean isDiscovering() 

Source Link

Document

Return true if the local Bluetooth adapter is currently in the device discovery process.

Usage

From source file:Main.java

public static boolean isBtSearch() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter.isDiscovering();
}

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

protected void startWithDelay(long d) {
    if (backgroundService != null && backgroundService.getServiceState() == ServiceState.SERVICE_STARTED) {
        return;/*from   w w  w.j a  va 2  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);
}

From source file:cz.tomsuch.lampicka.activities.LampActivity.java

/**
 * If there is discovered Service UUID, it will initialize connection,
 * creates BluetoothSocket and retrieves informations about device (X,i)
 * *///from  w  w w.j a v a2 s. c o  m
private void connectToService() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                setStatus(R.string.lamp_connecting);
                if (socket != null) {
                    socket.close();
                }
                socket = new FixedBluetoothSocket(
                        BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress())
                                .createRfcommSocketToServiceRecord(serviceUuid.getUuid()));

                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                if (adapter.isDiscovering())
                    adapter.cancelDiscovery();

                if (socket != null) {
                    try {
                        socket.connect();
                    } catch (Exception e) {
                        e.printStackTrace();
                        disconnect();
                        showToast(R.string.toast_cannot_connect_to_bluetooth_lamp);
                        return;
                    }
                }
                if (socket.isConnected()) {
                    afterSocketConnected();
                    bsl = new BluetoothSocketListener(socket, bluetoothInputListener);
                    new Thread(bsl).start();
                } else {
                    showToast(R.string.toast_could_not_connect);
                    disconnect();
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }).start();

}