گوش دادن به زمانی که یک دستگاه ارتباطی تغییر کرده است، به برنامه شما امکان میدهد متوجه شود که مسیریابی اعمال میشود و دستگاهی که انتخاب کردهاید فعال است.
از getAvailableCommuncationDevices() استفاده کنید تا ببینید کدام دستگاه ها برای تماس VoIP در دسترس هستند. از AudioDeviceInfo استفاده کنید تا ببینید آیا دستگاه یک هدست BLE است یا خیر. این مثال به دنبال اولین دستگاهی است که از صدای BLE پشتیبانی میکند، اما ممکن است در نظر داشته باشید که همه دستگاههای پشتیبانیشده را بیابید و به کاربران اجازه انتخاب بدهید.
کاتلین
// 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 برای تنظیم دستگاهی که می خواهید به آن مسیریابی کنید، استفاده کنید. بررسی نتیجه به برنامه شما اطلاع میدهد که آیا مدیر صدا سعی دارد دستگاه را تنظیم کند یا با خطا مواجه شده است.
کاتلین
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 تنظیم شده است، هنگام برقراری تماس، جریان صوتی مسیریابی صحیح صوتی را خواهد داشت.
جلسه را تمام کنید
پس از اتمام تماس یا جلسه برنامه، دستگاه ارتباط فعال را پاک کنید. این کمک می کند تا اطمینان حاصل شود که کاربر در هنگام جابجایی بین برنامه های مختلف تجربه خوبی دارد.
کاتلین
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice()
جاوا
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice();
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-13 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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 بهوقت ساعت هماهنگ جهانی."],[],[],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();"]]