Skip to content

Commit 4da51ab

Browse files
committed
feat: Disable BLE while regular conneciton active
1 parent 2c64c99 commit 4da51ab

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

app/src/main/java/com/sameerasw/airsync/data/ble/BleConnectionManager.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,31 @@ class BleConnectionManager(private val context: Context) {
4040
dataStoreManager.getBleSyncEnabled(),
4141
dataStoreManager.getBleAutoConnectEnabled(),
4242
WebSocketUtil.connectionState
43-
) { enabled, auto, wsState ->
44-
Triple(enabled, auto, wsState)
45-
}.collectLatest { (enabled, _, _) ->
43+
) { enabled, auto, wsConnected ->
44+
Triple(enabled, auto, wsConnected)
45+
}.collectLatest { (enabled, _, wsConnected) ->
4646
isBleEnabled = enabled
47-
updateBleState()
47+
updateBleState(regularConnectionActive = wsConnected)
4848
}
4949
}
5050
}
5151

52-
private fun updateBleState() {
53-
if (isBleEnabled) {
54-
Log.d(TAG, "BLE enabled, starting GATT server")
55-
bleServer?.start()
56-
} else {
52+
private fun updateBleState(regularConnectionActive: Boolean) {
53+
if (!isBleEnabled) {
5754
Log.d(TAG, "BLE disabled, stopping server")
5855
bleServer?.stop()
56+
return
57+
}
58+
59+
if (regularConnectionActive) {
60+
// Regular Wi-Fi/USB connection is up — pause advertising to save power.
61+
Log.d(TAG, "Regular connection active — pausing BLE advertising")
62+
bleServer?.pauseAdvertising()
63+
} else {
64+
// No regular connection — ensure server is started and advertising.
65+
Log.d(TAG, "No regular connection — resuming BLE advertising")
66+
bleServer?.start()
67+
bleServer?.resumeAdvertising()
5968
}
6069
}
6170

app/src/main/java/com/sameerasw/airsync/data/ble/BleGattServer.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)