Connecting to a Bluetooth A2DP Device from Android

Kushangi Patel
Oct 9 · 3 min read
Image for post
Image for post

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.

Image for post
Image for post

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() 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 as follows :

Step 2: Add permissions

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

Step 3: Enable Bluetooth and Get Paired Devices

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

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

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

Step 4: Establish Bluetooth Connection

Use to establish a connection to the profile proxy object associated with the profile. Set up a This listener notifies 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. 😊

Mindful Engineering

Mindinventory Tech Blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store