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

The following examples show how to use android.bluetooth.BluetoothAdapter#isDiscovering() . These examples are extracted from open source projects. 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
public boolean isScanningDisabled ()
{
    WifiManager wifiManager = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(MainActivity.WIFI_SERVICE);
    if (wifiManager.isScanAlwaysAvailable()) return false;
    //
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter.isDiscovering()) return false;
    //
    return true;
}
 
Example 2
@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 3
@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
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 Project: android-sdk   File: BluetoothCrashResolver.java    License: 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
private void stopDiscovery() {
    final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
        if (adapter.isDiscovering()) {
            adapter.cancelDiscovery();
        }
    }
}
 
Example 7
@TargetApi(17)
private void startRecovery() {
    // The discovery operation will start by clearing out the bluetooth mac list to only the 256
    // most recently seen BLE mac addresses.
    recoveryAttemptCount++;
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (isDebugEnabled()) Log.d(TAG, "about to check if discovery is active");
    if (!adapter.isDiscovering()) {
        Log.w(TAG, "Recovery attempt started");
        recoveryInProgress = true;
        discoveryStartConfirmed = false;
        if (isDebugEnabled()) Log.d(TAG, "about to command discovery");
        if (!adapter.startDiscovery()) {
            Log.w(TAG, "Can't start discovery.  Is bluetooth turned on?");
        }
        if (isDebugEnabled()) Log.d(TAG, "startDiscovery commanded.  isDiscovering()="+adapter.isDiscovering());
        // We don't actually need to do a discovery -- we just need to kick one off so the
        // mac list will be pared back to 256.  Because discovery is an expensive operation in
        // terms of battery, we will cancel it.
        if (TIME_TO_LET_DISCOVERY_RUN_MILLIS > 0 ) {
            if (isDebugEnabled()) Log.d(TAG, "We will be cancelling this discovery in "+TIME_TO_LET_DISCOVERY_RUN_MILLIS+" milliseconds.");
            discoveryCanceller.doInBackground();
        }
        else {
            Log.d(TAG, "We will let this discovery run its course.");
        }
    }
    else {
        Log.w(TAG, "Already discovering.  Recovery attempt abandoned.");
    }

}
 
Example 8
@TargetApi(17)
private void startRecovery() {
    // The discovery operation will start by clearing out the bluetooth mac list to only the 256
    // most recently seen BLE mac addresses.
    recoveryAttemptCount++;
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (isDebugEnabled()) Log.d(TAG, "about to check if discovery is active");
    if (!adapter.isDiscovering()) {
        Log.w(TAG, "Recovery attempt started");
        recoveryInProgress = true;
        discoveryStartConfirmed = false;
        if (isDebugEnabled()) Log.d(TAG, "about to command discovery");
        if (!adapter.startDiscovery()) {
            Log.w(TAG, "Can't start discovery.  Is bluetooth turned on?");
        }
        if (isDebugEnabled()) Log.d(TAG, "startDiscovery commanded.  isDiscovering()="+adapter.isDiscovering());
        // We don't actually need to do a discovery -- we just need to kick one off so the
        // mac list will be pared back to 256.  Because discovery is an expensive operation in
        // terms of battery, we will cancel it.
        if (TIME_TO_LET_DISCOVERY_RUN_MILLIS > 0 ) {
            if (isDebugEnabled()) Log.d(TAG, "We will be cancelling this discovery in "+TIME_TO_LET_DISCOVERY_RUN_MILLIS+" milliseconds.");
            discoveryCanceller.doInBackground();
        }
        else {
            Log.d(TAG, "We will let this discovery run its course.");
        }
    }
    else {
        Log.w(TAG, "Already discovering.  Recovery attempt abandoned.");
    }

}
 
