Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Modify Android's Bluetooth Stack to Enable A2dp Sink

I'm working on an audio recorder app that uses a bluetooth mic to record audio on to an Android device (Nexus 7 - rooted Android 4.4.2). It's currently implemented on HFP and everything is working fine. The bluetooth mic is implemented with Bluegiga's WT32 bluetooth module + a mic input, audio quality via HFP isn't great but it's sufficient for now.

However, I'm now trying to change the bluetooth profile to A2dp, since there are two mic inputs (L/R) and WT32 supports A2dp (source). After much research I found that stock Android doesn't support A2dp (sink), and it's possible to modify Android's bluetooth stack to enable A2dp (sink).

What I don't understand is how does one access and modify the bluetooth stack. It would be nice if someone with an answer is able to break-down the steps to achieve this.

I've tried following the answer to this question: Receive audio via Bluetooth in Android, yet I can't seem to find the appropriate file to modify. Actually, I don't even know if I'm looking into the right folder. I've looked through the devices file via Android-studio's DDMS-File Explorer.

ps, I'm still fairly new with Android app development, so I may have misused some of the terminologies and I apologies in-advance for that.

like image 360
Eu-Lee Avatar asked Jan 14 '14 01:01

Eu-Lee


People also ask

How do I set up A2DP?

Open Windows 10 Settings > Devices > Bluetooth & other devices. Click on 'Add Bluetooth or other device' and select 'Bluetooth'. Choose the phone and follow additional instructions, and pair the device. Select Done and your phone will appear in the Bluetooth & other devices page.

What is A2DP sink access?

A2DP Sink – A device is the sink when it acts as a sink of a digital audio stream delivered from the SOURCE on the same piconet. A2DP defines the protocols and procedures that realize distribution of audio content of high-quality in mono or stereo on ACL channels.

What is A2DP service android?

In Android 8.0, devices that use the Advanced Audio Distribution Profile (A2DP) can support additional audio codecs. The Bluetooth stack supports audio codec negotiation for when devices connect to a remote audio sink.


2 Answers

So the above answer isn't totally correct.

The following is how it breaks down:

HAL, is the hardware abstraction layer that implements the actual Bluetooth state machines in c/cpp code, as such it controls the various state machines for A2dp, HFP, GATT, SPP, AVRCP, etc. services. Each of these services also reference SMP and ATT files for controlling the actual Bluetooth server or client databases, and there security.

HCI, is where the actual work gets done. the HAL doesn't really do anything, it assembles the complex data messages that get sent along a tty serial (either spi, or UART) to an inter-network connected chip on the PCBA via the methods used in the HCI layer, which can be found in the "BTE" layer in /external/bluetooth/bluedroid/ directory of an android compiling trunk from AOSP 4.2.2 to current. - currently there are several manufacturers of these chips but they are mostly all Broadcom based ic's packaged in a dual, or triple radio package that contains a wifi, Bluetooth 4.0 Smart, and Bluetooth 4.0 radio.

It is possible to do what you are trying to do, but you would need to include hardware.so, and bluetooth_jni.so into an NDK/JNI package/project that goes with your apps, and registers via the calls from the .cpp files for each of the Bluetooth services found in "Packages/apps/Bluetooth/jni", you would then handle the registration in your NDK library of 'com_android_bluetooth_a2dp.cpp', and 'com_android_bluetooth_avrcp.cpp', as their appropriately typed objects.

The other issue is, you will need to implement your own custom A2DP stack, as the Android Bluedroid stack only has bit's and piece's of the Sink role implemented in the frameworks while the A2DP role has a full implementation of Source role. Additionally, depending what you actually intend to do with your Bluetooth A2DP sink implementation you will need to implement AVRCP as well - as per the Bluetooth SIG (special interest group), there are inter-connectivity requirements between Bluetooth devices that will lead to major issues if you implement sink role, without AVRCP "remote control target device" and "remote control control device", as the sink role ATT commands from Bluetooth over A2DP (or any Bluetooth service/profile) execute certain handshakes during the service discovery process, when the associated gateway ( the connecting device ), executes a capabilities request the A2DP service is expected to implement i/o capabilities for Start Stop commands, and possibly skip/track advance commands.

Additional to all of this, when implementing A2DP you will need to choose whether you will be handling PCM streams or AAC streams. If you are handling AAC streams (or DRM protected PCM streams for that matter, which anything like Pandora, spotify, etc. uses), you need to implement the SBC Encoder or Decoder appropriate to you implementation, else all you will have is a bunch of encrypted data. Also, be sure to implement the bitrate at the appropriate speed for your devices AudioManager implementation, some phones use 48,000hZ and some us 44,100hZ, this is important if you want high quality audio as generally most PCM A2DP implementations that are utilizing Surround Sound 7.1+ will require 48,000hZ as well as AAC encoding/decoding.

I hope this provides you with some insight.

like image 170
Zach Avatar answered Oct 16 '22 21:10

Zach


https://android-review.googlesource.com/#/c/98161/ implements A2DP Sink. It works on Nexus 5. You can try it.

like image 43
Wenjie Gong Avatar answered Oct 16 '22 20:10

Wenjie Gong