ניהול שיחות באמצעות Telecom API

במדריך הזה מוסבר איך לנתב אודיו למכשירי Bluetooth באמצעות Telecom API ולהגדיר את החיבור לשיחות VoIP. לפני שממשיכים, קראו את המדריך פיתוח אפליקציית שיחות.

באמצעות המחלקות ConnectionService ו-Connection, אתם יכולים לגשת למצב האודיו ולרשימה של מכשירי Bluetooth זמינים, ולנתב אודיו למכשיר Bluetooth נבחר.

חיבור VoIP ו-ConnectionService

יוצרים מחלקה מסוג VoIPConnection שמרחיבה את Connection. המחלקה הזו שולטת במצב של השיחה הנוכחית. כמו שמוסבר במדריך Build a calling app, צריך להגדיר את האפליקציה כאפליקציה בניהול עצמי ולהגדיר את מצב האודיו לאפליקציית 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);
  }
}

בשלב הבא, מחזירים מופע של המחלקה הזו ב-ConnectionService כשמתרחשת שיחה נכנסת או שיחה יוצאת.

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();
  }
}

מוודאים שהמניפסט מצביע בצורה נכונה על המחלקה 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 מחזירה את המידע אם המכשיר משדר באמצעות Bluetooth, אוזנייה, אוזניות חוטיות או רמקול.

mAudioState = connection.getCallAudioState()

כשהמצב משתנה

נרשמים לשינויים ב-CallAudioState על ידי שינוי של onCallAudioStateChanged(). כך תקבלו התראה על כל שינוי במצב.

Kotlin

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

Java

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

איך מקבלים את המכשיר הנוכחי

מאתרים את המכשיר הפעיל הנוכחי באמצעות CallAudioState.getActiveBluetoothDevice(). הפונקציה הזו מחזירה את מכשיר ה-Bluetooth הפעיל.

Kotlin

val activeDevice: BluetoothDevice = mAudioState.getActiveBluetoothDevice()

Java

BluetoothDevice activeDevice = mAudioState.getActiveBluetoothDevice();

קבלת מכשירי Bluetooth

כדי לקבל רשימה של מכשירי Bluetooth שזמינים לניתוב אודיו של שיחות, משתמשים ב-CallAudioState.getSupportedBluetoothDevices().

Kotlin

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

Java

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

ניתוב האודיו של השיחה

כדי להעביר את האודיו של השיחה למכשיר Bluetooth זמין, משתמשים ב-requestBluetoothAudio(BluetoothDevice):

requestBluetoothAudio(availableBluetoothDevices[0]);

שימוש ב-API ברמה 23 ומעלה

מפעילים את ROUTE_BLUETOOTH בלי לציין את המכשיר באמצעות setAudioRoute(int). כברירת מחדל, ב-Android מגרסה 9 ואילך, השיחות מנותבות למכשירי Bluetooth פעילים.

setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);