Connecting to a Bluetooth A2DP Device from Android

Kushangi Patel
Mindful Engineering
3 min readOct 9, 2020

For a while now, I’ve been wondering about how to connect Bluetooth Headset and stream audio from my android device with a swift easy to use program. Finally after some exhausting hours of searching I’ve found a solution!

So, as a developer it didn’t come as shock to me that I would have to learn and understand how this could be done in Android Apps. But the question that haunted and tormented me was how? It wasn’t a terrain I had crossed before but it sure was an interesting path.

But before we dive into the easy steps, there are some basic concepts worth understanding. Earlier, we had to go to all the trouble of creating the socket and establishing the connection between the Bluetooth devices.

Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles. A Bluetooth profile is a wireless interface specification for establishing Bluetooth-based communication between devices.

So, all we have to do is just add the class BluetoothA2dp that would be used to connect to an A2DP device.

What is Bluetooth A2DP?

Bluetooth A2DP(Advanced Audio Distribution Profile) is the Bluetooth Stereo profile which defines how high quality stereo audio can be streamed from one device to another over a Bluetooth connection — for example, music streamed from a mobile phone to wireless headphones.

In older Android versions the classes which are required to connect to a Bluetooth A2DP device was hidden in the API level and in the later versions from Android 4.0 onwards, it is made visible partially.

But there is a solution which will work on all the Android versions, you can easily interact with any A2DP device by simply making some IPC (Inter Process Communication) calls.

Basically, this is achieved by the use of AIDL (Android Interface Definition Language) which basically allows to define the programming interface that both the client and service agree upon in order to communicate with each other using IPC.

How to Connect a Bluetooth Device to Any Android Device?

To enable the connection to a Bluetooth device to any android device programmatically, you just need to follow these simple steps.

Step 1: Create AIDL files

Our first step is to get hold on the instance of Bluetooth A2DP class. For that, what you need to do is create 2 AIDL files first in your “src” folder of Android Project under package android.bluetooth as follows :

Step 2: Add permissions

Then in the Android Manifest, add permissions for using Bluetooth and the following services :

android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.BIND_ACCESSIBILITY_SERVICE

Step 3: Enable Bluetooth and Get Paired Devices

First, you need to ensure whether Bluetooth is enabled. To request that Bluetooth be enabled, call startActivityForResult() with the ACTION_REQUEST_ENABLE action Intent. This will issue a request to enable Bluetooth through the system settings.

val enableBtIntent = Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)

You can get the already paired devices from BluetoothAdapter class as follows:

devices = BluetoothAdapter.getDefaultAdapter().bondedDevices

From the devices you can get your headset device which you want to pair

Step 4: Establish Bluetooth Connection

Use getProfileProxy()to establish a connection to the profile proxy object associated with the profile. Set up a BluetoothProfile.ServiceListener(), This listener notifies BluetoothProfile IPC clients when they have been connected to or disconnected from the service.

Step 5: Stream Audio URL

Next step is to stream audio which you want to play on connected Bluetooth Headset. Initialize MediaPlayer and play the streaming url as below:

Step 6: Disconnect the device

Finally done with playing the audio and want to disconnect the device close proxy connection, use the following method to disconnect and release the MediaPlayer:

So that’s it! Hope you enjoyed this blog !!!

You can see the entire sample of Bluetooth A2DP device connectivity here, go & check it out.

For any queries feel free to leave a comment below. Your ideas are always welcome. 😊

--

--