-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New compatibility option for receiving BLE data (#82)
* Improve compat options: Add alternative method for receiving BLE scan results via PendingIntents * Add missing logtags * Reduce log spam * Make the linter happy
- Loading branch information
Showing
11 changed files
with
236 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app-common/src/main/java/eu/darken/capod/common/bluetooth/BleScanResultForwarder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package eu.darken.capod.common.bluetooth | ||
|
||
import android.bluetooth.le.ScanResult | ||
import eu.darken.capod.common.debug.logging.Logging.Priority.VERBOSE | ||
import eu.darken.capod.common.debug.logging.Logging.Priority.WARN | ||
import eu.darken.capod.common.debug.logging.log | ||
import eu.darken.capod.common.debug.logging.logTag | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class BleScanResultForwarder @Inject constructor() { | ||
|
||
private val forwarder = MutableSharedFlow<Collection<ScanResult>>( | ||
replay = 0, | ||
extraBufferCapacity = 128, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) | ||
val results: Flow<Collection<ScanResult>> = forwarder | ||
|
||
fun forward(scanResults: Collection<ScanResult>) { | ||
log(TAG, VERBOSE) { "forward($scanResults)" } | ||
val success = forwarder.tryEmit(scanResults) | ||
if (!success) log(TAG, WARN) { "Failed to forward (overflow?) $scanResults" } | ||
} | ||
|
||
companion object { | ||
private val TAG = logTag("Bluetooth", "BleScanner", "Forwarder") | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
app-common/src/main/java/eu/darken/capod/common/bluetooth/BleScanResultReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eu.darken.capod.common.bluetooth | ||
|
||
import android.bluetooth.le.BluetoothLeScanner | ||
import android.bluetooth.le.ScanResult | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import eu.darken.capod.common.coroutine.AppScope | ||
import eu.darken.capod.common.debug.logging.Logging.Priority.VERBOSE | ||
import eu.darken.capod.common.debug.logging.Logging.Priority.WARN | ||
import eu.darken.capod.common.debug.logging.log | ||
import eu.darken.capod.common.debug.logging.logTag | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class BleScanResultReceiver : BroadcastReceiver() { | ||
|
||
@Inject @AppScope lateinit var appScope: CoroutineScope | ||
@Inject lateinit var scanResultForwarder: BleScanResultForwarder | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
log(TAG, VERBOSE) { "onReceive($context, $intent)" } | ||
if (intent.action != ACTION) { | ||
log(TAG, WARN) { "Unknown action: ${intent.action}" } | ||
return | ||
} | ||
if (intent.extras == null) { | ||
log(TAG) { "Extras are null!" } | ||
return | ||
} | ||
|
||
val errorCode = intent.getIntExtra(BluetoothLeScanner.EXTRA_ERROR_CODE, 0) | ||
log(TAG, VERBOSE) { "errorCode=$errorCode" } | ||
if (errorCode != 0) { | ||
log(TAG, WARN) { "ScanCallback error code: $errorCode" } | ||
return | ||
} | ||
|
||
val callbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1) | ||
log(TAG, VERBOSE) { "callbackType=$callbackType" } | ||
|
||
val scanResults = intent.getParcelableArrayListExtra<ScanResult>(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT) | ||
log(TAG, VERBOSE) { "scanResults=$scanResults" } | ||
|
||
if (scanResults == null) { | ||
log(TAG) { "Scan results were empty!" } | ||
return | ||
} | ||
|
||
val pending = goAsync() | ||
appScope.launch { | ||
scanResultForwarder.forward(scanResults) | ||
pending.finish() | ||
} | ||
} | ||
|
||
companion object { | ||
private val TAG = logTag("Bluetooth", "BleScanner", "Forwarder", "Receiver") | ||
const val ACTION = "eu.darken.capod.bluetooth.DELIVER_SCAN_RESULTS" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.