Skip to content

Commit 8bb0272

Browse files
HeniganHenigan
authored andcommitted
Update BT Permissions and Notification Behavior
1 parent 01ca770 commit 8bb0272

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

android/hello_sdl_android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.sdl.hellosdlandroid">
55

6-
<uses-permission android:name="android.permission.BLUETOOTH" />
6+
<uses-permission android:name="android.permission.BLUETOOTH"
7+
android:maxSdkVersion="30"/>
8+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
9+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
10+
android:usesPermissionFlags="neverForLocation"
11+
tools:targetApi="31" />
712
<uses-permission android:name="android.permission.INTERNET" />
813
<!-- Required to check if WiFi is enabled -->
914
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ public void enterForeground() {
108108
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
109109
if (notificationManager != null) {
110110
notificationManager.createNotificationChannel(channel);
111-
Notification serviceNotification = new Notification.Builder(this, channel.getId())
111+
Notification.Builder builder = new Notification.Builder(this, channel.getId())
112112
.setContentTitle("Connected through SDL")
113-
.setSmallIcon(R.drawable.ic_sdl)
114-
.build();
113+
.setSmallIcon(R.drawable.ic_sdl);
114+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
115+
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
116+
}
117+
Notification serviceNotification = builder.build();
115118
startForeground(FOREGROUND_SERVICE_ID, serviceNotification);
116119
}
117120
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,10 @@ private void enterForeground(String content, long chronometerLength, boolean ong
14641464
builder = new Notification.Builder(this, SDL_NOTIFICATION_CHANNEL_ID);
14651465
}
14661466

1467+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
1468+
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
1469+
}
1470+
14671471
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) { //If we are in debug mode, include what app has the router service open
14681472
ComponentName name = new ComponentName(this, this.getClass());
14691473
builder.setContentTitle("SDL: " + name.getPackageName());
@@ -1534,12 +1538,22 @@ private void enterForeground(String content, long chronometerLength, boolean ong
15341538
private void safeStartForeground(int id, Notification notification) {
15351539
try {
15361540
if (notification == null) {
1537-
//Try the NotificationCompat this time in case there was a previous error
1538-
NotificationCompat.Builder builder =
1539-
new NotificationCompat.Builder(this, SDL_NOTIFICATION_CHANNEL_ID)
1541+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
1542+
Notification.Builder builder =
1543+
new Notification.Builder(this, SDL_NOTIFICATION_CHANNEL_ID)
15401544
.setContentTitle("SmartDeviceLink")
1541-
.setContentText("Service Running");
1542-
notification = builder.build();
1545+
.setContentText("Service Running")
1546+
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
1547+
notification = builder.build();
1548+
} else {
1549+
//Try the NotificationCompat this time in case there was a previous error
1550+
NotificationCompat.Builder builder =
1551+
new NotificationCompat.Builder(this, SDL_NOTIFICATION_CHANNEL_ID)
1552+
.setContentTitle("SmartDeviceLink")
1553+
.setContentText("Service Running");
1554+
1555+
notification = builder.build();
1556+
}
15431557
}
15441558
startForeground(id, notification);
15451559
DebugTool.logInfo(TAG, "Entered the foreground - " + System.currentTimeMillis());
@@ -3758,6 +3772,10 @@ private void notifySppError() {
37583772
} else {
37593773
builder = new Notification.Builder(getApplicationContext(), TransportConstants.SDL_ERROR_NOTIFICATION_CHANNEL_ID);
37603774
}
3775+
3776+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
3777+
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
3778+
}
37613779
ComponentName name = new ComponentName(this, this.getClass());
37623780
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) { //If we are in debug mode, include what app has the router service open
37633781
builder.setContentTitle("SDL: " + name.getPackageName());

0 commit comments

Comments
 (0)