@@ -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)
0 commit comments