> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 执行蓝牙 A2DP source 功能

> 您可以使用 ``bluetoothctl`` 菜单、``menu transport`` 子菜单和 ``paplay`` 命令执行蓝牙 A2DP source 功能。蓝牙 A2DP source 功能包括连接或断开远程设备以及列出可用的传输选项。它们允许您检索编解码器能力信息、设置绝对音量以及播放音频文件。您还可以使用蓝牙 A2DP source 功能获取可用 sink 的列表并在特定 sink 上播放音频。

## 前提条件

开始之前，请按照 <a href="verify-a2-dp-functions#set-up-the-device-for-a2dp-functions" target="_self">为 A2DP 功能设置设备</a> 中的说明设置您的设备并进入所需的菜单。

## 连接远程设备

要以 A2DP source 角色连接远程设备，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
connect <bt_address>
```

**参数**

`<bt_address>` 是远程设备的蓝牙地址。

**示例**

要连接 `<bt_address>` 为 `17:1A:35:8D:A2:A4` 的已配对远程设备，请运行以下命令：

```text theme={null}
connect 17:1A:35:8D:A2:A4
```

**示例输出**

```text theme={null}
[bluetooth]# connect 17:1A:35:8D:A2:A4
Attempting to connect to 17:1A:35:8D:A2:A4
[CHG] Device 17:1A:35:8D:A2:A4 Connected: yes
[CHG] Device 17:1A:35:8D:A2:A4 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 17:1A:35:8D:A2:A4 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 17:1A:35:8D:A2:A4 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 17:1A:35:8D:A2:A4 ServicesResolved: yes
[CHG] Device 17:1A:35:8D:A2:A4 Bonded: yes
[CHG] Device 17:1A:35:8D:A2:A4 Paired: yes
[NEW] Endpoint /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1
[NEW] Endpoint /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep2
[NEW] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0
Connection successful
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 State: active
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 Volume: 0x0040 (64)
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 State: idle
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 Volume: 0x0044 (68)
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 Volume: 0x0040 (64)
[MySoundbar]#
```

## 列出可用传输

要列出可用的传输，请在 `menu transport` 子菜单中运行以下命令：

```text theme={null}
list
```

**示例输出**

```text theme={null}
[MySoundbar]# list
Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0
```

## 获取编解码器能力信息

要获取传输的编解码器能力信息，请在 `menu transport` 子菜单中运行以下命令：

```text theme={null}
show <transport>
```

**参数**

`<transport>` 是传输路径。

**示例**

要获取 `<transport>` 为 `/org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0` 的编解码器能力信息，请运行以下命令：

```text theme={null}
show /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0
```

**示例输出**

```text theme={null}
[MySoundbar]# show /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0
Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0
      UUID: 0000110a-0000-1000-8000-00805f9b34fb
      Codec: 0x00 (0)
      Configuration: 0x21 (33)
      Configuration: 0x15 (21)
      Configuration: 0x02 (2)
      Configuration: 0x35 (53)
      Device: /org/bluez/hci0/dev_17_1A_35_8D_A2_A4
      State: idle
      Volume: 0x0040 (64)
