Java Code Examples for android.bluetooth.BluetoothAdapter#cancelDiscovery()

The following examples show how to use android.bluetooth.BluetoothAdapter#cancelDiscovery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: P_BluetoothCrashResolver.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {
    try {
        Thread.sleep(TIME_TO_LET_DISCOVERY_RUN_MILLIS);
        if (!discoveryStartConfirmed) {
            Log.w(TAG, "BluetoothAdapter.ACTION_DISCOVERY_STARTED never received.  Recovery may fail.");
        }

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter.isDiscovering()) {
            if (isDebugEnabled()) Log.d(TAG, "Cancelling discovery");
            adapter.cancelDiscovery();
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Discovery not running.  Won't cancel it");
        }
    } catch (InterruptedException e) {
        if (isDebugEnabled()) Log.d(TAG, "DiscoveryCanceller sleep interrupted.");
    }
    return null;
}
 
Example 2
Source File: BluetoothDeviceUtils.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Populates the device names and the device addresses with all the suitable
 * bluetooth devices.
 * 
 * @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());
      }
    }
  }
}
 
Example 3
Source File: P_BluetoothCrashResolver.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {
    try {
        Thread.sleep(TIME_TO_LET_DISCOVERY_RUN_MILLIS);
        if (!discoveryStartConfirmed) {
            Log.w(TAG, "BluetoothAdapter.ACTION_DISCOVERY_STARTED never received.  Recovery may fail.");
        }

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter.isDiscovering()) {
            if (isDebugEnabled()) Log.d(TAG, "Cancelling discovery");
            adapter.cancelDiscovery();
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Discovery not running.  Won't cancel it");
        }
    } catch (InterruptedException e) {
        if (isDebugEnabled()) Log.d(TAG, "DiscoveryCanceller sleep interrupted.");
    }
    return null;
}
 
Example 4
Source File: BluetoothCrashResolver.java    From android-beacon-library with Apache License 2.0 6 votes vote down vote up
private void cancelDiscovery() {
    try {
        Thread.sleep(TIME_TO_LET_DISCOVERY_RUN_MILLIS);
        if (!discoveryStartConfirmed) {
            LogManager.w(TAG, "BluetoothAdapter.ACTION_DISCOVERY_STARTED never received.  Recovery may fail.");
        }

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter.isDiscovering()) {
            LogManager.d(TAG, "Cancelling discovery");
            adapter.cancelDiscovery();
        }
        else {
            LogManager.d(TAG, "Discovery not running.  Won't cancel it");
        }
    } catch (InterruptedException e) {
        LogManager.d(TAG, "DiscoveryCanceller sleep interrupted.");
    }
}
 
Example 5
Source File: BluetoothCrashResolver.java    From android-sdk with MIT License 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {
    try {
        Thread.sleep(TIME_TO_LET_DISCOVERY_RUN_MILLIS);
        if (!discoveryStartConfirmed) {
            Logger.log.verbose("BluetoothAdapter.ACTION_DISCOVERY_STARTED never received.  Recovery may fail.");
        }

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter.isDiscovering()) {
            if (isDebugEnabled()) Log.d(TAG, "Cancelling discovery");
            adapter.cancelDiscovery();
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Discovery not running.  Won't cancel it");
        }
    } catch (InterruptedException e) {
        if (isDebugEnabled()) Log.d(TAG, "DiscoveryCanceller sleep interrupted.");
    }
    return null;
}
 
Example 6
Source File: SocketManager.java    From BluetoothHidEmu with Apache License 2.0 6 votes vote down vote up
/**
 * Init sockets and threads
 * 
 * @param adapter
 * @param device
 */
public void startSockets(BluetoothAdapter adapter, BluetoothDeviceView deviceView) {
    
    BluetoothDevice device = deviceView.getBluetoothDevice();
    
    if (device == null) {
        DoLog.w(TAG, "no hosts not found");
        return;
    } else {
        DoLog.d(TAG, "host selected: " + device);
    }

    // discovery is a heavy process. Apps must always cancel it when connecting.
    adapter.cancelDiscovery();
    
    mCtrlThread = initThread(mCtrlThread, "ctrl", device, 0x11);
    mIntrThread = initThread(mIntrThread, "intr", device, 0x13);
    
    mCtrlThread.start();
    mIntrThread.start();
}
 
Example 7
Source File: BluetoothDevicePreference.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
private void stopDiscovery() {
    final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
        if (adapter.isDiscovering()) {
            adapter.cancelDiscovery();
        }
    }
}
 
Example 8
Source File: InsightPairingActivity.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
private void stopBLScan() {
    if (scanning) {
        unregisterReceiver(broadcastReceiver);
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter != null) {
            bluetoothAdapter.cancelDiscovery();
        }
        scanning = false;
    }
}
 
