Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_CONNECTED

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_CONNECTED . 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: SdlBroadcastReceiver.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
@SuppressWarnings({"MissingPermission"})
private static boolean isBluetoothConnected() {
	BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	if(bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			int  a2dpState  = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
			int headSetState  = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);

			return ((a2dpState == BluetoothAdapter.STATE_CONNECTED || a2dpState == BluetoothAdapter.STATE_CONNECTING)
					&& (headSetState == BluetoothAdapter.STATE_CONNECTED || headSetState == BluetoothAdapter.STATE_CONNECTING));
		}else{
			return true;
		}
	}
	return false;
}
 
Example 2
Source File: ConnectionMonitorImpl.java    From neatle with MIT License 6 votes vote down vote up
@Override
public void onConnectionStateChanged(Connection connection, int newState) {
    if (connectionStateListener != null) {
        connectionStateListener.onConnectionStateChanged(connection, newState);
    }

    if (newState == BluetoothAdapter.STATE_DISCONNECTED) {
        NeatleLogger.d("Will try to reconnect to " + connection.getDevice().getAddress() + " after " + (reconnectTimeout / 1000) + " seconds");

        handler.removeCallbacks(reconnectRunnable);
        handler.postDelayed(reconnectRunnable, reconnectTimeout);
        reconnectTimeout = Math.min(reconnectTimeout * 2, MAX_RECONNECT_TIMEOUT);
    } else if (newState == BluetoothAdapter.STATE_CONNECTED) {
        reconnectTimeout = DEFAULT_RECONNECT_TIMEOUT;
    }
}
 
Example 3
Source File: SystemIconController.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private void updateBtIconVisibility() {
    if (mSbService == null || mBtMode == null) return;

    try {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            boolean enabled = btAdapter.getState() == BluetoothAdapter.STATE_ON;
            boolean connected = (Integer) XposedHelpers.callMethod(btAdapter, "getConnectionState") ==
                    BluetoothAdapter.STATE_CONNECTED;
            boolean visible;
            switch (mBtMode) {
                default:
                case DEFAULT: visible = enabled; break;
                case CONNECTED: visible = connected; break;
                case HIDDEN: visible = false; break;
            }
            if (DEBUG) log("updateBtIconVisibility: enabled=" + enabled + "; connected=" + connected +
                    "; visible=" + visible);
            XposedHelpers.callMethod(mSbService, "setIconVisibility", "bluetooth", visible);
        }
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 4
Source File: MediaStreamingStatus.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressLint("MissingPermission")
boolean isBluetoothActuallyAvailable(){
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if(adapter == null || !adapter.isEnabled() ){
        //False positive
        return false;
    }
    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ){
        int state = adapter.getProfileConnectionState(BluetoothProfile.A2DP);
        if(state != BluetoothAdapter.STATE_CONNECTING && state != BluetoothAdapter.STATE_CONNECTED){
            //False positive
            return false;
        }
    }

    return true;
}
 
Example 5
Source File: BluetoothBroadcastReceiver.java    From bluetooth-a2dp with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive (Context context, Intent intent) {
    if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
        Log.v(TAG, "Received irrelevant broadcast. Disregarding.");
        return;
    }

    //This is a State Change event, get the state extra, falling back to ERROR
    //if it isn't there (which shouldn't happen)
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

    switch (state) {
        case BluetoothAdapter.STATE_CONNECTED:
            safeUnregisterReceiver(context, this);
            fireOnBluetoothConnected();
            break;
        case BluetoothAdapter.ERROR:
            safeUnregisterReceiver(context, this);
            fireOnBluetoothError();
            break;
    }
}
 
Example 6
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 7
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 8
Source File: BluetoothManager.java    From tap-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    /*
     * status -
     *
     * 8  (0x08) - connection timeout / device went out of range
     * 19 (0x13) - connection terminated by peer user
     * 22 (0x16) - connection terminated by local host
     *
     * For more information, please refer to:
     * https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/refs/tags/android-cts-5.1_r28/stack/include/gatt_api.h
     */

    BluetoothDevice device = gatt.getDevice();
    log("Connection state changed - " + device.getName() + " " + device.getAddress() + " status: " + status + " newState: " + newState);

    if (ignoredDevices.contains(device.getAddress())) {
        return;
    }

    if (status == 22) {
        // will get here after calling 'removeBond'
        handleDeviceUnpaired(gatt);
        return;
    }

    switch (newState) {
        case BluetoothAdapter.STATE_CONNECTED:
            handleDeviceConnection(gatt);
            break;
        case BluetoothAdapter.STATE_DISCONNECTED:
            handleDeviceDisconnection(gatt);
            break;
    }
}
 
Example 9
Source File: AssistantService.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "blueConnectStateBroadcastReceiver action>>>>" + intent.getAction());
    if (intent.getAction() == null)
        return;
    if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
        int blueconState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
        switch (blueconState) {
            case BluetoothAdapter.STATE_CONNECTED:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTED");
                voiceMediator.setBlueHeadSet(true);
                isBlueToothHeadsetConnected();
                break;
            case BluetoothAdapter.STATE_CONNECTING:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTING");
                break;
            case BluetoothAdapter.STATE_DISCONNECTED:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTED");
                voiceMediator.setBlueHeadSet(false);
                voiceMediator.setSuportA2DP(false);
                AudioManager mAudioManager_ = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                mAudioManager_.setBluetoothScoOn(false);
                mAudioManager_.stopBluetoothSco();
                break;
            case BluetoothAdapter.STATE_DISCONNECTING:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTING");
                voiceMediator.setSuportA2DP(false);
                break;
            default:
                break;
        }
    }
}
 
Example 10
Source File: BluetoothUtils14.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isBTHeadsetConnected() {
    if(bluetoothAdapter != null) {
        return (bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothAdapter.STATE_CONNECTED);
    }
    return false;
}
 
Example 11
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 12
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 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: CharacteristicSubscriptionImpl.java    From neatle with MIT License 4 votes vote down vote up
@Override
public void onConnectionStateChanged(Connection connection, int newState) {
    if (newState == BluetoothAdapter.STATE_CONNECTED) {
        subscribeOnDevice();
    }
}