Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_TURNING_OFF

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_TURNING_OFF . 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: PairedDevicesFragment.java    From wearmouse with Apache License 2.0 6 votes vote down vote up
protected void updateBluetoothStateAndDevices() {
    switch (bluetoothAdapter.getState()) {
        case BluetoothAdapter.STATE_OFF:
            unregisterScanReceiver();
            clearBondedDevices();
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
        case BluetoothAdapter.STATE_TURNING_OFF:
            clearBondedDevices();
            startActivity(new Intent(getActivity(), BluetoothStateActivity.class));
            break;
        case BluetoothAdapter.STATE_ON:
            registerScanReceiver();
            updateBondedDevices();
            break;
        default: // fall out
    }
}
 
Example 2
Source File: BleMulticonnectProfileService.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
	final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
	final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);

	switch (state) {
		case BluetoothAdapter.STATE_ON:
			// On older phones (tested on Nexus 4 with Android 5.0.1) the Bluetooth requires some time
			// after it has been enabled before some operations can start. Starting the GATT server here
			// without a delay is very likely to cause a DeadObjectException from BluetoothManager#openGattServer(...).
			handler.postDelayed(() -> onBluetoothEnabled(), 600);
			break;
		case BluetoothAdapter.STATE_TURNING_OFF:
		case BluetoothAdapter.STATE_OFF:
			if (previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF)
				onBluetoothDisabled();
			break;
	}
}
 
Example 3
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
	final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
	final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);

	switch (state) {
		case BluetoothAdapter.STATE_TURNING_OFF:
		case BluetoothAdapter.STATE_OFF:
			if (connected && previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
				// The connection is killed by the system, no need to gently disconnect
				gattCallback.notifyDeviceDisconnected(bluetoothDevice);
			}
			close();
			break;
	}
}
 
Example 4
Source File: BluetoothAdapterSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@Rpc(description = "Enable bluetooth with a 30s timeout.")
public void btEnable() throws BluetoothAdapterSnippetException, InterruptedException {
    if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
        return;
    }
    // If bt is trying to turn off, wait for that to finish before continuing.
    if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_OFF) {
        if (!Utils.waitUntil(
                () -> mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF,
                TIMEOUT_TOGGLE_STATE)) {
            Log.e(String.format("BT failed to stabilize after %ss.", TIMEOUT_TOGGLE_STATE));
        }
    }
    if (!mBluetoothAdapter.enable()) {
        throw new BluetoothAdapterSnippetException("Failed to start enabling bluetooth");
    }
    if (!Utils.waitUntil(() -> mBluetoothAdapter.isEnabled(), TIMEOUT_TOGGLE_STATE)) {
        throw new BluetoothAdapterSnippetException(
                String.format("Bluetooth did not turn on within %ss.", TIMEOUT_TOGGLE_STATE));
    }
}
 
Example 5
Source File: ScannerRepository.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
    final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);

    switch (state) {
        case BluetoothAdapter.STATE_ON:
            mScannerStateLiveData.bluetoothEnabled();
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
        case BluetoothAdapter.STATE_OFF:
            if (previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
                stopScan();
                mScannerStateLiveData.bluetoothDisabled();
            }
            break;
    }
}
 
Example 6
Source File: MusicPlayer.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
                if (mp != null) {
                    if (mp.isPlaying()) {
                        pauseSound();
                    }
                }
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                break;
            case BluetoothAdapter.STATE_ON:
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                break;
        }
    }
}
 
Example 7
Source File: BleProfileService.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
    final ILogger logger = getBinder();

    final String stateString = "[Broadcast] Action received: " + BluetoothAdapter.ACTION_STATE_CHANGED + ", state changed to " + state2String(state);
    logger.log(Log.DEBUG, stateString);

    switch (state) {
        case BluetoothAdapter.STATE_ON:
            onBluetoothEnabled();
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
        case BluetoothAdapter.STATE_OFF:
            onBluetoothDisabled();
            break;
    }
}
 
Example 8
Source File: BluetoothStateReceiver.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

        switch (state) {
            case BluetoothAdapter.ERROR:
            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.STATE_TURNING_OFF:
                notifyState(false);
                break;
            case BluetoothAdapter.STATE_ON:
                notifyState(true);
                break;
        }
    }
}
 
