mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 01:05:20 +00:00
refactor app constants: service UUID for BLE (#494)
This commit is contained in:
@@ -9,6 +9,7 @@ import android.content.Context
|
|||||||
import android.os.ParcelUuid
|
import android.os.ParcelUuid
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.bitchat.android.protocol.BitchatPacket
|
import com.bitchat.android.protocol.BitchatPacket
|
||||||
|
import com.bitchat.android.util.AppConstants
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -31,13 +32,6 @@ class BluetoothGattClientManager(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "BluetoothGattClientManager"
|
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
|
// Core Bluetooth components
|
||||||
@@ -182,10 +176,10 @@ class BluetoothGattClientManager(
|
|||||||
Log.w(TAG, "Failed to request RSSI from ${deviceConn.device.address}: ${e.message}")
|
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) {
|
} catch (e: Exception) {
|
||||||
Log.w(TAG, "Error in RSSI monitoring: ${e.message}")
|
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()
|
val scanFilter = ScanFilter.Builder()
|
||||||
.setServiceUuid(ParcelUuid(SERVICE_UUID))
|
.setServiceUuid(ParcelUuid(AppConstants.Mesh.Gatt.SERVICE_UUID))
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val scanFilters = listOf(scanFilter)
|
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() {
|
scanCallback = object : ScanCallback() {
|
||||||
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
||||||
@@ -321,7 +315,7 @@ class BluetoothGattClientManager(
|
|||||||
val scanRecord = result.scanRecord
|
val scanRecord = result.scanRecord
|
||||||
|
|
||||||
// CRITICAL: Only process devices that have our service UUID
|
// 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) {
|
if (!hasOurService) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -456,9 +450,9 @@ class BluetoothGattClientManager(
|
|||||||
|
|
||||||
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
|
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
val service = gatt.getService(SERVICE_UUID)
|
val service = gatt.getService(AppConstants.Mesh.Gatt.SERVICE_UUID)
|
||||||
if (service != null) {
|
if (service != null) {
|
||||||
val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID)
|
val characteristic = service.getCharacteristic(AppConstants.Mesh.Gatt.CHARACTERISTIC_UUID)
|
||||||
if (characteristic != null) {
|
if (characteristic != null) {
|
||||||
connectionTracker.getDeviceConnection(deviceAddress)?.let { deviceConn ->
|
connectionTracker.getDeviceConnection(deviceAddress)?.let { deviceConn ->
|
||||||
val updatedConn = deviceConn.copy(characteristic = characteristic)
|
val updatedConn = deviceConn.copy(characteristic = characteristic)
|
||||||
@@ -467,7 +461,7 @@ class BluetoothGattClientManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
gatt.setCharacteristicNotification(characteristic, true)
|
gatt.setCharacteristicNotification(characteristic, true)
|
||||||
val descriptor = characteristic.getDescriptor(DESCRIPTOR_UUID)
|
val descriptor = characteristic.getDescriptor(AppConstants.Mesh.Gatt.DESCRIPTOR_UUID)
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
||||||
gatt.writeDescriptor(descriptor)
|
gatt.writeDescriptor(descriptor)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import android.content.Context
|
|||||||
import android.os.ParcelUuid
|
import android.os.ParcelUuid
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.bitchat.android.protocol.BitchatPacket
|
import com.bitchat.android.protocol.BitchatPacket
|
||||||
|
import com.bitchat.android.util.AppConstants
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -28,10 +29,6 @@ class BluetoothGattServerManager(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "BluetoothGattServerManager"
|
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
|
// Core Bluetooth components
|
||||||
@@ -223,7 +220,7 @@ class BluetoothGattServerManager(
|
|||||||
return
|
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")
|
Log.i(TAG, "Server: Received packet from ${device.address}, size: ${value.size} bytes")
|
||||||
val packet = BitchatPacket.fromBinaryData(value)
|
val packet = BitchatPacket.fromBinaryData(value)
|
||||||
if (packet != null) {
|
if (packet != null) {
|
||||||
@@ -297,7 +294,7 @@ class BluetoothGattServerManager(
|
|||||||
|
|
||||||
// Create characteristic with notification support
|
// Create characteristic with notification support
|
||||||
characteristic = BluetoothGattCharacteristic(
|
characteristic = BluetoothGattCharacteristic(
|
||||||
CHARACTERISTIC_UUID,
|
AppConstants.Mesh.Gatt.CHARACTERISTIC_UUID,
|
||||||
BluetoothGattCharacteristic.PROPERTY_READ or
|
BluetoothGattCharacteristic.PROPERTY_READ or
|
||||||
BluetoothGattCharacteristic.PROPERTY_WRITE or
|
BluetoothGattCharacteristic.PROPERTY_WRITE or
|
||||||
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE or
|
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE or
|
||||||
@@ -307,12 +304,12 @@ class BluetoothGattServerManager(
|
|||||||
)
|
)
|
||||||
|
|
||||||
val descriptor = BluetoothGattDescriptor(
|
val descriptor = BluetoothGattDescriptor(
|
||||||
DESCRIPTOR_UUID,
|
AppConstants.Mesh.Gatt.DESCRIPTOR_UUID,
|
||||||
BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE
|
BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE
|
||||||
)
|
)
|
||||||
characteristic?.addDescriptor(descriptor)
|
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)
|
service.addCharacteristic(characteristic)
|
||||||
|
|
||||||
gattServer?.addService(service)
|
gattServer?.addService(service)
|
||||||
@@ -357,7 +354,7 @@ class BluetoothGattServerManager(
|
|||||||
val settings = powerManager.getAdvertiseSettings()
|
val settings = powerManager.getAdvertiseSettings()
|
||||||
|
|
||||||
val data = AdvertiseData.Builder()
|
val data = AdvertiseData.Builder()
|
||||||
.addServiceUuid(ParcelUuid(SERVICE_UUID))
|
.addServiceUuid(ParcelUuid(AppConstants.Mesh.Gatt.SERVICE_UUID))
|
||||||
.setIncludeTxPowerLevel(false)
|
.setIncludeTxPowerLevel(false)
|
||||||
.setIncludeDeviceName(false)
|
.setIncludeDeviceName(false)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bitchat.android.util
|
package com.bitchat.android.util
|
||||||
|
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Centralized application-wide constants.
|
* Centralized application-wide constants.
|
||||||
*/
|
*/
|
||||||
@@ -22,6 +24,12 @@ object AppConstants {
|
|||||||
|
|
||||||
// GATT client RSSI updates
|
// GATT client RSSI updates
|
||||||
const val RSSI_UPDATE_INTERVAL_MS: Long = 5_000L
|
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 {
|
object Sync {
|
||||||
|
|||||||
Reference in New Issue
Block a user