From: luozheng <1732036298@qq.com> Date: Mon, 1 Dec 2025 06:18:01 +0000 (+0800) Subject: 添加按键调节耳机音量功能 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=a9a6317183a0cf6865b543a6757e92d48619725c;p=mirrors%2FFD230.git 添加按键调节耳机音量功能 --- diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothHeadsetEvent.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothHeadsetEvent.cpp index a036e7628..301b88ad2 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothHeadsetEvent.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothHeadsetEvent.cpp @@ -26,6 +26,26 @@ avrcp_ct_callbacks_t g_avrcp_ct_cb; avrcp_tg_callbacks_t g_avrcp_tg_cb; BtAvrcpTgInf g_avrcp_srv_inf; +// 获取AVRCP服务信息 +BtAvrcpTgInf GetBtAvrcpTgInf(void) +{ + return g_avrcp_srv_inf; +} + +// 获取本地媒体音量 +uint8_t BtAvrcpTgGetLocMediaVolume(void) +{ + if (g_avrcp_tg_local_media_vol == AVRCP_ABSOLUTE_VOLUME_INVALID) { + int32_t audio_volume = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_A2DP_MUSIC); + uint8_t a2dp_volume = (uint8_t)(audio_volume * AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 / BT_A2DP_MEDIA_VOL_LEVEL); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "audio_volume 0x%x, a2dp_volume 0x%x\n", audio_volume, a2dp_volume); + g_avrcp_tg_local_media_vol = a2dp_volume; + } + + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "get local media volume, a2dp_volume = 0x%x", g_avrcp_tg_local_media_vol); + return g_avrcp_tg_local_media_vol; +} + void BtAvrcpVolumeChangedSyncMedia(uint8_t a2dp_volume) { WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "[BtAvrcpVolumeChangedSyncMedia]::a2dp_volume = 0x%x\r\n", a2dp_volume); @@ -97,6 +117,7 @@ void BtAvrcpTgNotifyVolumeChangedProc(uint8_t a2dp_volume) int32_t audio_volume = a2dp_volume * BT_A2DP_MEDIA_VOL_LEVEL / AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100; WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "NotifyVolume = %d", audio_volume); AudioManager::GetInstance().SetVolume(AUDIO_STREAM_MUSIC, audio_volume); // 耳机连接手表场景调节手表音量 + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, audio_volume); // 耳机连接手表场景调节手表音量 } void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue) @@ -147,18 +168,6 @@ void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue) BtAvrcpTgNotifyMeidaStatus(&event); } -uint8_t BtAvrcpTgGetLocMediaVolume(void) -{ - if (g_avrcp_tg_local_media_vol == AVRCP_ABSOLUTE_VOLUME_INVALID) { - int32_t audio_volume = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_A2DP_MUSIC); - uint8_t a2dp_volume = (uint8_t)(audio_volume * AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 / BT_A2DP_MEDIA_VOL_LEVEL); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "audio_volume 0x%x, a2dp_volume 0x%x\n", audio_volume, a2dp_volume); - g_avrcp_tg_local_media_vol = a2dp_volume; - } - - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "get local media volume, a2dp_volume = 0x%x", g_avrcp_tg_local_media_vol); - return g_avrcp_tg_local_media_vol; -} void MediaStatusCallback(bt_avrcp_tg_evt_status_param *eventStatusParam) { diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp index 02e2c6b0b..2efc67ece 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp @@ -9,6 +9,9 @@ #include "bts_def.h" #include "bts_a2dp_source.h" #include "bts_br_gap.h" +#include "bts_avrcp_controller.h" +#include "bluetooth/BluetoothHeadsetEvent.h" +#include "osal_task.h" #include "ln_api.h" namespace OHOS @@ -312,6 +315,126 @@ extern "C" return 0; } + // 增加耳机音量 + int ln_bt_earphone_volume_up(void) + { + // 获取AVRCP服务信息 + BtAvrcpTgInf avrcpInf = GetBtAvrcpTgInf(); + // 检查AVRCP连接状态 + if (avrcpInf.avrcp_srv_conn_stat != PROFILE_STATE_CONNECTED) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_up: AVRCP not connected"); + return -1; + } + + // 发送音量增加按键 + int ret = avrcp_ct_press_button(&avrcpInf.bd_addr, AVRCP_KEY_VOLUME_UP); + if (ret == 0) { + // 短暂延时后释放按键 + LOS_Msleep(50); + avrcp_ct_release_button(&avrcpInf.bd_addr, AVRCP_KEY_VOLUME_UP); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_up: success"); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_up: failed, ret=%d", ret); + } + + return ret; + } + + // 减少耳机音量 + int ln_bt_earphone_volume_down(void) + { + // 获取AVRCP服务信息 + BtAvrcpTgInf avrcpInf = GetBtAvrcpTgInf(); + // 检查AVRCP连接状态 + if (avrcpInf.avrcp_srv_conn_stat != PROFILE_STATE_CONNECTED) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_down: AVRCP not connected"); + return -1; + } + + // 发送音量减少按键 + int ret = avrcp_ct_press_button(&avrcpInf.bd_addr, AVRCP_KEY_VOLUME_DOWN); + if (ret == 0) { + // 短暂延时后释放按键 + LOS_Msleep(50); + avrcp_ct_release_button(&avrcpInf.bd_addr, AVRCP_KEY_VOLUME_DOWN); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_down: success"); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_volume_down: failed, ret=%d", ret); + } + + return ret; + } + + // 设置耳机音量(绝对音量方式) + int ln_bt_earphone_set_volume(int volume) + { + // 参数校验:音量范围 0-100 + if (volume < 0 || volume > 100) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_set_volume: invalid volume %d, range is 0-100", volume); + return -1; + } + + // 获取AVRCP服务信息 + BtAvrcpTgInf avrcpInf = GetBtAvrcpTgInf(); + // 检查AVRCP连接状态 + if (avrcpInf.avrcp_srv_conn_stat != PROFILE_STATE_CONNECTED) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_set_volume: AVRCP not connected"); + return -1; + } + + // 将百分比音量(0-100)转换为AVRCP绝对音量值(0x00-0x7F) + // AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 = 0x7F = 127 + uint8_t avrcpVolume = (uint8_t)(volume * AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 / 100); + if (volume == 100) { + avrcpVolume = AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100; // 确保100%对应0x7F + } + + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_set_volume: volume=%d%%, avrcpVolume=0x%02x", volume, avrcpVolume); + + // 设置绝对音量 + int ret = avrcp_ct_set_absolute_volume(&avrcpInf.bd_addr, avrcpVolume); + if (ret == 0) { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_set_volume: success"); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_set_volume: failed, ret=%d", ret); + } + + return ret; + } + + // 获取当前耳机音量 + int ln_bt_earphone_get_volume(void) + { + // 获取AVRCP服务信息 + BtAvrcpTgInf avrcpInf = GetBtAvrcpTgInf(); + // 检查AVRCP连接状态 + if (avrcpInf.avrcp_srv_conn_stat != PROFILE_STATE_CONNECTED) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_get_volume: AVRCP not connected"); + return -1; + } + + // 获取当前音量值(AVRCP格式:0x00-0x7F) + uint8_t avrcpVolume = BtAvrcpTgGetLocMediaVolume(); + + // 检查是否为无效值 + if (avrcpVolume == AVRCP_ABSOLUTE_VOLUME_INVALID) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_get_volume: volume is invalid"); + return -1; + } + + // 将AVRCP音量值(0x00-0x7F)转换为百分比(0-100) + int volume = (int)(avrcpVolume * 100 / AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100); + + // 确保返回值在0-100范围内 + if (volume > 100) { + volume = 100; + } + + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ln_bt_earphone_get_volume: avrcpVolume=0x%02x, volume=%d%%", avrcpVolume, volume); + + return volume; + } + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothHeadsetEvent.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothHeadsetEvent.h index 8e5341d74..85639464c 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothHeadsetEvent.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothHeadsetEvent.h @@ -31,6 +31,8 @@ typedef struct { profile_connect_state_t avrcp_srv_conn_stat; } BtAvrcpTgInf; +BtAvrcpTgInf GetBtAvrcpTgInf(void); +uint8_t BtAvrcpTgGetLocMediaVolume(void); void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue); void MediaStatusCallback(bt_avrcp_tg_evt_status_param *eventStatusParam); void PressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue); diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h index 0e2a44541..9c69ccff4 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h @@ -36,6 +36,18 @@ int ln_bt_get_last_conned_device_battery_level(void); // 上报蓝牙耳机连接状态 int ln_bt_report_earphone_connection_status(void); + +// 增加耳机音量(按键方式,音量调节值由耳机固件决定,通常为固定步进值) +int ln_bt_earphone_volume_up(void); + +// 减少耳机音量(按键方式,音量调节值由耳机固件决定,通常为固定步进值) +int ln_bt_earphone_volume_down(void); + +// 设置耳机音量(绝对音量方式,volume范围:0-100,对应0%-100%) +int ln_bt_earphone_set_volume(int volume); + +// 获取当前耳机音量(返回值:0-100,对应0%-100%,-1表示获取失败) +int ln_bt_earphone_get_volume(void); #ifdef __cplusplus } #endif diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h index b2505aed6..72f6225c5 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h @@ -13,6 +13,7 @@ #include #include "AppViewIDs.h" // #include "UiConfig.h" +#include "bluetooth/Ln_Bluetooth_apiC.h" namespace OHOS { diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp index e0ef8740c..c5e0947f9 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp @@ -465,6 +465,7 @@ namespace OHOS if (value > 100) { value = 100; } remindProgressValue_ = value; AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, remindProgressValue_); + ln_bt_earphone_set_volume(remindProgressValue_); VolumeProgress->SetValue(remindProgressValue_); VolumeProgress->Invalidate(); }