Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_TURNING_ON

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_TURNING_ON . 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: MainMenuActivity.java    From EFRConnect-android with Apache License 2.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:
            case BluetoothAdapter.STATE_TURNING_OFF:
                isBluetoothAdapterEnabled = false;
                showEnableBluetoothAdapterBar();
                break;
            case BluetoothAdapter.STATE_ON:
                if (!isBluetoothAdapterEnabled) {
                    Toast.makeText(MainMenuActivity.this, R.string.toast_bluetooth_enabled, Toast.LENGTH_SHORT).show();
                }
                isBluetoothAdapterEnabled = true;
                bluetoothEnableBar.setVisibility(View.GONE);
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                isBluetoothAdapterEnabled = false;
                break;
        }
    }
}
 
Example 2
Source File: P_Task_TurnBleOn.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
@Override public void execute()
{
	if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_ON )
	{
		redundant();
	}
	else if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_TURNING_ON )
	{
		// DRK > Nothing to do, already turning on.
	}
	else
	{
		if( m_implicit )
		{
			fail();
		}
		else if( false == getManager().managerLayer().enable() )
		{
			fail();
		}
		else
		{
			// SUCCESS, so far...
		}
	}
}
 
Example 3
Source File: P_Task_TurnBleOn.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override public void execute()
{
	if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_ON )
	{
		redundant();
	}
	else if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_TURNING_ON )
	{
		// DRK > Nothing to do, already turning on.
	}
	else
	{
		if( m_implicit )
		{
			fail();
		}
		else if( false == getManager().managerLayer().enable() )
		{
			fail();
		}
		else
		{
			// SUCCESS, so far...
		}
	}
}
 
Example 4
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // This will be executed only once
    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);

    switch (state) {
        case BluetoothAdapter.STATE_TURNING_ON:
        case BluetoothAdapter.STATE_ON:
            updateBlePermissionStatus(true);
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
        case BluetoothAdapter.STATE_OFF:
            updateBlePermissionStatus(false);
            break;
    }
}
 
Example 5
Source File: BleUtils.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
private void onBleAdapterStatusChanged(int state, Context context) {
    switch (state) {
        case BluetoothAdapter.STATE_OFF: {
            // Turn off has finished. Turn it on again
            Log.d(TAG, "Ble adapter turned off. Turning on");
            BluetoothAdapter bleAdapter = BleUtils.getBluetoothAdapter(context);
            if (bleAdapter != null) {
                bleAdapter.enable();
            }
            break;
        }
        case BluetoothAdapter.STATE_TURNING_OFF:
            break;
        case BluetoothAdapter.STATE_ON: {
            Log.d(TAG, "Ble adapter turned on. Reset completed");
            // Turn on has finished.
            resetCompleted(context);
            break;
        }
        case BluetoothAdapter.STATE_TURNING_ON:
            break;
    }
}
 
Example 6
Source File: BluetoothLinkLayerAdapter.java    From Rumble with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        Log.d(TAG, "[!] BT State Changed");
        switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)){
            case BluetoothAdapter.STATE_ON:
                linkStarted();
                break;
            case BluetoothAdapter.STATE_OFF:
                linkStopped();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                break;
        }
    }

    if(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)){
    }
}
 
Example 7
Source File: RileyLinkBluetoothStateReceiver.java    From AndroidAPS 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();

    PumpInterface activePump = ConfigBuilderPlugin.getPlugin().getActivePump();

    if (action != null && activePump != null) {

        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                break;

            case BluetoothAdapter.STATE_ON: {
                    LOG.debug("RileyLinkBluetoothStateReceiver: Bluetooth back on. Sending broadcast to RileyLink Framework");
                    RileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothReconnected);
            }
            break;
        }
    }
}
 
