下面列出了android.bluetooth.BluetoothAdapter#getProfileConnectionState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
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;
}
/**
* 是否连接了蓝牙耳机
*
* @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;
}
/**
* 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;
}