getAvailableCommuncationDevices()를 사용하여 VoIP 통화에 사용할 수 있는 기기를 확인합니다. AudioDeviceInfo를 사용하여 기기가 BLE 헤드셋인지 확인합니다. 이 예에서는 BLE 오디오를 지원하는 첫 번째 기기를 찾지만 지원되는 모든 기기를 찾아 사용자가 선택하도록 허용할 수도 있습니다.
Kotlin
// Get list of currently available devices
val devices = audioManager.availableCommunicationDevices
// User choose one of the devices, let's say, TYPE_BLE_HEADSET
val userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET
//for the device from the list
var selectedDevice: AudioDeviceInfo? = null
for (device in devices) {
if (device.type == userSelectedDeviceType) {
selectedDevice = device
break
}
}
자바
// Get list of currently available devices
List<AudioDeviceInfo> devices = audioManager.getAvailableCommunicationDevices();
// User choose one of the devices, for example, TYPE_BLE_HEADSET
int userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET;
// Filter for the device from the list
AudioDeviceInfo selectedDevice = null;
for (AudioDeviceInfo device : devices) {
if (device.getType() == userSelectedDeviceType) {
selectedDevice = device;
break;
}
}
통신 기기 설정
호환되는 기기를 찾으면 setCommunicationDevice를 사용하여 라우팅할 기기를 설정합니다. 결과를 확인하면 오디오 관리자가 기기를 설정하려고 하는지 또는 오류가 발생했는지 앱에 알립니다.
Kotlin
val result = audioManager.setCommunicationDevice(selectedDevice)
if (!result) {
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)
자바
boolean result = audioManager.setCommunicationDevice(selectedDevice);
if (!result) {
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)
이제 BLE 오디오 기기가 설정되었으므로 전화를 걸 때 오디오 스트림의 오디오 라우팅이 올바르게 설정됩니다.
세션 종료
앱에서 통화 또는 세션을 완료하면 활성 통신 기기를 삭제합니다. 이렇게 하면 사용자가 여러 애플리케이션 간에 이동할 때 우수한 환경을 경험할 수 있습니다.
Kotlin
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice()
Java
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice();
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-13(UTC)
[[["이해하기 쉬움","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-08-13(UTC)"],[],[],null,["# Audio Manager self-managed call guide\n\nThis guide covers communication applications, such as Voice over Internet Protocol (VoIP), that want to self-manage their audio and hearable device state.\n| **Note:** Starting in Android 13 (API level 33), applications must migrate from [`AudioManager#startBluetoothSco()`](/reference/android/media/AudioManager#startBluetoothSco()) to [`AudioManager#setCommunicationDevice()`](/reference/android/media/AudioManager#setCommunicationDevice(android.media.AudioDeviceInfo)) to support upcoming BLE audio headsets. This change is backward-compatible with Hands-Free Profile (HFP) devices.\n\nRegister audio callback\n-----------------------\n\nFirst, create an `AudioDeviceCallback`, which notifies your app when audio devices connect or disconnect from the device. \n\n### Kotlin\n\n```kotlin\nval audioDeviceCallback: AudioDeviceCallback = object : AudioDeviceCallback() {\n override fun onAudioDevicesAdded(addedDevices: Array) {}\n override fun onAudioDevicesRemoved(removedDevices: Array) {}\n}\n\naudioManager.registerAudioDeviceCallback(audioDeviceCallback)\n```\n\n### Java\n\n```java\nfinal AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() {\n @Override\n public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {\n }\n\n @Override\n public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {\n // Handle device removal\n }\n};\n\naudioManager.registerAudioDeviceCallback(audioDeviceCallback);\n```\n\nCheck for active communication device\n-------------------------------------\n\nThe audio manager lets you know which communication device is currently active. \n\n### Kotlin\n\n```kotlin\nval currentCommunicationDevice: AudioDeviceInfo? = audioManager.communicationDevice\n```\n\n### Java\n\n```java\nAudioDeviceInfo currentCommunicationDevice = audioManager.getCommunicationDevice();\n```\n\nListening for when a communication device has changed lets your app know when routing is applied and the device that you selected is active. \n\n### Kotlin\n\n```kotlin\nval listener =\n OnCommunicationDeviceChangedListener { device -\u003e // Handle changes\n currentCommunicationDevice = device\n }\naudioManager.addOnCommunicationDeviceChangedListener(executor, listener)\n```\n\n### Java\n\n```java\nAudioManager.OnCommunicationDeviceChangedListener listener = \n new AudioManager.OnCommunicationDeviceChangedListener() {\n @Override\n void onCommunicationDeviceChanged(AudioDeviceInfo device) {\n // Handle changes\n currentCommunicationDevice = device;\n }\n};\nAudioManager.addOnCommunicationDeviceChangedListener(executor, listener);\n```\n\nFind BLE audio device\n---------------------\n\nUse [`getAvailableCommuncationDevices()`](/reference/android/media/AudioManager#getAvailableCommunicationDevices()) to see which devices are available for a VoIP call. Use `AudioDeviceInfo` to see if the device is a BLE headset. This example looks for the first device that supports BLE Audio, but you might also consider finding all supported devices and allowing users to choose. \n\n### Kotlin\n\n\n // Get list of currently available devices\n val devices = audioManager.availableCommunicationDevices\n\n // User choose one of the devices, let's say, TYPE_BLE_HEADSET\n val userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET\n\n //for the device from the list\n var selectedDevice: AudioDeviceInfo? = null\n for (device in devices) {\n if (device.type == userSelectedDeviceType) {\n selectedDevice = device\n break\n }\n }\n\n### Java\n\n\n // Get list of currently available devices\n List\u003cAudioDeviceInfo\u003e devices = audioManager.getAvailableCommunicationDevices();\n\n // User choose one of the devices, for example, TYPE_BLE_HEADSET\n int userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET;\n\n // Filter for the device from the list\n AudioDeviceInfo selectedDevice = null;\n for (AudioDeviceInfo device : devices) {\n if (device.getType() == userSelectedDeviceType) {\n selectedDevice = device;\n break;\n }\n }\n\nSet communication device\n------------------------\n\nAfter you find a compatible device, use `setCommunicationDevice` to set the device you wish to route to. Checking the result informs your app if the audio manager is trying to set the device or if it encountered an error. \n\n### Kotlin\n\n\n val result = audioManager.setCommunicationDevice(selectedDevice)\n if (!result) {\n // Handle error.\n }\n // Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)\n\n### Java\n\n\n boolean result = audioManager.setCommunicationDevice(selectedDevice);\n if (!result) {\n // Handle error.\n }\n // Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)\n\n| **Note:** Once you set a communication device, wait a maximum of 30 seconds. Unless an error occurs, `AudioDeviceCallback` is called within 30 seconds. If an error occurs, call `clearCommuncationDevice()`, and then call `setCommunicationDevice()`.\n\nNow that the BLE Audio device has been set, when placing a call, the audio stream will have the correct audio routing.\n\nEnd the session\n---------------\n\nAfter your app finishes a call or session, clear the active communication device. This helps ensure the user has a great experience when moving between different applications. \n\n### Kotlin\n\n\n // Clear the selection, ready for next call\n audioManager.clearCommunicationDevice()\n\n### Java\n\n\n // Clear the selection, ready for next call\n audioManager.clearCommunicationDevice();"]]