Example 8
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 9
Source File: AppRTCBluetoothManager.java    From flutter-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: BrowserActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    BluetoothAdapter defaultBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
                finish();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                isBluetoothAdapterEnabled = false;
                break;
            case BluetoothAdapter.STATE_ON:
                if (defaultBluetoothAdapter != null &&
                        defaultBluetoothAdapter.isEnabled()) {
                    if (!isBluetoothAdapterEnabled) {
                        toast = Toast.makeText(BrowserActivity.this,
                                R.string.toast_bluetooth_enabled,
                                Toast.LENGTH_SHORT);
                        toast.show();
                    }

                    updateListWhenAdapterIsReady = false;
                    bluetoothEnableBar.setVisibility(View.GONE);

                    discovery.disconnect();
                    discovery.connect(BrowserActivity.this);
                    startScanning();
                }
                isBluetoothAdapterEnabled = true;
                break;
        }
    }
}
 
Example 11
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 12
Source File: BleManagerHandler.java    From Android-BLE-Library 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 13
Source File: NetworkController.java    From DebugDrawer with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Bluetooth state representation to an Enum
 *
 * @param state
 */
private BluetoothState getBluetoothState(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_ON:
            return BluetoothState.ON;
        case BluetoothAdapter.STATE_OFF:
            return BluetoothState.OFF;
        case BluetoothAdapter.STATE_TURNING_ON:
            return BluetoothState.TURNING_ON;
        case BluetoothAdapter.STATE_TURNING_OFF:
            return BluetoothState.TURNING_OFF;
    }

    return BluetoothState.UNKNOWN;
}
 
Example 14
Source File: SdlRouterService.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
	@SuppressWarnings("MissingPermission")
	public void onReceive(Context context, Intent intent) 
	{
		String action = intent.getAction();
    	if(action == null){
			Log.d(TAG, "Disconnect received with no action.");
		}else {
			Log.d(TAG, "Disconnect received. Action: " + intent.getAction());

			if(action.equalsIgnoreCase(BluetoothAdapter.ACTION_STATE_CHANGED)){
				int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
				switch (bluetoothState) {
					case BluetoothAdapter.STATE_TURNING_ON:
					case BluetoothAdapter.STATE_ON:
						//There is nothing to do in the case the adapter is turning on or just switched to on
						return;
					case BluetoothAdapter.STATE_TURNING_OFF:
					case BluetoothAdapter.STATE_OFF:
						Log.d(TAG, "Bluetooth is shutting off, SDL Router Service is closing.");
						connectAsClient = false;
						if(!shouldServiceRemainOpen(intent)){
							closeSelf();
						}
						return;
					default:
						break;
				}
			}
			//Otherwise
			connectAsClient = false;
			if (legacyModeEnabled) {
				Log.d(TAG, "Legacy mode enabled and bluetooth d/c'ed, restarting router service bluetooth.");
				enableLegacyMode(false);
				onTransportDisconnected(new TransportRecord(TransportType.BLUETOOTH,null));
				initBluetoothSerialService();
			}
		}
}
 
Example 15
Source File: DeviceStatusUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 判断蓝牙是否打开
 * @return true:已经打开或者正在打开;false:已经关闭或者正在关闭
 *             没有找到蓝牙设备
 */
public static boolean isBluetoothOpen() throws Exception {
	int bluetoothStateCode = getBluetoothState();
	return bluetoothStateCode == BluetoothAdapter.STATE_ON
			|| bluetoothStateCode == BluetoothAdapter.STATE_TURNING_ON ? true
			: false;
}
 
