Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,19 @@ public void onComplete(Vector<ComponentName> routerServices) {
DebugTool.logInfo(TAG, ": This app's package: " + myPackage);
DebugTool.logInfo(TAG, ": Router service app's package: " + routerService.getPackageName());
if (myPackage != null && myPackage.equalsIgnoreCase(routerService.getPackageName())) {
//If the device is not null the listener should start as well as the
//case where this app was installed after BT connected and is the
//only SDL app installed on the device. (Rare corner case)
if (device != null || sdlAppInfoList.size() == 1) {
//If this app should be hosting the RS it's time to start the device
//listener. If the BT device is not null and has already connected,
//this app's RS will be started immediately. Otherwise the device
//listener will act as a gate keeper to prevent unnecessary notifications.
if (device != null || AndroidTools.isBluetoothDeviceConnected()) {
SdlDeviceListener sdlDeviceListener = getSdlDeviceListener(context, device);
if (!sdlDeviceListener.isRunning()) {
sdlDeviceListener.start();
} else {
DebugTool.logInfo(TAG, "Device listener is already running");
}
} else {
DebugTool.logInfo(TAG, "Not starting device listener, bluetooth device is null and other SDL apps installed.");
DebugTool.logInfo(TAG, "No bluetooth device and no device connected");
}
} else if (isPreAndroid12RSOnDevice) {
//If the RS app has the BLUETOOTH_CONNECT permission that means it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
package com.smartdevicelink.util;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.Manifest;
import android.content.ComponentName;
Expand Down Expand Up @@ -471,4 +473,28 @@ public static boolean hasForegroundServiceTypePermission(Context context) {
return checkPermission(context,
Manifest.permission.BLUETOOTH_CONNECT) || hasUsbAccessoryPermission(context);
}

/**
* A method that will check to see if there is a bluetooth device possibly connected. It will
* only check the headset and A2DP profiles. This is only to be used as a check for starting
* the SdlDeviceListener and not a direct start of the router service.
* @return if a bluetooth device is connected
*/
@SuppressLint("MissingPermission")
public static boolean isBluetoothDeviceConnected() {
try {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
int headsetState = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
int a2dpState = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
return headsetState == BluetoothAdapter.STATE_CONNECTING
|| headsetState == BluetoothAdapter.STATE_CONNECTED
|| a2dpState == BluetoothAdapter.STATE_CONNECTING
|| a2dpState == BluetoothAdapter.STATE_CONNECTED;
}
} catch (Exception e) {
DebugTool.logError(TAG, "Unable to check for connected bluetooth device", e);
}
return false;
}
}