@@ -48,6 +48,7 @@ class BleGattServer(private val context: Context) {
4848 private set
4949 private var negotiatedMtu = 23
5050 private var heartbeatJob: Job ? = null
51+ private var isAdvertisingPaused = false
5152
5253 enum class BleConnectionState {
5354 DISCONNECTED , ADVERTISING , CONNECTED , AUTHENTICATED
@@ -124,6 +125,7 @@ class BleGattServer(private val context: Context) {
124125 pendingServices.clear()
125126 _connectionState .value = BleConnectionState .DISCONNECTED
126127 isAuthenticated = false
128+ isAdvertisingPaused = false
127129 }
128130
129131 private fun setupGattServer () {
@@ -227,6 +229,32 @@ class BleGattServer(private val context: Context) {
227229 currentAdvertiseCallback = null
228230 }
229231
232+ /* *
233+ * Stop advertising while keeping the GATT server alive.
234+ * Existing BLE connections remain active
235+ */
236+ fun pauseAdvertising () {
237+ if (isAdvertisingPaused) return
238+ Log .d(TAG , " BLE advertising paused (regular connection active)" )
239+ isAdvertisingPaused = true
240+ stopAdvertising()
241+ if (_connectionState .value == BleConnectionState .ADVERTISING ) {
242+ _connectionState .value = BleConnectionState .CONNECTED
243+ }
244+ }
245+
246+ /* *
247+ * Resume advertising after it was paused. No-op if already advertising.
248+ */
249+ fun resumeAdvertising () {
250+ if (! isAdvertisingPaused) return
251+ if (gattServer == null ) return
252+ if (_connectionState .value == BleConnectionState .DISCONNECTED ) return
253+ Log .d(TAG , " BLE advertising resumed" )
254+ isAdvertisingPaused = false
255+ startAdvertising()
256+ }
257+
230258 private val gattServerCallback = object : BluetoothGattServerCallback () {
231259 override fun onServiceAdded (status : Int , service : BluetoothGattService ) {
232260 Log .d(TAG , " Service added: ${service.uuid} , status: $status " )
@@ -260,7 +288,9 @@ class BleGattServer(private val context: Context) {
260288 false
261289 }
262290 if (isEnabled) {
263- startAdvertising()
291+ if (! isAdvertisingPaused) {
292+ startAdvertising()
293+ }
264294 } else {
265295 Log .d(TAG , " BLE Sync is disabled, stopping server" )
266296 stop()
0 commit comments