添加按键调节耳机音量功能
authorluozheng <1732036298@qq.com>
Mon, 1 Dec 2025 06:18:01 +0000 (14:18 +0800)
committerluozheng <1732036298@qq.com>
Mon, 1 Dec 2025 06:18:01 +0000 (14:18 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothHeadsetEvent.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothHeadsetEvent.h
code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp

index a036e76..301b88a 100644 (file)
@@ -26,6 +26,26 @@ avrcp_ct_callbacks_t g_avrcp_ct_cb;
 avrcp_tg_callbacks_t g_avrcp_tg_cb;\r
 BtAvrcpTgInf g_avrcp_srv_inf;\r
 \r
+// 获取AVRCP服务信息\r
+BtAvrcpTgInf GetBtAvrcpTgInf(void)\r
+{\r
+    return g_avrcp_srv_inf;\r
+}\r
+\r
+// 获取本地媒体音量\r
+uint8_t BtAvrcpTgGetLocMediaVolume(void)\r
+{\r
+    if (g_avrcp_tg_local_media_vol == AVRCP_ABSOLUTE_VOLUME_INVALID) {\r
+        int32_t audio_volume = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_A2DP_MUSIC);\r
+        uint8_t a2dp_volume = (uint8_t)(audio_volume * AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 / BT_A2DP_MEDIA_VOL_LEVEL);\r
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "audio_volume 0x%x, a2dp_volume 0x%x\n", audio_volume, a2dp_volume);\r
+        g_avrcp_tg_local_media_vol = a2dp_volume;\r
+    }\r
+\r
+    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "get local media volume, a2dp_volume = 0x%x", g_avrcp_tg_local_media_vol);\r
+    return g_avrcp_tg_local_media_vol;\r
+}\r
+\r
 void BtAvrcpVolumeChangedSyncMedia(uint8_t a2dp_volume)\r
 {\r
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "[BtAvrcpVolumeChangedSyncMedia]::a2dp_volume = 0x%x\r\n", a2dp_volume);\r
@@ -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;\r
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "NotifyVolume = %d", audio_volume);\r
     AudioManager::GetInstance().SetVolume(AUDIO_STREAM_MUSIC, audio_volume); // 耳机连接手表场景调节手表音量\r
+    AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, audio_volume); // 耳机连接手表场景调节手表音量\r
 }\r
 \r
 void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue)\r
@@ -147,18 +168,6 @@ void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue)
     BtAvrcpTgNotifyMeidaStatus(&event);\r
 }\r
 \r
-uint8_t BtAvrcpTgGetLocMediaVolume(void)\r
-{\r
-    if (g_avrcp_tg_local_media_vol == AVRCP_ABSOLUTE_VOLUME_INVALID) {\r
-        int32_t audio_volume = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_A2DP_MUSIC);\r
-        uint8_t a2dp_volume = (uint8_t)(audio_volume * AVRCP_ABSOLUTE_VOLUME_PERCENTAGE_100 / BT_A2DP_MEDIA_VOL_LEVEL);\r
-        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "audio_volume 0x%x, a2dp_volume 0x%x\n", audio_volume, a2dp_volume);\r
-        g_avrcp_tg_local_media_vol = a2dp_volume;\r
-    }\r
-\r
-    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "get local media volume, a2dp_volume = 0x%x", g_avrcp_tg_local_media_vol);\r
-    return g_avrcp_tg_local_media_vol;\r
-}\r
 \r
 void MediaStatusCallback(bt_avrcp_tg_evt_status_param *eventStatusParam)\r
 {\r
index 02e2c6b..2efc67e 100644 (file)
@@ -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
index 8e5341d..8563946 100644 (file)
@@ -31,6 +31,8 @@ typedef struct {
     profile_connect_state_t avrcp_srv_conn_stat;\r
 } BtAvrcpTgInf;\r
 \r
+BtAvrcpTgInf GetBtAvrcpTgInf(void);\r
+uint8_t BtAvrcpTgGetLocMediaVolume(void);\r
 void UsingPressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue);\r
 void MediaStatusCallback(bt_avrcp_tg_evt_status_param *eventStatusParam);\r
 void PressButtonStatusCallback(td_u32 keyOperation, td_u32 keyValue);\r
index 0e2a445..9c69ccf 100644 (file)
@@ -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
index e0ef874..c5e0947 100644 (file)
@@ -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();
     }