Skip to content

Commit 26a901c

Browse files
committed
feat: add toggle checks for discovery BLE authentication requirements
1 parent 4a11814 commit 26a901c

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ class BleGattServer(private val context: Context) {
7474
return
7575
}
7676

77+
val isEnabled = try {
78+
runBlocking { dataStoreManager.getBleSyncEnabled().first() }
79+
} catch (e: Exception) {
80+
false
81+
}
82+
if (!isEnabled) {
83+
Log.d(TAG, "BLE Sync is disabled in settings, skipping start")
84+
return
85+
}
86+
7787
// Set Bluetooth adapter name dynamically based on configured device name to keep BLE matching precise
7888
val customName = try {
7989
runBlocking { dataStoreManager.getDeviceName().first() }
@@ -244,7 +254,17 @@ class BleGattServer(private val context: Context) {
244254
_connectionState.value = if (gattServer != null) BleConnectionState.ADVERTISING else BleConnectionState.DISCONNECTED
245255
isAuthenticated = false
246256
if (gattServer != null) {
247-
startAdvertising()
257+
val isEnabled = try {
258+
runBlocking { dataStoreManager.getBleSyncEnabled().first() }
259+
} catch (e: Exception) {
260+
false
261+
}
262+
if (isEnabled) {
263+
startAdvertising()
264+
} else {
265+
Log.d(TAG, "BLE Sync is disabled, stopping server")
266+
stop()
267+
}
248268
}
249269
}
250270
}
@@ -268,6 +288,14 @@ class BleGattServer(private val context: Context) {
268288
override fun onCharacteristicWriteRequest(device: BluetoothDevice, requestId: Int, characteristic: BluetoothGattCharacteristic, preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray) {
269289
Log.d(TAG, "Write request for ${characteristic.uuid}, length: ${value.size}")
270290

291+
if (characteristic.uuid != BleConstants.CHAR_AUTH_TOKEN && !isAuthenticated) {
292+
Log.w(TAG, "Blocked unauthorized write request to ${characteristic.uuid} from ${device.address}")
293+
if (responseNeeded) {
294+
gattServer?.sendResponse(device, requestId, BluetoothGatt.GATT_WRITE_NOT_PERMITTED, offset, null)
295+
}
296+
return
297+
}
298+
271299
when (characteristic.uuid) {
272300
BleConstants.CHAR_AUTH_TOKEN -> handleAuthRequest(device, value)
273301
BleConstants.CHAR_MAC_BATTERY -> handleMacBattery(value)

app/src/main/java/com/sameerasw/airsync/utils/UDPDiscoveryManager.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ object UDPDiscoveryManager {
119119
}
120120

121121
fun burstBroadcast(context: Context, durationMs: Long = 30000) {
122+
if (!isDiscoveryEnabled) {
123+
Log.d(TAG, "Discovery disabled, skipping burst broadcast")
124+
return
125+
}
126+
122127
Log.d(TAG, "Starting burst broadcast for ${durationMs}ms")
123128
burstJob?.cancel()
124129
burstJob = CoroutineScope(Dispatchers.IO).launch {
@@ -346,6 +351,8 @@ object UDPDiscoveryManager {
346351
}
347352

348353
private fun broadcastPresence(context: Context) {
354+
if (!isDiscoveryEnabled) return
355+
349356
val allIps = getAllIpAddresses()
350357
if (allIps.isEmpty()) {
351358
return
@@ -427,6 +434,8 @@ object UDPDiscoveryManager {
427434
}
428435

429436
private fun broadcastGoodbye(context: Context) {
437+
if (!isDiscoveryEnabled) return
438+
430439
val allIps = getAllIpAddresses()
431440
if (allIps.isEmpty()) return
432441

@@ -447,7 +456,7 @@ object UDPDiscoveryManager {
447456
val packet = DatagramPacket(
448457
data,
449458
data.size,
450-
InetAddress.getByName("255.55.255.255"),
459+
InetAddress.getByName("255.255.255.255"),
451460
BROADCAST_PORT
452461
)
453462
DatagramSocket(0, InetAddress.getByName(bindIp)).use { sender ->

0 commit comments

Comments
 (0)