Example 9
Source File: BtCommService.java    From AndrOBD with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor. Prepares a new Bluetooth Communication session.
 *
 * @param context The UI Activity Context
 * @param handler A Handler to send messages back to the UI Activity
 */
BtCommService(Context context, Handler handler)
{
	super(context, handler);

	// Always cancel discovery because it will slow down a connection
	// Member fields
	BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
	mAdapter.cancelDiscovery();
	
	// set up protocol handlers
	elm.addTelegramWriter(ser);
	ser.setMessageHandler(elm);
}
 
Example 10
Source File: BluetoothThread.java    From android-arduino-bluetooth with MIT License 5 votes vote down vote up
/**
 * Connect to a remote Bluetooth socket, or throw an exception if it fails.
 */
private void connect() throws Exception {

    Log.i(TAG, "Attempting connection to " + address + "...");

    // Get this device's Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if ((adapter == null) || (!adapter.isEnabled())){
        throw new Exception("Bluetooth adapter not found or not enabled!");
    }

    // Find the remote device
    BluetoothDevice remoteDevice = adapter.getRemoteDevice(address);

    // Create a socket with the remote device using this protocol
    socket = remoteDevice.createRfcommSocketToServiceRecord(uuid);

    // Make sure Bluetooth adapter is not in discovery mode
    adapter.cancelDiscovery();

    // Connect to the socket
    socket.connect();


    // Get input and output streams from the socket
    outStream = socket.getOutputStream();
    inStream = socket.getInputStream();

    Log.i(TAG, "Connected successfully to " + address + ".");
}
 
Example 11
Source File: BluetoothScanner.java    From Rumble with GNU General Public License v3.0 4 votes vote down vote up
public void stopScanner() {
    try {
        lock.lock();
        if (scanningState.equals(ScanningState.SCANNING_OFF))
            return;

        BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
        if (mBluetoothAdapter != null) {
            if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.isDiscovering())
                mBluetoothAdapter.cancelDiscovery();
        }

        switch (scanningState) {
            case SCANNING_IDLE:
                break;
            case SCANNING_SCHEDULED:
                handler.removeCallbacks(scanScheduleFires);
                break;
            case SCANNING_IN_PROGRESS:
                handler.removeCallbacks(scanTimeoutFires);
                EventBus.getDefault().post(new BluetoothScanEnded());
                break;
        }
        scanningState = ScanningState.SCANNING_OFF;

        Log.d(TAG, "--- Bluetooth Scanner stopped ---");

        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);

        if (registered) {
            RumbleApplication.getContext().unregisterReceiver(mReceiver);
            registered = false;
        }

        btNeighborhood.clear();
        resetTrickleTimer();
        /*
        if((mAccelerometer != null) && sensorregistered) {
            mSensorManager.unregisterListener(this);
            sensorregistered = false;
        }
        */
    } finally {
        lock.unlock();
    }
}
 
Example 12
Source File: BluetoothScanner.java    From Rumble with GNU General Public License v3.0 4 votes vote down vote up
public void onEvent(ChannelConnected event) {
    if (!event.neighbour.getLinkLayerIdentifier().equals(BluetoothLinkLayerAdapter.LinkLayerIdentifier))
        return;
    try {
        lock.lock();
        openedSocket++;

        if(openedSocket == 1) {
            Log.d(TAG, "[+] entering slow scan mode ");
            betamode = true;

            switch (scanningState) {
                case SCANNING_OFF:
                    return;
                case SCANNING_IDLE:
                    /*
                     * most probably we are in between a call to performScan(false)
                     * out from scanScheduleFires(). or for some reason the scanner stopped scanning
                     */
                    break;
                case SCANNING_IN_PROGRESS:
                    Log.d(TAG, "[-] cancelling current scan");
                    handler.removeCallbacks(scanTimeoutFires);
                    BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
                    if (mBluetoothAdapter.isDiscovering())
                        mBluetoothAdapter.cancelDiscovery();
                    EventBus.getDefault().post(new BluetoothScanEnded());
                    break;
                case SCANNING_SCHEDULED:
                    Log.d(TAG, "[-] cancelling previous scan scheduling");
                    handler.removeCallbacks(scanScheduleFires);
                    break;
            }

            handler.postDelayed(scanScheduleFires, (long) BETA_TRICKLE_TIMER);
            Log.d(TAG, "[->] next scan in: "+BETA_TRICKLE_TIMER/1000L+" seconds");
            scanningState = ScanningState.SCANNING_SCHEDULED;
        }
    } finally {
        lock.unlock();
    }
}