|
| 1 | +/* |
| 2 | + * © 2025 Sphereon International B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.sphereon.kiwa.sample.app |
| 19 | + |
| 20 | +import android.app.Notification |
| 21 | +import android.app.NotificationChannel |
| 22 | +import android.app.NotificationManager |
| 23 | +import android.app.PendingIntent |
| 24 | +import android.app.Service |
| 25 | +import android.content.Context |
| 26 | +import android.content.Intent |
| 27 | +import android.content.pm.ServiceInfo |
| 28 | +import android.os.Build |
| 29 | +import android.os.IBinder |
| 30 | +import androidx.core.app.NotificationCompat |
| 31 | + |
| 32 | +/** |
| 33 | + * Foreground service to maintain BLE advertising and engagement when the screen is locked. |
| 34 | + * |
| 35 | + * This service is essential for keeping BLE operations alive when the device screen is off |
| 36 | + * or the app is in the background. Without this service running as a foreground service, |
| 37 | + * Android will suspend BLE advertising when the screen locks, causing connection failures. |
| 38 | + * |
| 39 | + * Key responsibilities: |
| 40 | + * - Maintain foreground service status to prevent Android from killing BLE operations |
| 41 | + * - Display persistent notification to indicate active BLE engagement |
| 42 | + * - Survive screen lock and app backgrounding |
| 43 | + * - Coordinate with engagement manager for BLE lifecycle |
| 44 | + * |
| 45 | + * Note: This service must be started with startForeground() within 5 seconds of creation |
| 46 | + * as per Android requirements for foreground services. |
| 47 | + */ |
| 48 | +class BleEngagementService : Service() { |
| 49 | + |
| 50 | + companion object { |
| 51 | + private const val NOTIFICATION_ID = 2001 |
| 52 | + private const val CHANNEL_ID = "ble_engagement_channel" |
| 53 | + private const val CHANNEL_NAME = "BLE Engagement" |
| 54 | + |
| 55 | + /** |
| 56 | + * Starts the BLE engagement foreground service. |
| 57 | + * |
| 58 | + * @param context Application or activity context |
| 59 | + */ |
| 60 | + fun start(context: Context) { |
| 61 | + val intent = Intent(context, BleEngagementService::class.java) |
| 62 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 63 | + context.startForegroundService(intent) |
| 64 | + } else { |
| 65 | + context.startService(intent) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Stops the BLE engagement foreground service. |
| 71 | + * |
| 72 | + * @param context Application or activity context |
| 73 | + */ |
| 74 | + fun stop(context: Context) { |
| 75 | + val intent = Intent(context, BleEngagementService::class.java) |
| 76 | + context.stopService(intent) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private val app: KiwaSampleApplication |
| 81 | + get() = application as KiwaSampleApplication |
| 82 | + |
| 83 | + override fun onCreate() { |
| 84 | + super.onCreate() |
| 85 | + app.log.info("BleEngagementService: Service created") |
| 86 | + createNotificationChannel() |
| 87 | + } |
| 88 | + |
| 89 | + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
| 90 | + app.log.info("BleEngagementService: onStartCommand called") |
| 91 | + |
| 92 | + val notification = createNotification() |
| 93 | + |
| 94 | + // Start foreground with appropriate service type for Android 14+ |
| 95 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { |
| 96 | + startForeground( |
| 97 | + NOTIFICATION_ID, |
| 98 | + notification, |
| 99 | + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE |
| 100 | + ) |
| 101 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 102 | + startForeground( |
| 103 | + NOTIFICATION_ID, |
| 104 | + notification, |
| 105 | + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE |
| 106 | + ) |
| 107 | + } else { |
| 108 | + startForeground(NOTIFICATION_ID, notification) |
| 109 | + } |
| 110 | + |
| 111 | + app.log.info("BleEngagementService: Started as foreground service") |
| 112 | + |
| 113 | + // Return START_STICKY to ensure service is restarted if killed by system |
| 114 | + return START_STICKY |
| 115 | + } |
| 116 | + |
| 117 | + override fun onBind(intent: Intent?): IBinder? { |
| 118 | + // This service doesn't support binding |
| 119 | + return null |
| 120 | + } |
| 121 | + |
| 122 | + override fun onDestroy() { |
| 123 | + app.log.info("BleEngagementService: Service destroyed") |
| 124 | + super.onDestroy() |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creates the notification channel for BLE engagement notifications. |
| 129 | + * Required for Android O and above. |
| 130 | + */ |
| 131 | + private fun createNotificationChannel() { |
| 132 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 133 | + val channel = NotificationChannel( |
| 134 | + CHANNEL_ID, |
| 135 | + CHANNEL_NAME, |
| 136 | + NotificationManager.IMPORTANCE_LOW |
| 137 | + ).apply { |
| 138 | + description = "Keeps BLE engagement active" |
| 139 | + setShowBadge(false) |
| 140 | + } |
| 141 | + |
| 142 | + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager |
| 143 | + notificationManager.createNotificationChannel(channel) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates the persistent notification shown while the service is running. |
| 149 | + * |
| 150 | + * @return Notification to be displayed |
| 151 | + */ |
| 152 | + private fun createNotification(): Notification { |
| 153 | + val notificationIntent = Intent(this, MainActivity::class.java) |
| 154 | + val pendingIntent = PendingIntent.getActivity( |
| 155 | + this, |
| 156 | + 0, |
| 157 | + notificationIntent, |
| 158 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 159 | + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT |
| 160 | + } else { |
| 161 | + PendingIntent.FLAG_UPDATE_CURRENT |
| 162 | + } |
| 163 | + ) |
| 164 | + |
| 165 | + return NotificationCompat.Builder(this, CHANNEL_ID) |
| 166 | + .setContentTitle("BLE Engagement Active") |
| 167 | + .setContentText("Ready to connect with nearby devices") |
| 168 | + .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth) |
| 169 | + .setContentIntent(pendingIntent) |
| 170 | + .setOngoing(true) |
| 171 | + .setPriority(NotificationCompat.PRIORITY_LOW) |
| 172 | + .setCategory(NotificationCompat.CATEGORY_SERVICE) |
| 173 | + .build() |
| 174 | + } |
| 175 | +} |
0 commit comments