Telecom API를 사용하여 통화 관리

이 가이드에서는 Telecom API를 사용하여 블루투스 기기의 오디오를 라우팅하고 VoIP 통화 연결을 설정하는 방법을 설명합니다. 계속하기 전에 통화 앱 빌드 가이드를 읽어보세요.

ConnectionService 및 Connection 클래스를 사용하면 오디오 상태와 사용 가능한 블루투스 기기 목록에 액세스하고 선택한 블루투스 기기로 오디오를 라우팅할 수 있습니다.

VoIP 연결 및 ConnectionService

Connection에서 확장되는 VoIPConnection 클래스를 만듭니다. 이 클래스는 현재 통화의 상태를 제어합니다. 통화 앱 빌드 가이드에 설명된 대로 이 애플리케이션을 자체 관리 애플리케이션으로 만들고 VoIP 애플리케이션의 오디오 모드를 설정합니다.

Kotlin

class VoIPConnection : Connection() {
  init {
    setConnectionProperties(PROPERTY_SELF_MANAGED)
    setAudioModeIsVoip(true)
  }
}

자바

public class VoIPConnection extends Connection {
  public VoIPConnection() {
    setConnectionProperties(PROPERTY_SELF_MANAGED);
    setAudioModeIsVoip(true);
  }
}

다음으로, 수신 전화 또는 발신 전화가 발생할 때 ConnectionService에서 이 클래스의 인스턴스를 반환합니다.

Kotlin

class VoIPConnectionService : ConnectionService() {
  override fun onCreateOutgoingConnection(
    connectionManagerPhoneAccount: PhoneAccountHandle,
    request: ConnectionRequest,
  ): Connection {
    return VoIPConnection()
  }
}

자바

public class VoIPConnectionService extends ConnectionService {
  @Override
  public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    return new VoIPConnection();
  }
}

매니페스트가 VoIPConnectionService 클래스를 올바르게 가리키는지 확인합니다.

<service android:name=".voip.TelegramConnectionService" android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
  <intent-filter>
    <action android:name="android.telecom.ConnectionService"/>
  </intent-filter>
</service>

이러한 맞춤 Connection 및 ConnectionService 클래스를 사용하면 통화 중에 사용할 기기와 오디오 라우팅 유형을 제어할 수 있습니다.

현재 오디오 상태 가져오기

현재 오디오 상태를 가져오려면 getCallAudioState를 호출합니다. getCallAudioState는 기기가 블루투스, 스피커, 유선 헤드셋 또는 스피커를 사용하여 스트리밍 중인지 여부를 반환합니다.

mAudioState = connection.getCallAudioState()

상태 변경 시

onCallAudioStateChanged()를 재정의하여 CallAudioState의 변경사항을 구독합니다. 이렇게 하면 상태 변경사항이 알림으로 전송됩니다.

Kotlin

fun onCallAudioStateChanged(audioState: CallAudioState) {
  mAudioState = audioState
}

자바

@Override
public void onCallAudioStateChanged(CallAudioState audioState) {
  mAudioState = audioState;
}

현재 기기 가져오기

CallAudioState.getActiveBluetoothDevice()를 사용하여 현재 활성 기기를 가져옵니다. 이 함수는 활성 블루투스 기기를 반환합니다.

Kotlin

val activeDevice: BluetoothDevice = mAudioState.getActiveBluetoothDevice()

자바

BluetoothDevice activeDevice = mAudioState.getActiveBluetoothDevice();

블루투스 기기 가져오기

CallAudioState.getSupportedBluetoothDevices()를 사용하여 통화 오디오 라우팅에 사용할 수 있는 블루투스 기기 목록을 가져옵니다.

Kotlin

val availableBluetoothDevices: Collection<BluetoothDevice> =
  mAudioState.getSupportedBluetoothDevices()

자바

Collection<BluetoothDevice> availableBluetoothDevices = mAudioState.getSupportedBluetoothDevices();

통화 오디오 라우팅

requestBluetoothAudio(BluetoothDevice)를 사용하여 사용 가능한 블루투스 기기로 통화 오디오를 라우팅합니다.

requestBluetoothAudio(availableBluetoothDevices[0]);

API 수준 23 이상 사용

setAudioRoute(int)를 사용하여 기기를 지정하지 않고 ROUTE_BLUETOOTH를 사용 설정합니다. 이렇게 하면 Android 9 이상에서 현재 활성 상태인 블루투스 기기로 라우팅이 기본 설정됩니다.

setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);