Skip to content

Commit 1fb1482

Browse files
authored
Remove only app logic for device listener start (#1879)
* Remove only app logic for device listener start * Check for connected BT device when device is null
1 parent cf9d01c commit 1fb1482

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,19 @@ public void onComplete(Vector<ComponentName> routerServices) {
332332
DebugTool.logInfo(TAG, ": This app's package: " + myPackage);
333333
DebugTool.logInfo(TAG, ": Router service app's package: " + routerService.getPackageName());
334334
if (myPackage != null && myPackage.equalsIgnoreCase(routerService.getPackageName())) {
335-
//If the device is not null the listener should start as well as the
336-
//case where this app was installed after BT connected and is the
337-
//only SDL app installed on the device. (Rare corner case)
338-
if (device != null || sdlAppInfoList.size() == 1) {
335+
//If this app should be hosting the RS it's time to start the device
336+
//listener. If the BT device is not null and has already connected,
337+
//this app's RS will be started immediately. Otherwise the device
338+
//listener will act as a gate keeper to prevent unnecessary notifications.
339+
if (device != null || AndroidTools.isBluetoothDeviceConnected()) {
339340
SdlDeviceListener sdlDeviceListener = getSdlDeviceListener(context, device);
340341
if (!sdlDeviceListener.isRunning()) {
341342
sdlDeviceListener.start();
343+
} else {
344+
DebugTool.logInfo(TAG, "Device listener is already running");
342345
}
343346
} else {
344-
DebugTool.logInfo(TAG, "Not starting device listener, bluetooth device is null and other SDL apps installed.");
347+
DebugTool.logInfo(TAG, "No bluetooth device and no device connected");
345348
}
346349
} else if (isPreAndroid12RSOnDevice) {
347350
//If the RS app has the BLUETOOTH_CONNECT permission that means it

android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
package com.smartdevicelink.util;
3434

3535
import android.annotation.SuppressLint;
36+
import android.bluetooth.BluetoothAdapter;
37+
import android.bluetooth.BluetoothProfile;
3638
import android.content.BroadcastReceiver;
3739
import android.Manifest;
3840
import android.content.ComponentName;
@@ -471,4 +473,28 @@ public static boolean hasForegroundServiceTypePermission(Context context) {
471473
return checkPermission(context,
472474
Manifest.permission.BLUETOOTH_CONNECT) || hasUsbAccessoryPermission(context);
473475
}
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+
}
474500
}

0 commit comments

Comments
 (0)