120120import java .util .concurrent .ScheduledExecutorService ;
121121import java .util .concurrent .TimeUnit ;
122122
123+ import static android .Manifest .permission .BLUETOOTH_CONNECT ;
124+ import static android .Manifest .permission .BLUETOOTH_SCAN ;
123125import static com .smartdevicelink .transport .TransportConstants .CONNECTED_DEVICE_STRING_EXTRA_NAME ;
124126import static com .smartdevicelink .transport .TransportConstants .FOREGROUND_EXTRA ;
125127import static com .smartdevicelink .transport .TransportConstants .FORMED_PACKET_EXTRA_NAME ;
@@ -217,7 +219,6 @@ public class SdlRouterService extends Service {
217219
218220 private boolean startSequenceComplete = false ;
219221 private VehicleType receivedVehicleType ;
220- private boolean isConnectedOverUSB ;
221222 private boolean waitingForBTRuntimePermissions = false ;
222223 private Handler btPermissionsHandler ;
223224 private Runnable btPermissionsRunnable ;
@@ -373,7 +374,8 @@ public void handleMessage(Message msg) {
373374 switch (msg .what ) {
374375 case TransportConstants .ROUTER_REQUEST_BT_CLIENT_CONNECT :
375376 //Starting with Android 12 this use case will require the BLUETOOTH_SCAN PERMISSION
376- if (!AndroidTools .isBtScanPermissionGranted (service .getApplicationContext (), service .getPackageName ())) {
377+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !AndroidTools .isPermissionGranted (BLUETOOTH_SCAN , service .getApplicationContext (), service .getPackageName ())) {
378+ DebugTool .logError (TAG , "BLUETOOTH_SCAN Permissions not granted for this app" );
377379 break ;
378380 }
379381 if (receivedBundle .getBoolean (TransportConstants .CONNECT_AS_CLIENT_BOOLEAN_EXTRA , false )
@@ -1096,7 +1098,7 @@ private boolean permissionCheck(String permissionToCheck) {
10961098 *
10971099 * @return true if this service is set up correctly
10981100 */
1099- private boolean initCheck () {
1101+ private boolean initCheck (boolean isConnectedOverUSB ) {
11001102 if (!processCheck ()) {
11011103 DebugTool .logError (TAG , "Not using correct process. Shutting down" );
11021104 wrongProcess = true ;
@@ -1108,7 +1110,7 @@ private boolean initCheck() {
11081110 }
11091111
11101112 // If Android 12 or newer make sure we have BLUETOOTH_CONNECT Runtime permission
1111- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !AndroidTools .isBtConnectPermissionGranted ( this , this .getPackageName ())) {
1113+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !AndroidTools .isPermissionGranted ( BLUETOOTH_CONNECT , this , this .getPackageName ())) {
11121114 if (!isConnectedOverUSB ) { //If BLUETOOTH_CONNECT permission is not granted We want to make sure we are connected over USB
11131115 return false ;
11141116 }
@@ -1275,13 +1277,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {
12751277 (HashMap <String , Object >) intent .getSerializableExtra (TransportConstants .VEHICLE_INFO_EXTRA )
12761278 );
12771279 }
1280+ boolean isConnectedOverUSB = false ;
12781281 if (intent != null && intent .hasExtra (TransportConstants .CONNECTION_TYPE_EXTRA )) {
12791282 isConnectedOverUSB = TransportConstants .ACTION_USB_ACCESSORY_ATTACHED .equalsIgnoreCase (intent .getStringExtra (TransportConstants .CONNECTION_TYPE_EXTRA ));
12801283 }
12811284 // Only trusting the first intent received to start the RouterService and run initial checks to avoid a case where an app could send incorrect data after the spp connection has started.
12821285 if (firstStart ) {
12831286 firstStart = false ;
1284- if (!initCheck ()) { // Run checks on process and permissions
1287+ if (!initCheck (isConnectedOverUSB )) { // Run checks on process and permissions
12851288 deployNextRouterService ();
12861289 closeSelf ();
12871290 return START_REDELIVER_INTENT ;
@@ -1306,7 +1309,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
13061309 }
13071310 if (intent != null ) {
13081311 if (intent .getBooleanExtra (FOREGROUND_EXTRA , false )) {
1309- hasConnectedBefore = false ;
13101312 hasCalledStartForeground = false ;
13111313
13121314 if (!this .isPrimaryTransportConnected ()) { //If there is no transport connected we need to ensure the service is moved to the foreground
@@ -1361,6 +1363,10 @@ public void onDestroy() {
13611363 altTransportTimerHandler = null ;
13621364 }
13631365
1366+ if (btPermissionsHandler != null && btPermissionsRunnable != null ) {
1367+ btPermissionsHandler .removeCallbacks (btPermissionsRunnable );
1368+ }
1369+
13641370 DebugTool .logWarning (TAG , "Sdl Router Service Destroyed" );
13651371 closing = true ;
13661372 //No need for this Broadcast Receiver anymore
@@ -1723,6 +1729,9 @@ private boolean isTransportConnected(TransportType transportType) {
17231729 */
17241730 @ SuppressWarnings ("MissingPermission" )
17251731 private boolean bluetoothAvailable () {
1732+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !AndroidTools .isPermissionGranted (BLUETOOTH_CONNECT , SdlRouterService .this , SdlRouterService .this .getPackageName ())) {
1733+ return false ;
1734+ }
17261735 try {
17271736 return (!(BluetoothAdapter .getDefaultAdapter () == null ) && BluetoothAdapter .getDefaultAdapter ().isEnabled ());
17281737 } catch (NullPointerException e ) { // only for BluetoothAdapter.getDefaultAdapter().isEnabled() call
@@ -1785,7 +1794,7 @@ public void closeSelf() {
17851794
17861795 private synchronized void initBluetoothSerialService () {
17871796 if (waitingForBTRuntimePermissions ) {
1788- //The app has not be granted the BLUETOOTH_CONNECT runtime permission
1797+ DebugTool . logWarning ( TAG , "This app has not been granted the BLUETOOTH_CONNECT runtime permission" );
17891798 return ;
17901799 }
17911800
@@ -1864,15 +1873,15 @@ public void onTransportConnected(final TransportRecord record) {
18641873 notifyClients (createHardwareConnectedMessage (record ));
18651874 }
18661875
1867- if (isConnectedOverUSB && !AndroidTools .isBtConnectPermissionGranted ( SdlRouterService .this , SdlRouterService .this .getPackageName ())) {
1876+ if (Build . VERSION . SDK_INT >= Build . VERSION_CODES . S && TransportType . USB . equals ( record . getType ()) && !AndroidTools .isPermissionGranted ( BLUETOOTH_CONNECT , SdlRouterService .this , SdlRouterService .this .getPackageName ())) {
18681877 //Delay starting bluetoothTransport when we are connected over USB and the app does not have the BLUETOOTH_CONNECT permissions
18691878 waitingForBTRuntimePermissions = true ;
18701879 btPermissionsHandler = new Handler (Looper .myLooper ());
18711880 //Continuously Check for the BLUETOOTH_CONNECT Permission
18721881 btPermissionsRunnable = new Runnable () {
18731882 @ Override
18741883 public void run () {
1875- if (!AndroidTools .isBtConnectPermissionGranted ( SdlRouterService .this , SdlRouterService .this .getPackageName ())) {
1884+ if (!AndroidTools .isPermissionGranted ( BLUETOOTH_CONNECT , SdlRouterService .this , SdlRouterService .this .getPackageName ())) {
18761885 btPermissionsHandler .postDelayed (btPermissionsRunnable , BT_PERMISSIONS_CHECK_FREQUENCY );
18771886 } else {
18781887 waitingForBTRuntimePermissions = false ;
0 commit comments