คู่มือนี้ครอบคลุมวิธีกำหนดเส้นทางเสียงสำหรับอุปกรณ์บลูทูธโดยใช้
Telecom API และตั้งค่าการเชื่อมต่อสำหรับ
โทร VoIP อ่าน
คู่มือสร้างแอปการโทร
ก่อนดำเนินการต่อ
[[["เข้าใจง่าย","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-27 UTC"],[],[],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."]]