Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b3c72154 authored by Charlie Boutier's avatar Charlie Boutier Committed by Gerrit Code Review
Browse files

Merge "PandoraServer: use same scope to emit service into flow" into main

parents 16ed3893 50aaafe7
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -31,9 +31,7 @@ import java.io.Closeable
import java.util.UUID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
@@ -276,18 +274,15 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
                service.addCharacteristic(characteristic)
            }

            val fullService = coroutineScope {
                val firstService = mScope.async { serverManager.newServiceFlow.first() }
            serverManager.server.addService(service)
                firstService.await()
            }
            val addedService = serverManager.serviceFlow.first()

            RegisterServiceResponse.newBuilder()
                .setService(
                    GattService.newBuilder()
                        .setHandle(fullService.instanceId)
                        .setType(fullService.type)
                        .setUuid(fullService.uuid.toString().uppercase())
                        .setHandle(addedService.instanceId)
                        .setType(addedService.type)
                        .setUuid(addedService.uuid.toString().uppercase())
                        .addAllIncludedServices(generateServicesList(service.includedServices, 1))
                        .addAllCharacteristics(generateCharacteristicsList(service.characteristics))
                        .build()
+4 −3
Original line number Diff line number Diff line
@@ -28,16 +28,17 @@ import android.util.Log
import java.util.UUID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

class GattServerManager(
    bluetoothManager: BluetoothManager,
    context: Context,
    globalScope: CoroutineScope
    scope: CoroutineScope
) {
    val TAG = "PandoraGattServerManager"

    val services = mutableMapOf<UUID, BluetoothGattService>()
    val newServiceFlow = MutableSharedFlow<BluetoothGattService>(extraBufferCapacity = 8)
    val serviceFlow = MutableSharedFlow<BluetoothGattService>()
    var negociatedMtu = -1

    val callback =
@@ -45,7 +46,7 @@ class GattServerManager(
            override fun onServiceAdded(status: Int, service: BluetoothGattService) {
                Log.i(TAG, "onServiceAdded status=$status")
                check(status == BluetoothGatt.GATT_SUCCESS)
                check(newServiceFlow.tryEmit(service))
                scope.launch { serviceFlow.emit(service) }
            }
            override fun onMtuChanged(device: BluetoothDevice, mtu: Int) {
                Log.i(TAG, "onMtuChanged mtu=$mtu")