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 Original line Diff line number Diff line
@@ -31,9 +31,7 @@ import java.io.Closeable
import java.util.UUID
import java.util.UUID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.SharingStarted
@@ -276,18 +274,15 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
                service.addCharacteristic(characteristic)
                service.addCharacteristic(characteristic)
            }
            }


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


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


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


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


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