Repository navigation
Expand file tree
/
Copy pathBLEAudio.cpp
More file actions
261 lines (217 loc) · 7.43 KB
/
Copy pathBLEAudio.cpp
File metadata and controls
261 lines (217 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* @file BLEAudio.cpp
* @brief 蓝牙音频控制接口实现
*/
#include "BLEAudio.h"
#include "AudioPlayer.h"
#include "bluetooth.h"
#include "conn.h"
#include "gatt.h"
#include "uuid.h"
#include "stdio.h"
// 全局变量
static uint8_t current_volume = 50; // 默认音量50%
static bool is_playing = false; // 默认暂停状态
// 服务UUID - 使用128位UUID
static struct bt_uuid_128 audio_svc_uuid = BT_UUID_INIT_128(AUDIO_SERVICE_UUID_BASE);
// 特征UUID - 使用128位UUID
static struct bt_uuid_128 volume_char_uuid = BT_UUID_INIT_128(VOLUME_CHAR_UUID_BASE);
static struct bt_uuid_128 play_pause_char_uuid = BT_UUID_INIT_128(PLAY_PAUSE_CHAR_UUID_BASE);
static struct bt_uuid_128 track_control_char_uuid = BT_UUID_INIT_128(TRACK_CONTROL_CHAR_UUID_BASE);
// 特征值缓冲区
static uint8_t volume_value = 50;
static uint8_t play_pause_value = 0;
static uint8_t track_control_value = 0;
// 音量特征回调
static ssize_t read_volume(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
return bt_gatt_attr_read(conn, attr, buf, len, offset, &volume_value, sizeof(volume_value));
}
static ssize_t write_volume(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
if (offset > 0 || len != 1) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
uint8_t value = *(uint8_t *)buf;
// 限制音量范围0-100
if (value > 100) {
value = 100;
}
volume_value = value;
current_volume = value;
// 调用音频播放器设置音量
if (audioPlayer != NULL) {
audioPlayer->setVolume(value);
}
printf("BLE: Volume set to %d%%\n", value);
return len;
}
// 播放/暂停特征回调
static ssize_t read_play_pause(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
return bt_gatt_attr_read(conn, attr, buf, len, offset, &play_pause_value, sizeof(play_pause_value));
}
static ssize_t write_play_pause(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
if (offset > 0 || len != 1) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
uint8_t value = *(uint8_t *)buf;
play_pause_value = value;
if (audioPlayer != NULL) {
if (value == 0x01) { // 播放
if (!is_playing) {
audioPlayer->resume();
is_playing = true;
printf("BLE: Play command received\n");
}
} else if (value == 0x00) { // 暂停
if (is_playing) {
audioPlayer->pause();
is_playing = false;
printf("BLE: Pause command received\n");
}
}
}
return len;
}
// 切歌特征回调
static ssize_t read_track_control(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
return bt_gatt_attr_read(conn, attr, buf, len, offset, &track_control_value, sizeof(track_control_value));
}
static ssize_t write_track_control(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
if (offset > 0 || len != 1) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
uint8_t value = *(uint8_t *)buf;
track_control_value = value;
if (audioPlayer != NULL) {
if (value == 0x01) { // 下一首
audioPlayer->nextTrack();
printf("BLE: Next track command received\n");
} else if (value == 0x02) { // 上一首
audioPlayer->prevTrack();
printf("BLE: Previous track command received\n");
}
}
return len;
}
// 定义GATT服务
BT_GATT_SERVICE_DEFINE(audio_svc,
BT_GATT_PRIMARY_SERVICE(&audio_svc_uuid),
// 音量特征
BT_GATT_CHARACTERISTIC(&volume_char_uuid.uuid,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_volume, write_volume, &volume_value),
// 播放/暂停特征
BT_GATT_CHARACTERISTIC(&play_pause_char_uuid.uuid,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_play_pause, write_play_pause, &play_pause_value),
// 切歌特征
BT_GATT_CHARACTERISTIC(&track_control_char_uuid.uuid,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_track_control, write_track_control, &track_control_value),
);
// 初始化蓝牙音频控制
void ble_audio_init(void)
{
// 初始化默认值
current_volume = 50;
is_playing = false;
printf("BLE Audio Control initialized\n");
}
// 处理特征写入回调
void ble_audio_handle_volume(uint8_t volume)
{
current_volume = volume;
if (audioPlayer != NULL) {
audioPlayer->setVolume(volume);
}
}
void ble_audio_handle_play_pause(uint8_t command)
{
if (audioPlayer != NULL) {
if (command == 0x01) { // 播放
audioPlayer->resume();
is_playing = true;
} else if (command == 0x00) { // 暂停
audioPlayer->pause();
is_playing = false;
}
}
}
void ble_audio_handle_track_control(uint8_t command)
{
if (audioPlayer != NULL) {
if (command == 0x01) { // 下一首
audioPlayer->nextTrack();
} else if (command == 0x02) { // 上一首
audioPlayer->prevTrack();
}
}
}
// 获取当前状态
uint8_t ble_audio_get_volume(void)
{
return current_volume;
}
bool ble_audio_is_playing(void)
{
return is_playing;
}
// 实现新增函数
void ble_audio_set_volume(uint8_t volume)
{
current_volume = volume;
volume_value = volume;
if (audioPlayer != NULL) {
audioPlayer->setVolume(volume);
}
}
ble_audio_status_t ble_audio_get_status(void)
{
if (is_playing) {
return BLE_AUDIO_STATUS_PLAYING;
} else {
return BLE_AUDIO_STATUS_PAUSED;
}
}
bool ble_audio_is_connected(void)
{
// 在实际实现中,需要检查蓝牙连接状态
// 此处为简化实现,假设已连接
return true;
}
void ble_audio_send_command(ble_audio_cmd_t cmd, uint8_t data)
{
switch (cmd) {
case BLE_AUDIO_CMD_PLAY:
ble_audio_handle_play_pause(0x01);
break;
case BLE_AUDIO_CMD_PAUSE:
ble_audio_handle_play_pause(0x00);
break;
case BLE_AUDIO_CMD_NEXT:
ble_audio_handle_track_control(0x01);
break;
case BLE_AUDIO_CMD_PREV:
ble_audio_handle_track_control(0x02);
break;
case BLE_AUDIO_CMD_SET_VOLUME:
ble_audio_handle_volume(data);
break;
default:
break;
}
}