Stay organized with collections
Save and categorize content based on your preferences.
Bluetooth audio profiles based on Bluetooth Low Energy (BLE) Audio
allow bidirectional streaming of high quality audio (for example, stereo audio
with a 32 kHz sampling rate). This is possible thanks to the creation of the LE
Isochronous Channel (ISO). ISO is similar to the Synchronous Connection-Oriented
(SCO) Link because it also uses reserved wireless bandwidth, but the bandwidth
reservation is no longer capped at 64 Kbps and can be dynamically adjusted.
Bluetooth audio input can use the latest
AudioManager API for nearly all use
cases, excluding phone calls. This guide covers how to record stereo audio from
BLE Audio hearables.
Configure your application
First, configure your application to target the correct SDK in build.gradle:
targetSdkVersion 31
Register audio callback
Create an
AudioDeviceCallback
that lets your application know of any changes to connected or disconnected
AudioDevices.
To check if stereo microphones are supported on the selected device, see if the
device has two or more channels. If the device only has one channel, set the channel mask to mono.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-01-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-01-03 UTC."],[],[],null,["# Audio recording\n\nBluetooth audio profiles based on Bluetooth Low Energy (BLE) Audio\nallow bidirectional streaming of high quality audio (for example, stereo audio\nwith a 32 kHz sampling rate). This is possible thanks to the creation of the LE\nIsochronous Channel (ISO). ISO is similar to the Synchronous Connection-Oriented\n(SCO) Link because it also uses reserved wireless bandwidth, but the bandwidth\nreservation is no longer capped at 64 Kbps and can be dynamically adjusted.\n\nBluetooth audio input can use the latest\n[AudioManager API](/reference/android/media/AudioManager) for nearly all use\ncases, excluding phone calls. This guide covers how to record stereo audio from\nBLE Audio hearables.\n\nConfigure your application\n--------------------------\n\nFirst, configure your application to target the correct SDK in `build.gradle`: \n\n targetSdkVersion 31\n\nRegister audio callback\n-----------------------\n\nCreate an\n[`AudioDeviceCallback`](/reference/android/media/AudioManager#registerAudioDeviceCallback(android.media.AudioDeviceCallback,%20android.os.Handler))\nthat lets your application know of any changes to connected or disconnected\n`AudioDevices`. \n\n final AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() {\n @Override\n public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {\n };\n @Override\n public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {\n // Handle device removal\n };\n };\n\n audioManager.registerAudioDeviceCallback(audioDeviceCallback);\n\nFind BLE Audio Device\n---------------------\n\nGet a list of all connected audio devices with input supported, then use\n[`getType()`](/reference/android/media/AudioDeviceInfo#getType()) to see if\nthe device is a headset using\n[`AudioDeviceInfo.TYPE_BLE_HEADSET`](/reference/android/media/AudioDeviceInfo#TYPE_BLE_HEADSET). \n\n### Kotlin\n\n```kotlin\nval allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS)\nvar bleInputDevice: AudioDeviceInfo? = null\n for (device in allDeviceInfo) {\n if (device.type == AudioDeviceInfo.TYPE_BLE_HEADSET) {\n bleInputDevice = device\n break\n }\n }\n```\n\n### Java\n\n```java\nAudioDeviceInfo[] allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS);\nAudioDeviceInfo bleInputDevice = null;\nfor (AudioDeviceInfo device : allDeviceInfo) {\n if (device.getType() == AudioDeviceInfo.TYPE_BLE_HEADSET) {\n bleInputDevice = device;\n break;\n }\n}\n```\n\nStereo support\n--------------\n\nTo check if stereo microphones are supported on the selected device, see if the\ndevice has two or more channels. If the device only has one channel, set the channel mask to mono. \n\n### Kotlin\n\n```kotlin\nvar channelMask: Int = AudioFormat.CHANNEL_IN_MONO\nif (audioDevice.channelCounts.size \u003e= 2) {\n channelMask = AudioFormat.CHANNEL_IN_STEREO\n}\n```\n\n### Java\n\n```java\nif (bleInputDevice.getChannelCounts() \u003e= 2) {\n channelMask = AudioFormat.CHANNEL_IN_STEREO;\n};\n```\n\nSet up the audio recorder\n-------------------------\n\nAudio recorders can be set up using the standard `AudioRecord` builder.\nUse the channel mask to select stereo or mono configuration. \n\n### Kotlin\n\n```kotlin\nval recorder = AudioRecord.Builder()\n .setAudioSource(MediaRecorder.AudioSource.MIC)\n .setAudioFormat(AudioFormat.Builder()\n .setEncoding(AudioFormat.ENCODING_PCM_16BIT)\n .setSampleRate(32000)\n .setChannelMask(channelMask)\n .build())\n .setBufferSizeInBytes(2 * minBuffSizeBytes)\n .build()\n```\n\n### Java\n\n```java\nAudioRecord recorder = new AudioRecord.Builder()\n .setAudioSource(MediaRecorder.AudioSource.MIC)\n .setAudioFormat(new AudioFormat.Builder()\n .setEncoding(AudioFormat.ENCODING_PCM_16BIT)\n .setSampleRate(32000)\n .setChannelMask(channelMask)\n .build())\n .setBufferSizeInBytes(2*minBuffSizeBytes)\n .build();\n```\n\nSet preferred device\n--------------------\n\nSetting a preferred device informs the audio `recorder` which audio device\nyou wish to record with. \n\n### Kotlin\n\n```kotlin\nrecorder.preferredDevice = audioDevice\n```\n\n### Java\n\n```java\nrecorder.setPreferredDevice(bleInputDevice);\n```\n| **Note:** The user can manually override this preference in device settings.\n\nNow, you can record audio as outlined in [the MediaRecorder guide](/guide/topics/media/mediarecorder)."]]