Skip to content

Commit 17d39c2

Browse files
committed
Toggle Service enabled based on BT permissions
1 parent a07bd7c commit 17d39c2

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Intent;
44
import android.content.pm.PackageManager;
5+
import android.os.Build;
56
import android.os.Bundle;
67
import android.view.Menu;
78
import android.view.MenuItem;
@@ -23,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2324
super.onCreate(savedInstanceState);
2425
setContentView(R.layout.activity_main);
2526

26-
if (android.os.Build.VERSION.SDK_INT >= 31) {
27+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2728
if (!checkPermission()) {
2829
requestPermission();
2930
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import android.content.ComponentName;
4242
import android.content.Context;
4343
import android.content.Intent;
44+
import android.content.pm.PackageInfo;
4445
import android.content.pm.PackageManager;
46+
import android.content.pm.ServiceInfo;
4547
import android.hardware.usb.UsbManager;
4648
import android.os.Build;
4749
import android.os.Looper;
@@ -75,6 +77,7 @@
7577
public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
7678

7779
private static final String TAG = "Sdl Broadcast Receiver";
80+
private static final String SDL_ROUTER_SERVICE_PROCESS_NAME = "com.smartdevicelink.router";
7881

7982
protected static final String SDL_ROUTER_SERVICE_CLASS_NAME = "sdlrouterservice";
8083

@@ -131,22 +134,28 @@ public void onReceive(Context context, Intent intent) {
131134
onSdlEnabled(context, intent);
132135
return;
133136
} else {
134-
//Should Be BT?
135-
136-
//Check BT Permissions
137-
int btConnectPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_CONNECT);
138-
int btScanPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_SCAN);
139-
140-
//Get ComponentName
141-
PackageManager pm = context.getPackageManager();
142-
ComponentName componentName = intent.getParcelableExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME);
137+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
138+
//Check BT Permissions
139+
int btConnectPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_CONNECT);
140+
int btScanPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_SCAN);
141+
142+
PackageManager pm = context.getPackageManager();
143+
try {
144+
PackageInfo info = pm.getPackageInfo(context.getPackageName(),PackageManager.GET_SERVICES);
145+
ServiceInfo[] services = info.services;
146+
if (services != null) {
147+
for (ServiceInfo service : services) {
148+
//If this service is this apps router service
149+
if (service.processName != null && service.processName.equalsIgnoreCase(SDL_ROUTER_SERVICE_PROCESS_NAME)) {
150+
//Set the service enabled flag to True or False based on if the user has granted BT permissions
151+
service.enabled = btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
152+
}
153+
}
154+
}
143155

144-
if (btConnectPermission != PackageManager.PERMISSION_GRANTED || btScanPermission != PackageManager.PERMISSION_GRANTED) {
145-
//User has denied BT Permissions we need to disable this apps router service
146-
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
147-
} else {
148-
//User has enabled BT Permissions we need to enable this apps router service
149-
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
156+
} catch (PackageManager.NameNotFoundException e) {
157+
e.printStackTrace();
158+
}
150159
}
151160
}
152161

0 commit comments

Comments
 (0)