```

## 设置绝对音量

<Note>
  请确保 DUT 和远程设备支持 `setabsvolume` 功能。
</Note>

要设置传输的绝对音量，请在 `menu transport` 子菜单中运行以下命令：

```text theme={null}
volume <transport> [value]
```

**参数**

* `<transport>` 是传输路径。

* `[value]` 是音量级别。

**示例**

要将 `<transport>` `/org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0` 的绝对音量设置为 `50`，请运行以下命令：

```text theme={null}
volume /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 50
```

**示例输出**

```text theme={null}
[MySoundbar]# volume /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 50
Changing Volume succeeded
[CHG] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 Volume: 0x0032 (50)
[MySoundbar]# volume /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd0 10
Changing Volume succeeded
```

## 播放音频文件

<Note>
  DUT 不支持音频播放器。请使用 `paplay` 命令播放音乐。

  仅支持 PCM 音频采样。
</Note>

要从 DUT 播放歌曲，请执行以下操作：

1. 运行 SSH。

2. 运行以下命令播放 WAV 文件：

   ```text theme={null}
   paplay <audio_filepath> -v
   ```

   **参数**

   `<audio_filepath>` 是 WAV 文件的文件路径。

   **示例**

   要播放位于 `/tmp/playback.wav` 的 WAV 文件，请运行以下命令：

   ```text theme={null}
   paplay /tmp/playback.wav -v
   ```

   **示例输出**

   ```text theme={null}
   sh-5.1# paplay /tmp/playback.wav -v
   Opening a playback stream with sample specification 's16le 2ch 44100Hz' and channel map 'front-left,front-right'.
   Connection established.
   Stream successfully created.
   Buffer metrics: maxlength=4194304, tlength=352800, prebuf=349276, minreq=3528
   Using sample spec 's16le 2ch 44100Hz', channel map 'front-left,front-right'.
   Connected to device bluez_output.17:1A:35:8D:A2:A4 (index: 68, suspended: no).
   Stream started.
   ```

## 获取 sink 列表

当连接了多个设备时，要获取 sink 列表，请在 SSH 中运行以下命令：

```text theme={null}
pactl list sinks short
```

<Note>
  pactl 工具默认不可用。请在您的设备上启用并构建该工具。
</Note>

**示例输出**

```text theme={null}
sh-5.1# pactl list sinks short
51      alsa_output.platform-sound.HiFi__Speaker__sink  PipeWire        s16le 2ch 48000Hz       SUSPENDED
68      bluez_output.17:1A:35:8D:A2:A4  PipeWire        float32le 2ch 48000Hz   SUSPENDED
```

## 在特定 sink 上播放音频

当连接了多个设备时，要在特定 sink 上播放音频，请在 SSH 中运行以下命令：

```text theme={null}
paplay <audio_filepath> --device=<device_name> -v
```

**参数**

* `<audio_filepath>` 是 WAV 文件的文件路径。

* `<device_name>` 是 sink 名称。

**示例**

要在 `bluez_output.17:1A:35:8D:A2:A4` sink 上播放 `pcmtest.wav`，请运行以下命令：

```text theme={null}
paplay pcmtest.wav --device=bluez_output.17:1A:35:8D:A2:A4 -v
```

**示例输出**

```text theme={null}
sh-5.1# paplay pcmtest.wav --device=bluez_output.17:1A:35:8D:A2:A4 -v
Opening a playback stream with sample specification 's16le 2ch 44100Hz' and channel map 'front-left,front-right'.
Connection established.
Stream successfully created.
Buffer metrics: maxlength=4194304, tlength=352800, prebuf=349276, minreq=3528
Using sample spec 's16le 2ch 44100Hz', channel map 'front-left,front-right'.
Connected to device bluez_output.17:1A:35:8D:A2:A4 (index: 68, suspended: no).
Stream started.
Time: 2.925 sec; Latency: 2062081 usec.
```

## 断开远程设备

要断开远程设备，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
disconnect <bt_address>
```

**参数**

`<bt_address>` 是远程设备的蓝牙地址。

**示例**

要断开 `<bt_address>` 为 `17:1A:35:8D:A2:A4` 的已配对远程设备，请运行以下命令：

```text theme={null}
disconnect 17:1A:35:8D:A2:A4
```

**示例输出**

```text theme={null}
[MySoundbar]# disconnect 17:1A:35:8D:A2:A4
Attempting to disconnect from 17:1A:35:8D:A2:A4
[DEL] Transport /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1/fd2
[DEL] Endpoint /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep1
[DEL] Endpoint /org/bluez/hci0/dev_17_1A_35_8D_A2_A4/sep2
[CHG] Device 17:1A:35:8D:A2:A4 ServicesResolved: no
Successful disconnected
[CHG] Device 17:1A:35:8D:A2:A4 Connected: no
```
