disable server, but client works

This commit is contained in:
callebtc
2025-07-09 21:10:20 +02:00
parent 250772a693
commit 90750c2635
2 changed files with 32 additions and 35 deletions
@@ -128,7 +128,7 @@ class BluetoothConnectionManager(
try { try {
isActive = true isActive = true
setupGattServer() // setupGattServer()
// Start power manager and services // Start power manager and services
connectionScope.launch { connectionScope.launch {
@@ -804,7 +804,7 @@ class BluetoothConnectionManager(
try { try {
Log.d(TAG, "Attempting GATT connection to $deviceAddress with autoConnect=false") Log.d(TAG, "Attempting GATT connection to $deviceAddress with autoConnect=false")
val gatt = device.connectGatt(context, false, gattCallback) val gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
if (gatt == null) { if (gatt == null) {
Log.e(TAG, "connectGatt returned null for $deviceAddress") Log.e(TAG, "connectGatt returned null for $deviceAddress")
pendingConnections.remove(deviceAddress) pendingConnections.remove(deviceAddress)
@@ -112,41 +112,38 @@ class PowerManager(private val context: Context) {
/** /**
* Get scan settings optimized for current power mode * Get scan settings optimized for current power mode
*/ */
fun getScanSettings(): ScanSettings { fun getScanSettings(): ScanSettings {
return when (currentMode) { // CRITICAL FIX: Set reportDelay to 0 for all modes.
PowerMode.PERFORMANCE -> ScanSettings.Builder() // When using a custom duty cycle, we want scan results delivered immediately,
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // not batched. A non-zero report delay can conflict with the scan window,
// causing missed results if the scan stops before the delay is met.
val builder = ScanSettings.Builder()
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT) when (currentMode) {
.setReportDelay(50) // Reduced from 10ms PowerMode.PERFORMANCE -> builder
.build() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
PowerMode.BALANCED -> ScanSettings.Builder() .setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT)
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) PowerMode.BALANCED -> builder
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE) .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT) .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
.setReportDelay(200) .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.build()
PowerMode.POWER_SAVER -> builder
PowerMode.POWER_SAVER -> ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) .setMatchMode(ScanSettings.MATCH_MODE_STICKY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.setMatchMode(ScanSettings.MATCH_MODE_STICKY)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT) PowerMode.ULTRA_LOW_POWER -> builder
.setReportDelay(1000) .setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC)
.build() .setMatchMode(ScanSettings.MATCH_MODE_STICKY)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
PowerMode.ULTRA_LOW_POWER -> ScanSettings.Builder() }
.setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) return builder.setReportDelay(0).build()
.setMatchMode(ScanSettings.MATCH_MODE_STICKY)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.setReportDelay(5000)
.build()
} }
}
/** /**
* Get advertising settings optimized for current power mode * Get advertising settings optimized for current power mode