@@ -90,6 +90,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
9090 private static Thread .UncaughtExceptionHandler foregroundExceptionHandler = null ;
9191 private static final Object DEVICE_LISTENER_LOCK = new Object ();
9292 private static SdlDeviceListener sdlDeviceListener ;
93+ private static String serviceName ;
9394
9495 public int getRouterServiceVersion () {
9596 return SdlRouterService .ROUTER_SERVICE_VERSION_NUMBER ;
@@ -98,6 +99,8 @@ public int getRouterServiceVersion() {
9899 @ Override
99100 @ CallSuper
100101 public void onReceive (Context context , Intent intent ) {
102+ serviceName = getSdlServiceName ();
103+
101104 //Log.i(TAG, "Sdl Receiver Activated");
102105 final String action = intent .getAction ();
103106 if (action == null ) {
@@ -297,9 +300,17 @@ public void onComplete(Vector<ComponentName> routerServices) {
297300 if (sdlAppInfoList != null && !sdlAppInfoList .isEmpty () && sdlAppInfoList .get (0 ).getRouterServiceComponentName () != null ) {
298301 routerServicePackage = sdlAppInfoList .get (0 ).getRouterServiceComponentName ().getPackageName ();
299302 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
300- if (!AndroidTools .areBtPermissionsGranted (context , routerServicePackage ) && sdlAppInfoList .size () > 1 ) {
303+ boolean preAndroid12RouterServiceOnDevice = false ;
304+ for (SdlAppInfo appInfo : sdlAppInfoList ) {
305+ //If the RS version is older than Android 12 update version
306+ if (appInfo .getRouterServiceVersion () < 16 ) {
307+ preAndroid12RouterServiceOnDevice = true ;
308+ break ;
309+ }
310+ }
311+ if (!preAndroid12RouterServiceOnDevice && !AndroidTools .isBtConnectPermissionGranted (context , routerServicePackage ) && sdlAppInfoList .size () > 1 ) {
301312 for (SdlAppInfo appInfo : sdlAppInfoList ) {
302- if (AndroidTools .areBtPermissionsGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
313+ if (AndroidTools .isBtConnectPermissionGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
303314 routerServicePackage = appInfo .getRouterServiceComponentName ().getPackageName ();
304315 break ;
305316 }
@@ -388,10 +399,9 @@ static protected void setForegroundExceptionHandler() {
388399 public void uncaughtException (Thread t , Throwable e ) {
389400 if (e != null
390401 && e instanceof AndroidRuntimeException
391- && "android.app.RemoteServiceException" .equals (e .getClass ().getName ()) //android.app.RemoteServiceException is a private class
402+ && ( "android.app.RemoteServiceException" .equals (e .getClass ().getName ()) || "android.app.ForegroundServiceDidNotStartInTimeException" . equals ( e . getClass (). getName ())) //android.app.RemoteServiceException is a private class
392403 && e .getMessage () != null
393- && e .getMessage ().contains ("SdlRouterService" )) {
394-
404+ && (e .getMessage ().contains ("SdlRouterService" )) || e .getMessage ().contains (serviceName )) {
395405 DebugTool .logInfo (TAG , "Handling failed startForegroundService call" );
396406 Looper .loop ();
397407 } else if (defaultUncaughtExceptionHandler != null ) { //No other exception should be handled
@@ -611,9 +621,17 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
611621 //If we are on android 12 check the app has BT permissions
612622 //If it does not try to find another app in the list that does;
613623 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
614- if (!AndroidTools .areBtPermissionsGranted (context , routerService .getPackageName ()) && sdlAppInfoList .size () > 1 ) {
624+ boolean preAndroid12RouterServiceOnDevice = false ;
625+ for (SdlAppInfo appInfo : sdlAppInfoList ) {
626+ //If the RS version is older than Android 12 update version
627+ if (appInfo .getRouterServiceVersion () < 16 ) {
628+ preAndroid12RouterServiceOnDevice = true ;
629+ break ;
630+ }
631+ }
632+ if (!preAndroid12RouterServiceOnDevice && !AndroidTools .isBtConnectPermissionGranted (context , routerService .getPackageName ()) && sdlAppInfoList .size () > 1 ) {
615633 for (SdlAppInfo appInfo : sdlAppInfoList ) {
616- if (AndroidTools .areBtPermissionsGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
634+ if (AndroidTools .isBtConnectPermissionGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
617635 routerService = appInfo .getRouterServiceComponentName ();
618636 break ;
619637 }
@@ -678,6 +696,10 @@ public static ComponentName consumeQueuedRouterService() {
678696 */
679697 public abstract void onSdlEnabled (Context context , Intent intent );
680698
699+ public String getSdlServiceName () {
700+ return "SdlService" ;
701+ }
702+
681703 //public abstract void onSdlDisabled(Context context); //Removing for now until we're able to abstract from developer
682704
683705
0 commit comments