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

The following examples show how to use android.bluetooth.BluetoothAdapter#getProfileConnectionState() . 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: 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 3
Source File: Utils.java    From your-local-weather with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isBluetoothHeadsetEnabledConnected(Context context) {
    BluetoothAdapter bluetoothAdapter = Utils.getBluetoothAdapter(context);
    boolean isBtConnected = (bluetoothAdapter != null && (
            BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) ||
                    BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP)));
    if (!isBtConnected) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (!sharedPreferences.getStringSet(Constants.CONNECTED_BT_DEVICES, new HashSet<String>()).isEmpty()) {
            sharedPreferences.edit().putStringSet(Constants.CONNECTED_BT_DEVICES, new HashSet<String>()).apply();
        }
    }
    return isBtConnected;
}
 
Example 4
Source File: BluetoothUtil.java    From sealrtc-android with MIT License 5 votes vote down vote up
/**
 * 是否连接了蓝牙耳机
 *
 * @return
 */
@SuppressLint("WrongConstant")
public static boolean hasBluetoothA2dpConnected() {
    boolean bool = false;
    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mAdapter != null && mAdapter.isEnabled()) {
        int a2dp = mAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
        if (a2dp == BluetoothProfile.STATE_CONNECTED) {
            bool = true;
        }
    }
    return bool;
}
 
Example 5
Source File: AudioManagerAndroid.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the current Bluetooth headset state.
 * android.bluetooth.BluetoothAdapter.getProfileConnectionState() requires
 * the BLUETOOTH permission.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private boolean hasBluetoothHeadset() {
    if (!mHasBluetoothPermission) {
        Log.w(TAG, "hasBluetoothHeadset() requires BLUETOOTH permission");
        return false;
    }

    // To get a BluetoothAdapter representing the local Bluetooth adapter,
    // when running on JELLY_BEAN_MR1 (4.2) and below, call the static
    // getDefaultAdapter() method; when running on JELLY_BEAN_MR2 (4.3) and
    // higher, retrieve it through getSystemService(String) with
    // BLUETOOTH_SERVICE.
    BluetoothAdapter btAdapter = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // Use BluetoothManager to get the BluetoothAdapter for
        // Android 4.3 and above.
        BluetoothManager btManager =
                (BluetoothManager) ContextUtils.getApplicationContext().getSystemService(
                        Context.BLUETOOTH_SERVICE);
        btAdapter = btManager.getAdapter();
    } else {
        // Use static method for Android 4.2 and below to get the
        // BluetoothAdapter.
        btAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    if (btAdapter == null) {
        // Bluetooth not supported on this platform.
        return false;
    }

    int profileConnectionState;
    profileConnectionState = btAdapter.getProfileConnectionState(
            android.bluetooth.BluetoothProfile.HEADSET);

    // Ensure that Bluetooth is enabled and that a device which supports the
    // headset and handsfree profile is connected.
    // TODO(henrika): it is possible that btAdapter.isEnabled() is
    // redundant. It might be sufficient to only check the profile state.
    return btAdapter.isEnabled()
            && profileConnectionState == android.bluetooth.BluetoothProfile.STATE_CONNECTED;
}