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

Commit 0a61bcfa authored by Thomas Girardier's avatar Thomas Girardier Committed by Automerger Merge Worker
Browse files

Merge "[PTS-bot] Wait for disconnect LE callback" am: ab47d802 am: 7232eefa am: 9ecfae43

parents a9daa957 9ecfae43
Loading
Loading
Loading
Loading
+16 −9
Original line number Original line Diff line number Diff line
@@ -267,7 +267,6 @@ class Host(private val context: Context, private val server: Server) : HostImplB


      bluetoothDevice.disconnect()
      bluetoothDevice.disconnect()
      connectionStateChangedFlow.filter { it == BluetoothAdapter.STATE_DISCONNECTED }.first()
      connectionStateChangedFlow.filter { it == BluetoothAdapter.STATE_DISCONNECTED }.first()

      DisconnectResponse.getDefaultInstance()
      DisconnectResponse.getDefaultInstance()
    }
    }
  }
  }
@@ -277,9 +276,9 @@ class Host(private val context: Context, private val server: Server) : HostImplB
    responseObserver: StreamObserver<ConnectLEResponse>
    responseObserver: StreamObserver<ConnectLEResponse>
  ) {
  ) {
    grpcUnary<ConnectLEResponse>(scope, responseObserver) {
    grpcUnary<ConnectLEResponse>(scope, responseObserver) {
      val ptsAddress = request.address.decodeAsMacAddressToString()
      val address = request.address.decodeAsMacAddressToString()
      Log.i(TAG, "connect LE: $ptsAddress")
      Log.i(TAG, "connectLE: $address")
      val device = scanLeDevice(ptsAddress)
      val device = scanLeDevice(address)
      GattInstance(device!!, TRANSPORT_LE, context).waitForState(BluetoothProfile.STATE_CONNECTED)
      GattInstance(device!!, TRANSPORT_LE, context).waitForState(BluetoothProfile.STATE_CONNECTED)
      ConnectLEResponse.newBuilder()
      ConnectLEResponse.newBuilder()
        .setConnection(
        .setConnection(
@@ -291,14 +290,22 @@ class Host(private val context: Context, private val server: Server) : HostImplB


  override fun disconnectLE(request: DisconnectLERequest, responseObserver: StreamObserver<Empty>) {
  override fun disconnectLE(request: DisconnectLERequest, responseObserver: StreamObserver<Empty>) {
    grpcUnary<Empty>(scope, responseObserver) {
    grpcUnary<Empty>(scope, responseObserver) {
      val ptsAddress = request.connection.cookie.toByteArray().decodeToString()
      val address = request.connection.cookie.toByteArray().decodeToString()
      Log.i(TAG, "disconnect: $ptsAddress")
      Log.i(TAG, "disconnectLE: $address")
      GattInstance.get(ptsAddress).disconnectInstance()
      val gattInstance = GattInstance.get(address)

      if (gattInstance.isDisconnected()) {
        Log.e(TAG, "Device is not connected, cannot disconnect")
        throw Status.UNKNOWN.asException()
      }

      gattInstance.disconnectInstance()
      gattInstance.waitForState(BluetoothProfile.STATE_DISCONNECTED)
      Empty.getDefaultInstance()
      Empty.getDefaultInstance()
    }
    }
  }
  }


  private fun scanLeDevice(ptsAddress: String): BluetoothDevice? {
  private fun scanLeDevice(address: String): BluetoothDevice? {
    Log.d(TAG, "scanLeDevice")
    Log.d(TAG, "scanLeDevice")
    var bluetoothDevice: BluetoothDevice? = null
    var bluetoothDevice: BluetoothDevice? = null
    runBlocking {
    runBlocking {
@@ -313,7 +320,7 @@ class Host(private val context: Context, private val server: Server) : HostImplB
            override fun onScanResult(callbackType: Int, result: ScanResult) {
            override fun onScanResult(callbackType: Int, result: ScanResult) {
              super.onScanResult(callbackType, result)
              super.onScanResult(callbackType, result)
              val deviceAddress = result.device.address
              val deviceAddress = result.device.address
              if (deviceAddress == ptsAddress) {
              if (deviceAddress == address) {
                Log.d(TAG, "found device address: $deviceAddress")
                Log.d(TAG, "found device address: $deviceAddress")
                trySendBlocking(result.device)
                trySendBlocking(result.device)
              }
              }