Example 9
Source File: AppRTCBluetoothManager.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
  switch (state) {
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "DISCONNECTED";
    case BluetoothAdapter.STATE_CONNECTED:
      return "CONNECTED";
    case BluetoothAdapter.STATE_CONNECTING:
      return "CONNECTING";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "DISCONNECTING";
    case BluetoothAdapter.STATE_OFF:
      return "OFF";
    case BluetoothAdapter.STATE_ON:
      return "ON";
    case BluetoothAdapter.STATE_TURNING_OFF:
      // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
      // attempt graceful disconnection of any remote links.
      return "TURNING_OFF";
    case BluetoothAdapter.STATE_TURNING_ON:
      // Indicates the local Bluetooth adapter is turning on. However local clients should wait
      // for STATE_ON before attempting to use the adapter.
      return  "TURNING_ON";
    default:
      return "INVALID";
  }
}
 
Example 10
Source File: BluetoothStateActivity.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
private void checkState(int state) {
    if (state != BluetoothAdapter.STATE_TURNING_ON
            && state != BluetoothAdapter.STATE_TURNING_OFF) {
        finish();
    } else {
        ((TextView) findViewById(R.id.title))
                .setText(
                        getString(
                                state == BluetoothAdapter.STATE_TURNING_ON
                                        ? R.string.pref_bluetooth_turningOn
                                        : R.string.pref_bluetooth_turningOff));
    }
}
 
Example 11
Source File: AppRTCBluetoothManager.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts BluetoothAdapter states into local string representations.
 */
private String stateToString(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_DISCONNECTED:
            return "DISCONNECTED";
        case BluetoothAdapter.STATE_CONNECTED:
            return "CONNECTED";
        case BluetoothAdapter.STATE_CONNECTING:
            return "CONNECTING";
        case BluetoothAdapter.STATE_DISCONNECTING:
            return "DISCONNECTING";
        case BluetoothAdapter.STATE_OFF:
            return "OFF";
        case BluetoothAdapter.STATE_ON:
            return "ON";
        case BluetoothAdapter.STATE_TURNING_OFF:
            // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
            // attempt graceful disconnection of any remote links.
            return "TURNING_OFF";
        case BluetoothAdapter.STATE_TURNING_ON:
            // Indicates the local Bluetooth adapter is turning on. However local clients should wait
            // for STATE_ON before attempting to use the adapter.
            return "TURNING_ON";
        default:
            return "INVALID";
    }
}
 
Example 12
Source File: RxBleAdapterStateObservable.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
static BleAdapterState mapToBleAdapterState(int state) {

        switch (state) {
            case BluetoothAdapter.STATE_ON:
                return BleAdapterState.STATE_ON;
            case BluetoothAdapter.STATE_TURNING_ON:
                return BleAdapterState.STATE_TURNING_ON;
            case BluetoothAdapter.STATE_TURNING_OFF:
                return BleAdapterState.STATE_TURNING_OFF;
            case BluetoothAdapter.STATE_OFF:
            default:
                return BleAdapterState.STATE_OFF;
        }
    }
 
Example 13
Source File: AppRTCBluetoothManager.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts BluetoothAdapter states into local string representations.
 */
private String stateToString(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_DISCONNECTED:
            return "DISCONNECTED";
        case BluetoothAdapter.STATE_CONNECTED:
            return "CONNECTED";
        case BluetoothAdapter.STATE_CONNECTING:
            return "CONNECTING";
        case BluetoothAdapter.STATE_DISCONNECTING:
            return "DISCONNECTING";
        case BluetoothAdapter.STATE_OFF:
            return "OFF";
        case BluetoothAdapter.STATE_ON:
            return "ON";
        case BluetoothAdapter.STATE_TURNING_OFF:
            // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
            // attempt graceful disconnection of any remote links.
            return "TURNING_OFF";
        case BluetoothAdapter.STATE_TURNING_ON:
            // Indicates the local Bluetooth adapter is turning on. However local clients should wait
            // for STATE_ON before attempting to use the adapter.
            return "TURNING_ON";
        default:
            return "INVALID";
    }
}
 
