BT A2DP Sharing Example
Source code path: example/bt/a2dp_sharing
Supported Platforms
eh-lb525, only one earphone can be connected for audio forwarding, call forwarding is not supported
eh-lb563, only one earphone can be connected for audio forwarding, call forwarding is not supported
eh-lb567, only one earphone can be connected for audio forwarding, call forwarding is not supported
eh-lb58x
eh-lb57x
sf32lb52-lcd series
sf32lb56-lcd series, only one earphone can be connected for audio forwarding, call forwarding is not supported
sf32lb58-lcd series
Overview
This example demonstrates A2DP music sharing and HFP call relay: the device acts as a relay, connecting both a phone and an earphone simultaneously, sharing music played on the phone to the earphone, and forwarding music control commands from the earphone back to the phone. For earphones that support AVRCP absolute volume, the example also synchronizes the media volume set by the phone to the earphone.
This example also supports dual HFP HF and HFP AG roles: the device acts as HFP HF when connected to a phone, and as HFP AG when connected to a Bluetooth earphone. Once both the phone and earphone are connected to the relay device, call status, incoming call numbers, signal/battery level, and other information from the phone side can be synchronized to the earphone. HFP control requests initiated by the earphone side (such as dialing, answering, hanging up, DTMF, and call volume adjustment) can be forwarded to the phone.
Example Usage
After the example starts, Bluetooth is enabled by default with the name sifli_a2dp_transfor. It can accept connections from a phone or actively initiate connections to an earphone. The phone side is used for A2DP Sink/HFP HF connections, and the earphone side is used for A2DP Source/HFP AG connections.
Search for Bluetooth devices Use the command
a2dp_trans inquiry startto search for earphone-type Bluetooth devices. This command only reports devices whose COD Major Class is 0x000400 (Audio device). Found devices are printed in the format"device [%s] searched"and"device COD is [%d], addr is xx:xx:xx:xx:xx:xx".Connect to Bluetooth devices Use the command
a2dp_trans conn [addr]to connect, whereaddris copied from the address (xx:xx:xx:xx:xx:xx) printed above. If the address of the earphone-type Bluetooth device is already known, you can connect directly without searching.Music sharing
With only the phone connected and music playing, the relay device produces no sound.
With only the earphone connected and music playing, the earphone produces no sound.
With both phone and earphone connected and music playing, the earphone produces sound while the relay device produces no sound.
While music is being shared, disconnecting the earphone causes the relay device to produce no sound.
While music is being shared, disconnecting the phone causes the earphone to produce no sound.
While music is being shared, disconnecting the earphone and then reconnecting it causes the earphone to produce sound again.
The relay device does not reconnect to the earphone or phone by default.
Volume synchronization: when both a phone that supports absolute volume and an earphone that supports AVRCP absolute volume are connected, after the phone adjusts the media volume, the relay device synchronizes the volume value to the earphone.
HFP call relay
After both the phone and earphone are connected to the relay device, incoming calls, outgoing calls, and call status changes on the phone side are synchronized to the earphone side.
When the earphone side performs operations such as answering, hanging up, dialing, sending DTMF keys, or adjusting call volume, the relay device forwards the corresponding HFP control requests to the phone.
HFP indicator information from the phone side, including carrier service status, signal strength, battery level, roaming status, incoming call number, local phone number, and current call information, is cached and replied to the earphone.
Once the SCO call audio link is established on the phone side, the relay device attempts to establish the SCO audio link on the earphone side and relays call audio via
CONFIG_CFG_BT_VOICE_RELAY. When the SCO link on either side is disconnected, the corresponding voice relay link is closed synchronously.Typical successful connection logs are
"HFP HF connected"and"HFP AG connected". When disconnected,"HFP HF disconnected"and"HFP AG disconnected"are printed respectively.
Hardware Requirements
Before running this example, prepare:
A development board supported by this example (Supported Platforms).
A phone that supports HFP and A2DP.
A Bluetooth earphone.
Compilation and Flashing
Switch to the example project directory and run the scons command to compile:
> scons --board=eh-lb525 -j32
Switch to the example project/build_xx directory and run uart_download.bat. Select the port as prompted to download:
$ ./uart_download.bat
Uart Download
please input the serial port num:5
For detailed compilation and download steps, refer to the Quick Start guide.
Expected Results
After the example starts:
Built-in music plays without a Bluetooth connection.
Earphone-type Bluetooth devices can be searched and built-in music plays after connection.
With both phone and earphone connected, music played on the phone can be shared to the earphone through the relay device.
After HFP connections are established with both phone and earphone, call status and number information from the phone side can be synchronized to the earphone. Controls from the earphone side such as answering, hanging up, dialing, DTMF, and call volume adjustment can be forwarded to the phone.
When HFP connections are successful, the serial port prints
"HFP HF connected"and"HFP AG connected". When call status changes, logs such as"the remote phone call_status","callsetup_status", and"callheld_status"are printed.
Technical Notes
AVRCP Role Configuration
In the A2DP music sharing scenario, the relay device must dynamically set the AVRCP role based on the type of the connected peer device.
AVRCP Role Definitions
AVRCP TG (Target): The audio source device — the party that plays music and is being controlled.
AVRCP CT (Controller): The control device — the party that sends control commands.
Necessity of Dynamic Role Assignment
In the BT_NOTIFY_AVRCP_PROFILE_CONNECTED event handler, the code dynamically sets the AVRCP role based on the A2DP role:
avrcp_role = (inst->con[con_idx].cfg == AV_AUDIO_SRC) ? AVRCP_TG : AVRCP_CT;
When the device acts as A2DP Source (connected to an earphone), it is assigned AVRCP TG.
When the device acts as A2DP Sink (connected to a phone), it is assigned AVRCP CT.
If all connections are incorrectly fixed to the CT role, the following issues occur:
The AVRCP connection with the earphone cannot properly negotiate absolute volume functionality.
Volume change registration requests from the earphone will not be responded to.
Volume synchronization fails, returning a
BT_ERROR_UNSUPPORTEDerror.After the phone adjusts the volume, it cannot be forwarded to the earphone.
Troubleshooting
Reference Documentation
Update History
Version |
Date |
Release Notes |
|---|---|---|
0.0.1 |
05/2026 |
Initial version |