Skip to content

Commit

Permalink
Merge pull request #99 from Semper-Viventem/fix-background-scanner
Browse files Browse the repository at this point in the history
Fix background ble scanner
  • Loading branch information
Semper-Viventem authored Jan 22, 2024
2 parents c1081da + 593977d commit 369d6d9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {

// This version code will be applied only for F-Droid builds, for other builds version code will be generated by gradle dynamically
// For some reason F-Droid requires version code to be hardcoded in the build.gradle file
versionCode = 1705859018
versionName = "0.22.4-beta"
versionCode = 1705859020
versionName = "0.22.5-beta"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/f/cking/software/data/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Context.MODE_PRIVATE
import f.cking.software.data.database.AppDatabase
import f.cking.software.data.helpers.ActivityProvider
import f.cking.software.data.helpers.BleFiltersProvider
import f.cking.software.data.helpers.BleScannerHelper
import f.cking.software.data.helpers.IntentHelper
import f.cking.software.data.helpers.LocationProvider
Expand All @@ -24,6 +25,7 @@ class DataModule(
) {
val module = module {
single { BleScannerHelper(get(), get(), get()) }
single { BleFiltersProvider(get()) }
single { get<Context>().getSharedPreferences(sharedPreferencesName, MODE_PRIVATE) }
single { SettingsRepository(get()) }
single { AppDatabase.build(get(), appDatabaseName) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package f.cking.software.data.helpers

import android.bluetooth.le.ScanFilter
import android.os.ParcelUuid
import f.cking.software.domain.interactor.GetKnownDevicesInteractor
import f.cking.software.domain.interactor.GetAllDevicesInteractor
import f.cking.software.domain.model.ManufacturerInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class BleFiltersProvider(
private val getKnownDevicesInteractor: GetKnownDevicesInteractor,
private val getAllDevicesInteractor: GetAllDevicesInteractor,
) {

val previouslyNoticedServicesUUIDs = mutableSetOf<String>()

suspend fun getBackgroundFilters(): List<ScanFilter> {
return getKnownDevicesFilters() + getPopularServiceUUIDS() + getManufacturerFilter()
}

fun getPopularServiceUUIDS(): List<ScanFilter> {
return (popularServicesUUID + previouslyNoticedServicesUUIDs).map { uuid ->
ScanFilter.Builder()
Expand Down Expand Up @@ -39,14 +45,28 @@ class BleFiltersProvider(

}

suspend fun getBGFilters(): List<ScanFilter> {
return getKnownDevicesInteractor.execute()
.take(KNOWN_DEVICES_LIMIT) // Limit filters to fit into android scan registerer limitations
.map {
ScanFilter.Builder()
.setDeviceAddress(it.address)
.build()
}
suspend fun getKnownDevicesFilters(): List<ScanFilter> {
return withContext(Dispatchers.Default) {
val allKnownDevices = getAllDevicesInteractor.execute()

val lastSeenDevices = allKnownDevices
.sortedByDescending { it.lastDetectTimeMs }
.take(KNOWN_DEVICES_LIMIT) // Limit filters to fit into android scan registerer limitations
.map { it.address }
.toSet()

val shouldBeIncluded = allKnownDevices
.filter { it.tags.isNotEmpty() || it.favorite }
.map { it.address }
.toSet()

(lastSeenDevices + shouldBeIncluded)
.map {
ScanFilter.Builder()
.setDeviceAddress(it)
.build()
}
}
}

private object NearByData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.bluetooth.le.*
import android.content.Context
import android.os.Handler
import android.os.Looper
import f.cking.software.domain.interactor.GetKnownDevicesInteractor
import f.cking.software.domain.model.BleScanDevice
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,7 +15,7 @@ import timber.log.Timber
import java.util.*

class BleScannerHelper(
private val getKnownDevicesInteractor: GetKnownDevicesInteractor,
private val bleFiltersProvider: BleFiltersProvider,
private val appContext: Context,
private val powerModeHelper: PowerModeHelper,
) {
Expand All @@ -26,7 +25,6 @@ class BleScannerHelper(
private val handler: Handler = Handler(Looper.getMainLooper())
private val batch = hashMapOf<String, BleScanDevice>()
private var currentScanTimeMs: Long = System.currentTimeMillis()
private val bleFiltersProvider = BleFiltersProvider(getKnownDevicesInteractor)

var inProgress = MutableStateFlow(false)

Expand Down Expand Up @@ -84,10 +82,9 @@ class BleScannerHelper(
inProgress.tryEmit(true)
currentScanTimeMs = System.currentTimeMillis()

val scanFilters = if (powerModeHelper.powerMode().useRestrictedBleConfig) {
bleFiltersProvider.getBGFilters() +
bleFiltersProvider.getPopularServiceUUIDS() +
bleFiltersProvider.getManufacturerFilter()
val powerMode = powerModeHelper.powerMode()
val scanFilters = if (powerMode.useRestrictedBleConfig) {
bleFiltersProvider.getBackgroundFilters()
} else {
listOf(ScanFilter.Builder().build())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package f.cking.software.domain.interactor

import f.cking.software.data.repo.DevicesRepository
import f.cking.software.domain.model.DeviceData

class GetAllDevicesInteractor(
private val devicesRepository: DevicesRepository,
) {

suspend fun execute(): List<DeviceData> {
return devicesRepository.getDevices()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object InteractorsModule {
factory { AnalyseScanBatchInteractor(get(), get()) }
factory { ClearGarbageInteractor(get(), get(), get(), get()) }
factory { GetKnownDevicesCountInteractor(get(), get()) }
factory { GetKnownDevicesInteractor(get(), get()) }
factory { GetAllDevicesInteractor(get()) }
factory { IsKnownDeviceInteractor() }
factory { SaveScanBatchInteractor(get(), get(), get(), get()) }
factory { GetBleRecordFramesFromRawInteractor() }
Expand Down

0 comments on commit 369d6d9

Please sign in to comment.