Example 14
Source File: BlueToothService.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
void notifyBluetoothStateChange(final int newState) {
    if (newState == BluetoothAdapter.STATE_TURNING_OFF) {
        stopScanning();
        stopConnectedDevice();
    }

    handler.post(new Runnable() {
        @Override
        public void run() {
            if (prevBluetoothState != newState) {
                if (newState == BluetoothAdapter.STATE_OFF) {
                    if (discoveryStarted) {
                        discoveryStarted = false;
                        listeners.onScanEnded();
                    }

                    synchronized (discoveredDevices) {
                        discoveredDevices.clear();
                        interestingDevices.clear();
                    }

                    setKnownDevice(null);
                    listeners.onDeviceReady(null, false);
                    listeners.onStateChanged(newState);
                } else if (newState == BluetoothAdapter.STATE_ON) {
                    listeners.onStateChanged(newState);
                    isBTAdapterAlreadyBeingEnabled = false;
                } else {
                    listeners.onStateChanged(newState);
                }

                prevBluetoothState = newState;
            }
        }
    });
}
 
Example 15
Source File: BleProfileService.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String state2String(final int state) {
    switch (state) {
        case BluetoothAdapter.STATE_TURNING_ON:
            return "TURNING ON";
        case BluetoothAdapter.STATE_ON:
            return "ON";
        case BluetoothAdapter.STATE_TURNING_OFF:
            return "TURNING OFF";
        case BluetoothAdapter.STATE_OFF:
            return "OFF";
        default:
            return "UNKNOWN (" + state + ")";
    }
}
 
Example 16
Source File: AppRTCBluetoothManager.java    From imsdk-android with MIT License 5 votes vote down vote up
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
  switch (state) {
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "DISCONNECTED";
    case BluetoothAdapter.STATE_CONNECTED:
      return "CONNECTED";
    case BluetoothAdapter.STATE_CONNECTING:
      return "CONNECTING";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "DISCONNECTING";
    case BluetoothAdapter.STATE_OFF:
      return "OFF";
    case BluetoothAdapter.STATE_ON:
      return "ON";
    case BluetoothAdapter.STATE_TURNING_OFF:
      // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
      // attempt graceful disconnection of any remote links.
      return "TURNING_OFF";
    case BluetoothAdapter.STATE_TURNING_ON:
      // Indicates the local Bluetooth adapter is turning on. However local clients should wait
      // for STATE_ON before attempting to use the adapter.
      return  "TURNING_ON";
    default:
      return "INVALID";
  }
}
 
Example 17
Source File: P_BluetoothCrashResolver.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
        if (recoveryInProgress) {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery finished");
            finishRecovery();
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery finished (external)");
        }
    }
    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
        if (recoveryInProgress) {
            discoveryStartConfirmed = true;
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery started");
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery started (external)");
        }
    }

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.ERROR:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is ERROR");
                break;
            case BluetoothAdapter.STATE_OFF:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is OFF");
                lastBluetoothOffTime = new Date().getTime();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                break;
            case BluetoothAdapter.STATE_ON:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is ON");
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth was turned off for "+(lastBluetoothTurningOnTime - lastBluetoothOffTime)+" milliseconds");
                if (lastBluetoothTurningOnTime - lastBluetoothOffTime < SUSPICIOUSLY_SHORT_BLUETOOTH_OFF_INTERVAL_MILLIS) {
                    crashDetected();
                }
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                lastBluetoothTurningOnTime = new Date().getTime();
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is TURNING_ON");
                break;
        }
    }
}
 