Example 9
@TargetApi(17)
private void startRecovery() {
    // The discovery operation will start by clearing out the Bluetooth mac list to only the 256
    // most recently seen BLE mac addresses.
    recoveryAttemptCount++;
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    LogManager.d(TAG, "about to check if discovery is active");
    if (!adapter.isDiscovering()) {
        LogManager.w(TAG, "Recovery attempt started");
        recoveryInProgress = true;
        discoveryStartConfirmed = false;
        LogManager.d(TAG, "about to command discovery");
        if (!adapter.startDiscovery()) {
            LogManager.w(TAG, "Can't start discovery.  Is Bluetooth turned on?");
        }
        LogManager.d(TAG, "startDiscovery commanded.  isDiscovering()=%s", adapter.isDiscovering());
        // We don't actually need to do a discovery -- we just need to kick one off so the
        // mac list will be pared back to 256.  Because discovery is an expensive operation in
        // terms of battery, we will cancel it.
        if (TIME_TO_LET_DISCOVERY_RUN_MILLIS > 0 ) {
            LogManager.d(TAG, "We will be cancelling this discovery in %s milliseconds.", TIME_TO_LET_DISCOVERY_RUN_MILLIS);
            cancelDiscovery();
        }
        else {
            LogManager.d(TAG, "We will let this discovery run its course.");
        }
    }
    else {
        LogManager.w(TAG, "Already discovering.  Recovery attempt abandoned.");
    }

}
 
Example 10
Source Project: android-sdk   File: BluetoothCrashResolver.java    License: MIT License 5 votes vote down vote up
@TargetApi(17)
private void startRecovery() {
    // The discovery operation will start by clearing out the bluetooth mac list to only the 256
    // most recently seen BLE mac addresses.
    recoveryAttemptCount++;
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (isDebugEnabled()) Log.d(TAG, "about to check if discovery is active");
    if (!adapter.isDiscovering()) {
        Logger.log.verbose("Recovery attempt started");
        recoveryInProgress = true;
        discoveryStartConfirmed = false;
        if (isDebugEnabled()) Log.d(TAG, "about to command discovery");
        if (!adapter.startDiscovery()) {
            Logger.log.verbose("Can't start discovery.  Is bluetooth turned on?");
        }
        if (isDebugEnabled()) Log.d(TAG, "startDiscovery commanded.  isDiscovering()="+adapter.isDiscovering());
        // We don't actually need to do a discovery -- we just need to kick one off so the
        // mac list will be pared back to 256.  Because discovery is an expensive operation in
        // terms of battery, we will cancel it.
        if (TIME_TO_LET_DISCOVERY_RUN_MILLIS > 0 ) {
            if (isDebugEnabled()) Log.d(TAG, "We will be cancelling this discovery in "+TIME_TO_LET_DISCOVERY_RUN_MILLIS+" milliseconds.");
            discoveryCanceller.doInBackground();
        }
        else {
            Logger.log.verbose("We will let this discovery run its course.");
        }
    }
    else {
        Logger.log.verbose("Already discovering.Recovery attempt abandoned.");
    }

}
 
Example 11
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
private void performScan(boolean force) {
    try {
        lock.lock();

        switch (scanningState) {
            case SCANNING_OFF:
                return;
            case SCANNING_IN_PROGRESS:
                return;
            case SCANNING_SCHEDULED:
                if(!force)
                    return;
                else {
                    handler.removeCallbacks(scanScheduleFires);
                    scanningState = ScanningState.SCANNING_IDLE;
                }
            case SCANNING_IDLE:
                break;
        }

        /*
        if((mAccelerometer != null) && !sensorregistered) {
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            sensorregistered = true;
        }
        */
        BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());

        if (mBluetoothAdapter == null) {
            Log.d(TAG, "Bluetooth is not supported on this platform");
            return;
        }

        if( mBluetoothAdapter.isEnabled() ){
            btNeighborhood.clear();

            /*
             * it is possible that the device is already discovering if another app
             * ran a discovery procedure
             */
            if (!mBluetoothAdapter.isDiscovering())
                mBluetoothAdapter.startDiscovery();
            scanningState = ScanningState.SCANNING_IN_PROGRESS;
            EventBus.getDefault().post(new BluetoothScanStarted());

            /*
             * we set a timeout in case the scanning discovery procedure doesn't stop by itself
             * (yes. it does happen too.)
             */
            handler.postDelayed(scanTimeoutFires, (long)SCANNING_TIMEOUT);
        }
    }
    catch (Exception e) {
        Log.d(TAG, "Exception:"+e.toString());
    } finally {
        lock.unlock();
    }
}
 
Example 13
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();
    }
}