Example 16
Source File: BluetoothCentral.java    From blessed-android with MIT License 5 votes vote down vote up
private void handleAdapterState(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_OFF:
            // Check if there are any connected peripherals or connections in progress
            if (connectedPeripherals.size() > 0 || unconnectedPeripherals.size() > 0) {
                // See if they are automatically disconnect
                expectingBluetoothOffDisconnects = true;
                startDisconnectionTimer();
            }
            Timber.d("bluetooth turned off");
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
            expectingBluetoothOffDisconnects = true;

            // Stop all scans so that we are back in a clean state
            // Note that we can't call stopScan if the adapter is off
            cancelTimeoutTimer();
            cancelAutoConnectTimer();
            currentCallback = null;
            currentFilters = null;
            autoConnectScanner = null;
            Timber.d("bluetooth turning off");
            break;
        case BluetoothAdapter.STATE_ON:
            expectingBluetoothOffDisconnects = false;
            Timber.d("bluetooth turned on");
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            expectingBluetoothOffDisconnects = false;
            Timber.d("bluetooth turning on");
            break;
    }
}
 
Example 17
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 18
Source File: BluetoothStateReceiver.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
private String getStateString(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_ON: return "state_on";
        case BluetoothAdapter.STATE_OFF: return "state_off";
        case BluetoothAdapter.STATE_TURNING_OFF: return "state_turning_off";
        case BluetoothAdapter.STATE_TURNING_ON: return "state_turning_on";
        default: return "unknown";
    }
}
 
Example 19
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 20
Source File: BluetoothManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void bluetoothStateChangeHandler(int prevState, int newState) {
    boolean isStandardBroadcast = true;
    if (prevState == newState) { // No change. Nothing to do.
        return;
    }
    // Notify all proxy objects first of adapter state change
    if (newState == BluetoothAdapter.STATE_BLE_ON || newState == BluetoothAdapter.STATE_OFF) {
        boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
                && newState == BluetoothAdapter.STATE_BLE_ON);

        if (newState == BluetoothAdapter.STATE_OFF) {
            // If Bluetooth is off, send service down event to proxy objects, and unbind
            if (DBG) {
                Slog.d(TAG, "Bluetooth is complete send Service Down");
            }
            sendBluetoothServiceDownCallback();
            unbindAndFinish();
            sendBleStateChanged(prevState, newState);
            // Don't broadcast as it has already been broadcast before
            isStandardBroadcast = false;

        } else if (!intermediate_off) {
            // connect to GattService
            if (DBG) {
                Slog.d(TAG, "Bluetooth is in LE only mode");
            }
            if (mBluetoothGatt != null || !mContext.getPackageManager()
                        .hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                continueFromBleOnState();
            } else {
                if (DBG) {
                    Slog.d(TAG, "Binding Bluetooth GATT service");
                }
                Intent i = new Intent(IBluetoothGatt.class.getName());
                doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
                        UserHandle.CURRENT);
            }
            sendBleStateChanged(prevState, newState);
            //Don't broadcase this as std intent
            isStandardBroadcast = false;

        } else if (intermediate_off) {
            if (DBG) {
                Slog.d(TAG, "Intermediate off, back to LE only mode");
            }
            // For LE only mode, broadcast as is
            sendBleStateChanged(prevState, newState);
            sendBluetoothStateCallback(false); // BT is OFF for general users
            // Broadcast as STATE_OFF
            newState = BluetoothAdapter.STATE_OFF;
            sendBrEdrDownCallback();
        }
    } else if (newState == BluetoothAdapter.STATE_ON) {
        boolean isUp = (newState == BluetoothAdapter.STATE_ON);
        sendBluetoothStateCallback(isUp);
        sendBleStateChanged(prevState, newState);

    } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON
            || newState == BluetoothAdapter.STATE_BLE_TURNING_OFF) {
        sendBleStateChanged(prevState, newState);
        isStandardBroadcast = false;

    } else if (newState == BluetoothAdapter.STATE_TURNING_ON
            || newState == BluetoothAdapter.STATE_TURNING_OFF) {
        sendBleStateChanged(prevState, newState);
    }

    if (isStandardBroadcast) {
        if (prevState == BluetoothAdapter.STATE_BLE_ON) {
            // Show prevState of BLE_ON as OFF to standard users
            prevState = BluetoothAdapter.STATE_OFF;
        }
        Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
    }
}