Example 18
Source File: AppLinkReceiver.java    From AndroidDemoProjects with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	if( intent == null || intent.getAction() == null || context == null )
		return;

	BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
	String action = intent.getAction();

	Intent serviceIntent = new Intent( context, AppLinkService.class );
	serviceIntent.putExtras( intent );


	//Should start service
	if( action.compareTo(BluetoothDevice.ACTION_ACL_CONNECTED) == 0 &&
			device != null &&
			device.getName() != null &&
			device.getName().contains( context.getString( R.string.device_name ) ) &&
			AppLinkService.getInstance() == null )
	{
		context.startService(serviceIntent);
	}

	else if( action.equals( Intent.ACTION_BOOT_COMPLETED ) &&
			BluetoothAdapter.getDefaultAdapter() != null &&
			BluetoothAdapter.getDefaultAdapter().isEnabled() ) {
		context.startService(serviceIntent);

	}

	//Should stop service
	else if( action.equals( BluetoothDevice.ACTION_ACL_DISCONNECTED ) &&
			device != null &&
			device.getName() != null &&
			device.getName().contains( context.getString( R.string.device_name ) ) &&
			AppLinkService.getInstance() != null )
	{
		context.stopService( intent );
	}

	else if( action.equals(BluetoothAdapter.ACTION_STATE_CHANGED ) &&
		intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_TURNING_OFF &&
		AppLinkService.getInstance() != null )
	{
		context.stopService( serviceIntent );
	}

	else if( action.equals( AudioManager.ACTION_AUDIO_BECOMING_NOISY ) ) {
		context.stopService( serviceIntent );
	}
}
 
Example 19
Source File: BluetoothCrashResolver.java    From android-beacon-library with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
        if (recoveryInProgress) {
            LogManager.d(TAG, "Bluetooth discovery finished");
            finishRecovery();
        }
        else {
            LogManager.d(TAG, "Bluetooth discovery finished (external)");
        }
    }
    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
        if (recoveryInProgress) {
            discoveryStartConfirmed = true;
            LogManager.d(TAG, "Bluetooth discovery started");
        }
        else {
            LogManager.d(TAG, "Bluetooth discovery started (external)");
        }
    }

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.ERROR:
                LogManager.d(TAG, "Bluetooth state is ERROR");
                break;
            case BluetoothAdapter.STATE_OFF:
                LogManager.d(TAG, "Bluetooth state is OFF");
                lastBluetoothOffTime = SystemClock.elapsedRealtime();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                break;
            case BluetoothAdapter.STATE_ON:
                LogManager.d(TAG, "Bluetooth state is ON");
                LogManager.d(TAG, "Bluetooth was turned off for %s milliseconds", lastBluetoothTurningOnTime - lastBluetoothOffTime);
                if (lastBluetoothTurningOnTime - lastBluetoothOffTime < SUSPICIOUSLY_SHORT_BLUETOOTH_OFF_INTERVAL_MILLIS) {
                    crashDetected();
                }
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                lastBluetoothTurningOnTime = SystemClock.elapsedRealtime();
                LogManager.d(TAG, "Bluetooth state is TURNING_ON");
                break;
        }
    }
}
 
Example 20
Source File: BluetoothRadioStatusListener.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
private void performActionIfNecessaryAndUpdateState(int state) {
    // if a dev wants to know about flapping listen to turning on / off
    if (state == BluetoothAdapter.STATE_TURNING_ON || state == BluetoothAdapter.STATE_TURNING_OFF) {
        Timber.v("Turning off or turning on, passing through with no delay");
        mainHandler.post(() -> {
            if (listener != null) {
                switch (state) {
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        listener.bluetoothTurningOff();
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        listener.bluetoothTurningOn();
                        break;
                    default:
                        Timber.w("The BT radio went into a state that we do not handle");
                }
            }
        });
    } else {
        boolean shouldCallback = shouldScheduleCallback(currentState, state);
        currentState = state;
        if (shouldCallback) {
            // if we got that we should callback, then we should cancel any existing pending
            // callback before starting the new one
            mainHandler.removeCallbacksAndMessages(null);
            Timber.v("Clearing old messages");
            Timber.v("BT on or off, sending after %dms", (state == BluetoothAdapter.STATE_OFF) ? MIN_TURNING_OFF_CALLBACK_DELAY : MIN_TURNING_ON_CALLBACK_DELAY);
            mainHandler.postDelayed(() -> {
                if (listener != null) {
                    switch (state) {
                        case BluetoothAdapter.STATE_OFF:
                            Timber.v("Notifying off");
                            listener.bluetoothOff();
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Timber.v("Notifying on");
                            listener.bluetoothOn();
                            break;
                        default:
                            Timber.w("The BT radio went into a state that we do not handle");
                    }
                }
            }, ((state == BluetoothAdapter.STATE_OFF) ? MIN_TURNING_OFF_CALLBACK_DELAY : MIN_TURNING_ON_CALLBACK_DELAY));
        } else {
            Timber.d("Not calling back, flapping");
        }
    }
}