با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این راهنما نحوه مسیریابی صدا برای دستگاههای بلوتوث با استفاده از Telecom API و تنظیم اتصال برای تماسهای VoIP را پوشش میدهد. قبل از ادامه، راهنمای ساخت اپلیکیشن تماس را بخوانید.
با استفاده از کلاس های ConnectionService و Connection ، می توانید به وضعیت صدا و لیستی از دستگاه های بلوتوث موجود دسترسی داشته باشید و می توانید صدا را به یک دستگاه بلوتوث انتخابی هدایت کنید.
اتصال VoIP و ConnectionService
یک کلاس VoIPConnection ایجاد کنید که از Connection گسترش می یابد. این کلاس وضعیت تماس فعلی را کنترل می کند. همانطور که راهنمای Build a calling app بیان می کند، این برنامه را به یک برنامه خود مدیریت تبدیل کنید و حالت صوتی را برای یک برنامه VoIP تنظیم کنید.
با این کلاس های Connection و ConnectionService سفارشی، می توانید کنترل کنید که کدام دستگاه و چه نوع مسیریابی صوتی را می خواهید در طول تماس استفاده کنید.
وضعیت فعلی صدا را دریافت کنید
برای دریافت وضعیت فعلی صدا، getCallAudioState() را فراخوانی کنید. getCallAudioState() برمی گردد اگر دستگاه با استفاده از بلوتوث، Earpiece، Wired یا Speaker در حال پخش جریانی باشد.
mAudioState=connection.getCallAudioState()
در وضعیت تغییر کرد
با نادیده گرفتن onCallAudioStateChanged() در تغییرات CallAudioState مشترک شوید. این به شما از هرگونه تغییر در وضعیت هشدار می دهد.
ROUTE_BLUETOOTH بدون تعیین دستگاه با استفاده از setAudioRoute(int) فعال کنید. این به طور پیشفرض مسیریابی به دستگاههای بلوتوث فعال فعلی را در Android 9 و بالاتر انجام میدهد.
setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Manage calls using the Telecom API\n\nThis guide covers how to route audio for Bluetooth devices using the\n[Telecom API](/develop/connectivity/telecom) and set the connection for\nVoIP calls. Read the\n[Build a calling app](/develop/connectivity/telecom/selfManaged) guide\nbefore continuing.\n\nBy using the [`ConnectionService`](/reference/android/telecom/ConnectionService)\nand [`Connection`](/reference/android/telecom/Connection) classes, you can access\nthe audio state and a list of available Bluetooth devices, and can route\naudio to a selected Bluetooth device.\n\nVoIP Connection and ConnectionService\n-------------------------------------\n\nCreate a `VoIPConnection` class that extends from\n[`Connection`](/reference/android/telecom/Connection). This class controls the state of the current call. As the\n[Build a calling app](/develop/connectivity/telecom/selfManaged) guide\nstates, make this a self-managed application and set the audio mode for a VoIP\napplication. \n\n### Kotlin\n\n```kotlin\nclass VoIPConnection : Connection() {\n init {\n setConnectionProperties(PROPERTY_SELF_MANAGED)\n setAudioModeIsVoip(true)\n }\n}\n```\n\n### Java\n\n```java\npublic class VoIPConnection extends Connection {\n public VoIPConnection() {\n setConnectionProperties(PROPERTY_SELF_MANAGED);\n setAudioModeIsVoip(true);\n }\n}\n```\n\nNext, return an instance of this class in\n[`ConnectionService`](/reference/android/telecom/ConnectionService) when an\nincoming or outgoing call occurs. \n\n### Kotlin\n\n```kotlin\nclass VoIPConnectionService : ConnectionService() {\n override fun onCreateOutgoingConnection(\n connectionManagerPhoneAccount: PhoneAccountHandle,\n request: ConnectionRequest,\n ): Connection {\n return VoIPConnection()\n }\n}\n```\n\n### Java\n\n```java\npublic class VoIPConnectionService extends ConnectionService {\n @Override\n public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {\n return new VoIPConnection();\n }\n}\n```\n\nEnsure the manifest correctly points to the `VoIPConnectionService` class. \n\n \u003cservice android:name=\".voip.TelegramConnectionService\" android:permission=\"android.permission.BIND_TELECOM_CONNECTION_SERVICE\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.telecom.ConnectionService\"/\u003e\n \u003c/intent-filter\u003e\n \u003c/service\u003e\n\nWith these custom [`Connection`](/reference/android/telecom/Connection) and\n[`ConnectionService`](/reference/android/telecom/ConnectionService) classes, you\ncan control which device and what type of audio routing you wish to use during a\ncall.\n\nGet the current audio state\n---------------------------\n\nTo get the current audio state, call\n[`getCallAudioState()`](/reference/android/telecom/Connection#getCallAudioState()).\n[`getCallAudioState()`](/reference/android/telecom/Connection#getCallAudioState())\nreturns if the device is streaming using Bluetooth, Earpiece, Wired, or\nSpeaker. \n\n mAudioState = connection.getCallAudioState()\n\nOn State Changed\n----------------\n\nSubscribe to changes in CallAudioState by overriding\n[`onCallAudioStateChanged()`](/reference/android/telecom/Connection#onCallAudioStateChanged(android.telecom.CallAudioState)).\nThis alerts you of any changes to the state. \n\n### Kotlin\n\n```kotlin\nfun onCallAudioStateChanged(audioState: CallAudioState) {\n mAudioState = audioState\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCallAudioStateChanged(CallAudioState audioState) {\n mAudioState = audioState;\n}\n```\n\nGet the current device\n----------------------\n\nGet the current active device using\n[`CallAudioState.getActiveBluetoothDevice()`](/reference/android/telecom/CallAudioState#getActiveBluetoothDevice()).\nThis function returns the active Bluetooth device.\n**Note:** This feature is only available in API level 28 and higher. \n\n### Kotlin\n\n```kotlin\nval activeDevice: BluetoothDevice = mAudioState.getActiveBluetoothDevice()\n```\n\n### Java\n\n```java\nBluetoothDevice activeDevice = mAudioState.getActiveBluetoothDevice();\n```\n\nGet Bluetooth devices\n---------------------\n\nGet a list of Bluetooth devices that are available for call audio routing using\n[`CallAudioState.getSupportedBluetoothDevices()`](/reference/android/telecom/CallAudioState#getSupportedBluetoothDevices()). \n\n### Kotlin\n\n```kotlin\nval availableBluetoothDevices: Collection =\n mAudioState.getSupportedBluetoothDevices()\n```\n\n### Java\n\n```java\nCollection availableBluetoothDevices = mAudioState.getSupportedBluetoothDevices();\n```\n\nRoute the call audio\n--------------------\n\n### Using API level 28 and higher (recommended)\n\nRoute the call audio to an available Bluetooth device using\n[`requestBluetoothAudio(BluetoothDevice)`](/reference/android/telecom/Connection#requestBluetoothAudio(android.bluetooth.BluetoothDevice)): \n\n requestBluetoothAudio(availableBluetoothDevices[0]);\n\n### Using API level 23 and higher\n\nEnable\n[`ROUTE_BLUETOOTH`](/reference/android/telecom/CallAudioState#ROUTE_BLUETOOTH)\nwithout specifying the device using\n[`setAudioRoute(int)`](/reference/android/telecom/Connection#setAudioRoute(int)).\nThis defaults routing to current, active bluetooth devices on Android 9 and higher. \n\n setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);\n\n| **Note:** This step isn't required if the app already knows which Bluetooth device to route audio to."]]