Example usage for android.bluetooth BluetoothAdapter cancelDiscovery

List of usage examples for android.bluetooth BluetoothAdapter cancelDiscovery

Introduction

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

Prototype

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

Source Link

Document

Cancel the current device discovery process.

Usage

From source file:Main.java

public static void cancelDiscovery(BluetoothAdapter adapter) {
    if (null != adapter) {
        adapter.cancelDiscovery();
    }//from  w  w w . j  a  v a 2 s.c om
}

From source file:Main.java

public static void stopBtSearch() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.cancelDiscovery();
}

From source file:Main.java

/**
 * Populates the device names and the device addresses with all the suitable
 * bluetooth devices./*from  w ww.  j  a v a2  s  .c  o  m*/
 * 
 * @param bluetoothAdapter the bluetooth adapter
 * @param deviceNames list of device names
 * @param deviceAddresses list of device addresses
 */
public static void populateDeviceLists(BluetoothAdapter bluetoothAdapter, List<String> deviceNames,
        List<String> deviceAddresses) {
    // Ensure the bluetooth adapter is not in discovery mode.
    bluetoothAdapter.cancelDiscovery();

    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
    for (BluetoothDevice device : pairedDevices) {
        BluetoothClass bluetoothClass = device.getBluetoothClass();
        if (bluetoothClass != null) {
            // Not really sure what we want, but I know what we don't want.
            switch (bluetoothClass.getMajorDeviceClass()) {
            case BluetoothClass.Device.Major.COMPUTER:
            case BluetoothClass.Device.Major.PHONE:
                break;
            default:
                deviceAddresses.add(device.getAddress());
                deviceNames.add(device.getName());
            }
        }
    }
}

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  v  a 2 s. c om
    }

    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();

}