Quản lý cuộc gọi bằng Telecom API

Hướng dẫn này trình bày cách định tuyến âm thanh cho thiết bị Bluetooth bằng Telecom API và thiết lập kết nối cho cuộc gọi VoIP. Hãy đọc hướng dẫn Xây dựng ứng dụng gọi điện trước khi tiếp tục.

Bằng cách sử dụng các lớp ConnectionService và Connection, bạn có thể truy cập vào trạng thái âm thanh và danh sách các thiết bị Bluetooth có sẵn, đồng thời có thể định tuyến âm thanh đến một thiết bị Bluetooth đã chọn.

Kết nối VoIP và ConnectionService

Tạo một lớp VoIPConnection mở rộng từ Connection. Lớp này kiểm soát trạng thái của cuộc gọi hiện tại. Như hướng dẫn Xây dựng ứng dụng gọi điện đã nêu, hãy đặt ứng dụng này thành ứng dụng tự quản lý và đặt chế độ phát âm thanh cho ứng dụng VoIP.

Kotlin

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

Java

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

Tiếp theo, hãy trả về một thực thể của lớp này trong ConnectionService khi có cuộc gọi đến hoặc cuộc gọi đi.

Kotlin

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

Java

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

Đảm bảo tệp kê khai trỏ chính xác đến lớp 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>

Với các lớp Connection và ConnectionService tuỳ chỉnh này, bạn có thể kiểm soát thiết bị và loại định tuyến âm thanh mà bạn muốn sử dụng trong cuộc gọi.

Lấy trạng thái âm thanh hiện tại

Để biết trạng thái âm thanh hiện tại, hãy gọi getCallAudioState. getCallAudioState trả về thông tin cho biết thiết bị có đang phát trực tuyến bằng Bluetooth, loa thoại, tai nghe có dây hay loa hay không.

mAudioState = connection.getCallAudioState()

Khi trạng thái thay đổi

Đăng ký nhận thông báo về các thay đổi trong CallAudioState bằng cách ghi đè onCallAudioStateChanged(). Thao tác này sẽ cảnh báo bạn về mọi thay đổi đối với trạng thái.

Kotlin

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

Java

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

Lấy thiết bị hiện tại

Lấy thiết bị đang hoạt động bằng cách sử dụng CallAudioState.getActiveBluetoothDevice(). Hàm này trả về thiết bị Bluetooth đang hoạt động.

Kotlin

val activeDevice: BluetoothDevice = mAudioState.getActiveBluetoothDevice()

Java

BluetoothDevice activeDevice = mAudioState.getActiveBluetoothDevice();

Lấy thiết bị Bluetooth

Lấy danh sách các thiết bị Bluetooth có thể dùng để định tuyến âm thanh cuộc gọi bằng cách sử dụng CallAudioState.getSupportedBluetoothDevices().

Kotlin

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

Java

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

Định tuyến âm thanh cuộc gọi

Định tuyến âm thanh cuộc gọi đến một thiết bị Bluetooth có sẵn bằng cách sử dụng requestBluetoothAudio(BluetoothDevice):

requestBluetoothAudio(availableBluetoothDevices[0]);

Sử dụng cấp độ API 23 trở lên

Bật ROUTE_BLUETOOTH mà không chỉ định thiết bị bằng cách sử dụng setAudioRoute(int). Thao tác này sẽ định tuyến mặc định đến các thiết bị Bluetooth đang hoạt động trên Android 9 trở lên.

setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);