|
33 | 33 | package com.smartdevicelink.util; |
34 | 34 |
|
35 | 35 | import android.annotation.SuppressLint; |
| 36 | +import android.bluetooth.BluetoothAdapter; |
| 37 | +import android.bluetooth.BluetoothProfile; |
36 | 38 | import android.content.BroadcastReceiver; |
37 | 39 | import android.Manifest; |
38 | 40 | import android.content.ComponentName; |
@@ -471,4 +473,28 @@ public static boolean hasForegroundServiceTypePermission(Context context) { |
471 | 473 | return checkPermission(context, |
472 | 474 | Manifest.permission.BLUETOOTH_CONNECT) || hasUsbAccessoryPermission(context); |
473 | 475 | } |
| 476 | + |
| 477 | + /** |
| 478 | + * A method that will check to see if there is a bluetooth device possibly connected. It will |
| 479 | + * only check the headset and A2DP profiles. This is only to be used as a check for starting |
| 480 | + * the SdlDeviceListener and not a direct start of the router service. |
| 481 | + * @return if a bluetooth device is connected |
| 482 | + */ |
| 483 | + @SuppressLint("MissingPermission") |
| 484 | + public static boolean isBluetoothDeviceConnected() { |
| 485 | + try { |
| 486 | + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 487 | + if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) { |
| 488 | + int headsetState = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET); |
| 489 | + int a2dpState = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP); |
| 490 | + return headsetState == BluetoothAdapter.STATE_CONNECTING |
| 491 | + || headsetState == BluetoothAdapter.STATE_CONNECTED |
| 492 | + || a2dpState == BluetoothAdapter.STATE_CONNECTING |
| 493 | + || a2dpState == BluetoothAdapter.STATE_CONNECTED; |
| 494 | + } |
| 495 | + } catch (Exception e) { |
| 496 | + DebugTool.logError(TAG, "Unable to check for connected bluetooth device", e); |
| 497 | + } |
| 498 | + return false; |
| 499 | + } |
474 | 500 | } |
0 commit comments