From c3f5739fea72cab042faeec6c0d5e12004808eb0 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 22 Oct 2025 13:48:35 +0200 Subject: [PATCH] refactor app constants: service UUID for BLE (#494) --- .../mesh/BluetoothGattClientManager.kt | 24 +++++++------------ .../mesh/BluetoothGattServerManager.kt | 15 +++++------- .../com/bitchat/android/util/AppConstants.kt | 8 +++++++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/mesh/BluetoothGattClientManager.kt b/app/src/main/java/com/bitchat/android/mesh/BluetoothGattClientManager.kt index f263b068..e5feea0a 100644 --- a/app/src/main/java/com/bitchat/android/mesh/BluetoothGattClientManager.kt +++ b/app/src/main/java/com/bitchat/android/mesh/BluetoothGattClientManager.kt @@ -9,6 +9,7 @@ import android.content.Context import android.os.ParcelUuid import android.util.Log import com.bitchat.android.protocol.BitchatPacket +import com.bitchat.android.util.AppConstants import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -31,13 +32,6 @@ class BluetoothGattClientManager( companion object { private const val TAG = "BluetoothGattClientManager" - // Use exact same UUIDs as iOS version - private val SERVICE_UUID = UUID.fromString("F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5C") - private val CHARACTERISTIC_UUID = UUID.fromString("A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D") - private val DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb") - - // RSSI monitoring constants - private const val RSSI_UPDATE_INTERVAL = com.bitchat.android.util.AppConstants.Mesh.RSSI_UPDATE_INTERVAL_MS // 5 seconds } // Core Bluetooth components @@ -182,10 +176,10 @@ class BluetoothGattClientManager( Log.w(TAG, "Failed to request RSSI from ${deviceConn.device.address}: ${e.message}") } } - delay(RSSI_UPDATE_INTERVAL) + delay(AppConstants.Mesh.RSSI_UPDATE_INTERVAL_MS) } catch (e: Exception) { Log.w(TAG, "Error in RSSI monitoring: ${e.message}") - delay(RSSI_UPDATE_INTERVAL) + delay(AppConstants.Mesh.RSSI_UPDATE_INTERVAL_MS) } } } @@ -231,12 +225,12 @@ class BluetoothGattClientManager( } val scanFilter = ScanFilter.Builder() - .setServiceUuid(ParcelUuid(SERVICE_UUID)) + .setServiceUuid(ParcelUuid(AppConstants.Mesh.Gatt.SERVICE_UUID)) .build() val scanFilters = listOf(scanFilter) - Log.d(TAG, "Starting BLE scan with target service UUID: $SERVICE_UUID") + Log.d(TAG, "Starting BLE scan with target service UUID: ${AppConstants.Mesh.Gatt.SERVICE_UUID}") scanCallback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult) { @@ -321,7 +315,7 @@ class BluetoothGattClientManager( val scanRecord = result.scanRecord // CRITICAL: Only process devices that have our service UUID - val hasOurService = scanRecord?.serviceUuids?.any { it.uuid == SERVICE_UUID } == true + val hasOurService = scanRecord?.serviceUuids?.any { it.uuid == AppConstants.Mesh.Gatt.SERVICE_UUID } == true if (!hasOurService) { return } @@ -456,9 +450,9 @@ class BluetoothGattClientManager( override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { if (status == BluetoothGatt.GATT_SUCCESS) { - val service = gatt.getService(SERVICE_UUID) + val service = gatt.getService(AppConstants.Mesh.Gatt.SERVICE_UUID) if (service != null) { - val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID) + val characteristic = service.getCharacteristic(AppConstants.Mesh.Gatt.CHARACTERISTIC_UUID) if (characteristic != null) { connectionTracker.getDeviceConnection(deviceAddress)?.let { deviceConn -> val updatedConn = deviceConn.copy(characteristic = characteristic) @@ -467,7 +461,7 @@ class BluetoothGattClientManager( } gatt.setCharacteristicNotification(characteristic, true) - val descriptor = characteristic.getDescriptor(DESCRIPTOR_UUID) + val descriptor = characteristic.getDescriptor(AppConstants.Mesh.Gatt.DESCRIPTOR_UUID) if (descriptor != null) { descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE gatt.writeDescriptor(descriptor) diff --git a/app/src/main/java/com/bitchat/android/mesh/BluetoothGattServerManager.kt b/app/src/main/java/com/bitchat/android/mesh/BluetoothGattServerManager.kt index 5be14307..6a1c6fbf 100644 --- a/app/src/main/java/com/bitchat/android/mesh/BluetoothGattServerManager.kt +++ b/app/src/main/java/com/bitchat/android/mesh/BluetoothGattServerManager.kt @@ -9,6 +9,7 @@ import android.content.Context import android.os.ParcelUuid import android.util.Log import com.bitchat.android.protocol.BitchatPacket +import com.bitchat.android.util.AppConstants import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -28,10 +29,6 @@ class BluetoothGattServerManager( companion object { private const val TAG = "BluetoothGattServerManager" - // Use exact same UUIDs as iOS version - private val SERVICE_UUID = UUID.fromString("F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5C") - private val CHARACTERISTIC_UUID = UUID.fromString("A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D") - private val DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb") } // Core Bluetooth components @@ -223,7 +220,7 @@ class BluetoothGattServerManager( return } - if (characteristic.uuid == CHARACTERISTIC_UUID) { + if (characteristic.uuid == AppConstants.Mesh.Gatt.CHARACTERISTIC_UUID) { Log.i(TAG, "Server: Received packet from ${device.address}, size: ${value.size} bytes") val packet = BitchatPacket.fromBinaryData(value) if (packet != null) { @@ -297,7 +294,7 @@ class BluetoothGattServerManager( // Create characteristic with notification support characteristic = BluetoothGattCharacteristic( - CHARACTERISTIC_UUID, + AppConstants.Mesh.Gatt.CHARACTERISTIC_UUID, BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE or @@ -307,12 +304,12 @@ class BluetoothGattServerManager( ) val descriptor = BluetoothGattDescriptor( - DESCRIPTOR_UUID, + AppConstants.Mesh.Gatt.DESCRIPTOR_UUID, BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE ) characteristic?.addDescriptor(descriptor) - val service = BluetoothGattService(SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY) + val service = BluetoothGattService(AppConstants.Mesh.Gatt.SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY) service.addCharacteristic(characteristic) gattServer?.addService(service) @@ -357,7 +354,7 @@ class BluetoothGattServerManager( val settings = powerManager.getAdvertiseSettings() val data = AdvertiseData.Builder() - .addServiceUuid(ParcelUuid(SERVICE_UUID)) + .addServiceUuid(ParcelUuid(AppConstants.Mesh.Gatt.SERVICE_UUID)) .setIncludeTxPowerLevel(false) .setIncludeDeviceName(false) .build() diff --git a/app/src/main/java/com/bitchat/android/util/AppConstants.kt b/app/src/main/java/com/bitchat/android/util/AppConstants.kt index ce396b9a..a332fca2 100644 --- a/app/src/main/java/com/bitchat/android/util/AppConstants.kt +++ b/app/src/main/java/com/bitchat/android/util/AppConstants.kt @@ -1,5 +1,7 @@ package com.bitchat.android.util +import java.util.UUID + /** * Centralized application-wide constants. */ @@ -22,6 +24,12 @@ object AppConstants { // GATT client RSSI updates const val RSSI_UPDATE_INTERVAL_MS: Long = 5_000L + + object Gatt { + val SERVICE_UUID: UUID = UUID.fromString("F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5C") + val CHARACTERISTIC_UUID: UUID = UUID.fromString("A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D") + val DESCRIPTOR_UUID: UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb") + } } object Sync {