增加hfp和A2DP开关,以及获取状态(目前支持关一个,双关后会再次重连,后续优化) 540/1 540/head 541/target/1
authorhcj <1224667075@qq.com>
Wed, 17 Jul 2024 06:36:19 +0000 (14:36 +0800)
committerhcj <1224667075@qq.com>
Wed, 17 Jul 2024 06:36:19 +0000 (14:36 +0800)
code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h
code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp
code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsView.cpp
code/sdk/application/wearable/fd230app/nativeui/producttest/src/testbt.cpp

index 5e21c8a..e02a1ec 100644 (file)
@@ -16,6 +16,9 @@
 #include "bts_br_gap.h"\r
 \r
 namespace OHOS {\r
+\r
+\r
+\r
 typedef enum : uint16_t {\r
     CALLERSETTING,\r
     CALLERPLAYERS,\r
@@ -29,6 +32,8 @@ typedef struct {
     int16_t temp;\r
 } BluetoothCaseInfo;\r
 \r
+\r
+\r
 class BluetoothItemView;\r
 class BluetoothModel {\r
 public:\r
@@ -81,6 +86,7 @@ public:
     int ConnectRemoteDevice(const bd_addr_t *addr);\r
     bool ConnectRemoteCheck(const bd_addr_t *addr);\r
     char* getWatchName(void);\r
+    void set_test_bt_falg(bool falg);\r
 \r
 private:\r
     BluetoothModel();\r
@@ -126,6 +132,14 @@ void bluetooth_init(void);
 void InitBluetoothInfo(void);\r
 void bluetooth_enable_all(void);\r
 void bluetooth_close_all(void);\r
+void bluetooth_set_auto_reconnect(uint8_t flag);\r
+\r
+int uapi_a2dp_snk_get_device_connect_state(void);\r
+int uapi_a2dp_snk_connect(void);\r
+int uapi_a2dp_snk_disconnect(void);\r
+int uapi_disconnect_remote_device(void);\r
+\r
+\r
 #ifdef __cplusplus\r
 }\r
 #endif\r
index ff58c74..95714c0 100644 (file)
@@ -35,6 +35,7 @@
 #include "tcxo.h"\r
 #include "custom_runtime.h"\r
 #include "bleService.h"\r
+//#include "hfp_hf_service.h"\r
 \r
 #ifdef SUPPORT_AT_OHOSFWK\r
 #include "BluetoothWrapper.h"\r
@@ -117,6 +118,14 @@ void InitBluetoothInfo(void)
         }\r
     }    \r
 }\r
+//1关 0开\r
+void bluetooth_set_auto_reconnect(uint8_t flag)\r
+{\r
+    bts_feature_t bts_feature = { 0 };\r
+    bts_feature.bt_auto_reconnect_closed = flag;\r
+    errcode_t ret = bts_set_features(&bts_feature);\r
+    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt bts_set_features ret = 0x%X", ret);\r
+}\r
 \r
 static int g_bluetooth_initialled=0;\r
 static int bluetooth_init_all(void)\r
@@ -126,10 +135,8 @@ static int bluetooth_init_all(void)
 #ifndef SUPPORT_AUTO_OTA\r
     errcode_t ret;\r
     // 设置自动回连,ios一键双连依赖此配置\r
-    bts_feature_t bts_feature = { 0 };\r
-    bts_feature.bt_auto_reconnect_closed = 0;\r
-    ret = bts_set_features(&bts_feature);\r
-    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt bts_set_features ret = %u", ret);\r
+   \r
+    bluetooth_set_auto_reconnect(0);\r
 \r
     ret = enable_bt_stack();\r
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt enable_bt_stack ret = %u", ret);\r
@@ -164,13 +171,13 @@ static int bluetooth_init_all(void)
     // BtInitCallBacksEx(); //BLE\r
     // BtInitGatts(); //BLE \r
 \r
+    // BtInitSpp();\r
+    BtA2dpInit();\r
 \r
     //start reg ble server\r
     wrapper_gatts_service_start(); \r
     // do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_BLE_WRAPPERSTART,NULL,0);\r
 \r
-    // BtInitSpp();\r
-    BtA2dpInit();\r
 \r
     /**开始ble广播*/\r
     // custom_ble_service_start();\r
@@ -757,10 +764,19 @@ void BtGapPairConfirmedCallback(const bd_addr_t *bdAddr, int req_type, int numbe
         req_type, number);\r
 }\r
 \r
+\r
+static bool test_bt_falg=false;\r
+\r
+\r
+void BluetoothModel::set_test_bt_falg(bool falg)\r
+{\r
+    test_bt_falg=falg;\r
+}\r
+\r
 // 回调:扫描结果处理/发现结果上报\r
-void BtGapDiscoveryResultCallback(const bd_addr_t *bdAddr)\r
+void BtGapDiscoveryResultCallback(const bd_addr_t *bdAddr )\r
 {\r
-    if ((gap_get_device_class(bdAddr) & DEVICE_MAJOR_CLASS_PHONE) != 0) {\r
+    if ((gap_get_device_class(bdAddr) & DEVICE_MAJOR_CLASS_PHONE) != 0 && test_bt_falg==false) {\r
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BtGapDiscoveryResultCallback: Mobile Phone Device");\r
         return;\r
     }\r
@@ -1176,7 +1192,7 @@ static void BtGattsRequestWriteCallback(uint8_t serverId, uint16_t connId, gatts
 \r
                 // 发起ble配对\r
                 ret = gap_ble_pair_remote_device(&g_bd_addr);\r
-                WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "gap_ble_pair_remote_device ret = %u ", ret);\r
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_pair_remote_device ret = %u ", ret);\r
             }\r
         } else if (writeCbPara->length == 10) {\r
             uint8_t ctkd[] = {'c', 'l', 'o', 's', 'e', ' ', 'c', 't', 'k', 'd'};\r
@@ -1282,10 +1298,12 @@ static int32_t BtA2dpSnkAudioDestroy()
     return 0;\r
 }\r
 \r
+//a2dp sink端 状态改变回调\r
 static void BtA2dpSnkConnectStateChangedCb(bd_addr_t *bd_addr, int conn_state)\r
 {\r
-    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x conn_state: %x\n",\r
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x conn_state: %x\n",\r
         bd_addr->addr[3], bd_addr->addr[2], bd_addr->addr[1], bd_addr->addr[0], conn_state); /* 3 2 1 0 idx */\r
+     (void)memcpy_s(&g_bd_addr, sizeof(bd_addr_t), bd_addr, sizeof(bd_addr_t));\r
     \r
 }\r
 \r
@@ -1544,11 +1562,72 @@ void BleAdvStart(void)
     adv_para.adv_type = GAP_BLE_ADV_CONN_SCAN_UNDIR;\r
     adv_para.channel_map = 0x7;\r
     adv_para.duration = 0;\r
+\r
     gap_ble_set_adv_param(advId, &adv_para);\r
     gap_ble_set_local_appearance(GAP_BLE_APPEARANCE_TYPE_GENERIC_WATCH);\r
     errcode_t ret = gap_ble_start_adv(advId);\r
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "BleAdvStart bt gap_ble_start_adv  ret = 0x%x", ret);\r
 }\r
+//////////////////////////////////A2DP sink端 代码///////////////////////////////////////////////\r
+int uapi_a2dp_snk_get_device_connect_state()\r
+{\r
+    int ret = a2dp_snk_get_device_connect_state(&g_bd_addr);\r
+    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret);\r
+\r
+    if(ret==PROFILE_STATE_CONNECTING || ret==PROFILE_STATE_CONNECTED)\r
+    return 1;\r
+    else\r
+    return 0;\r
+}\r
+\r
+int uapi_a2dp_snk_connect()\r
+{\r
+\r
+    int ret = a2dp_snk_connect(&g_bd_addr);\r
+     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x playing_state: %x\n",\r
+    g_bd_addr.addr[3], g_bd_addr.addr[2], g_bd_addr.addr[1], g_bd_addr.addr[0], g_bd_addr.type); /* 3 2 1 0 idx */\r
+\r
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"state(int): %x\n", ret);\r
+    return ret;\r
+}\r
+\r
+int uapi_a2dp_snk_disconnect()\r
+{\r
+    int ret = a2dp_snk_disconnect(&g_bd_addr);\r
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret);\r
+    return ret;\r
+}\r
+\r
+int uapi_disconnect_remote_device()\r
+{\r
+    bd_addr_t bdaddr={0};\r
+    get_phone_addr(&bdaddr);\r
+    if (bt_is_acl_connected(&bdaddr))\r
+    {\r
+       int ret = gap_disconnect_remote_device(&bdaddr);\r
+       WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bluetooth_close_all gap_disconnect_remote_device ret = %u", ret);\r
+    }\r
+    return 0;\r
+}\r
+  \r
+// /////////////////////////////////////////HFP hf端 代码///////////////////////////////////////////////////\r
+\r
+// int uapi_HFP_hf_connect()\r
+// {\r
+//     int ret = hfp_hf_connect(&g_bd_addr);\r
+//     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret);\r
+//     return ret;\r
+// }\r
+\r
+// int uapi_a2dp_snk_disconnect()\r
+// {\r
+//     int ret = a2dp_snk_disconnect(&g_bd_addr);\r
+//     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret);\r
+//     return ret;\r
+// }\r
+\r
+\r
+\r
 \r
 #ifdef __cplusplus\r
 }\r
index 3f88523..ce09b4c 100644 (file)
@@ -9,7 +9,9 @@
 #include "map"
 #include "classicbluetooth/include/BluetoothServiceOptionsPresenter.h"
 #include "classicbluetooth/include/BluetoothServiceOptionsView.h"
-
+#include "BluetoothModel.h"
+#include "PhoneService.h"
+#include "bts_le_gap.h"
 namespace OHOS
 {
     // 当前Presenter属性实例化的内存指针
@@ -82,12 +84,15 @@ namespace OHOS
                     if (it->second == "HFP")
                     {
                         ConfigModel::GetInstance()->SetHFPState(true);
-                        enable_hfp_service(true);
+                        //enable_hfp_service(true);
+                        uapi_hfp_connect();
                     }
                     else if (it->second == "A2DP")
                     {
                         ConfigModel::GetInstance()->SetA2DPState(true);
-                        enable_a2dp_service(true);
+                        uapi_a2dp_snk_connect();
+                        
+                        //enable_a2dp_service(true);
                     }
                 }
                 else
@@ -96,12 +101,22 @@ namespace OHOS
                     if (it->second == "HFP")
                     {
                         ConfigModel::GetInstance()->SetHFPState(false);
-                        enable_hfp_service(false);
+                        //enable_hfp_service(false);
+                        uapi_hfp_disconnect();
+                        // errcode_t ret = gap_ble_set_ctkd_enable(false);
+                        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_set_ctkd_enable ret = %u", ret);
                     }
                     else if (it->second == "A2DP")
                     {
+
                         ConfigModel::GetInstance()->SetA2DPState(false);
-                        enable_a2dp_service(false);
+                        uapi_a2dp_snk_disconnect();
+                        // uapi_hfp_disconnect();
+                        // uapi_disconnect_remote_device();
+                        // errcode_t ret = gap_ble_set_ctkd_enable(false);
+                        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_set_ctkd_enable ret = %u", ret);
+                        //bluetooth_set_auto_reconnect(1);
+                        //enable_a2dp_service(false);
                     }
                 }
             }
index 6dd5679..694965b 100644 (file)
@@ -3,7 +3,8 @@
 #include "ConfigModel.h"
 #include "UiConfig.h"
 #include "wearable_log.h"
-
+#include "PhoneService.h"
+#include "BluetoothModel.h"
 namespace OHOS
 {
     static BluetoothServiceOptionsView *g_pBluetoothServiceOptionsView{nullptr};
@@ -163,7 +164,7 @@ namespace OHOS
             return false;
         }
         HFP_ToggleButton->SetPosition(298, 145, 100, 50);
-        HFP_ToggleButton->SetState(ConfigModel::GetInstance()->GetHFPState());
+        HFP_ToggleButton->SetState(uapi_hfp_connect_state());//ConfigModel::GetInstance()->GetHFPState());
         HFP_ToggleButton->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
         HFP_ToggleButton->SetImages(PNG_TOGGLEBUTTON_ON_IMAGE, PNG_TOGGLEBUTTON_OFF_IMAGE);
         /* -------------------------------------------------------------------------- */
@@ -174,7 +175,7 @@ namespace OHOS
             return false;
         }
         A2DP_ToggleButton->SetPosition(298, 265, 100, 50);
-        A2DP_ToggleButton->SetState(ConfigModel::GetInstance()->GetA2DPState());
+        A2DP_ToggleButton->SetState(uapi_a2dp_snk_get_device_connect_state());
         A2DP_ToggleButton->SetStyle(STYLE_BACKGROUND_OPA, 0xffffffff);
         A2DP_ToggleButton->SetImages(PNG_TOGGLEBUTTON_ON_IMAGE, PNG_TOGGLEBUTTON_OFF_IMAGE);
         /* -------------------------------------------------------------------------- */
index 478c164..87b92d3 100644 (file)
@@ -91,6 +91,7 @@ namespace OHOS
                     label_BR_SCAN[i]=nullptr;
                 }
             }
+            BluetoothModel::GetInstance()->set_test_bt_falg(false);
 
             uint8_t cnt = pm_veto_get_id_cnt(PM_ID_P_TEST);
             if (cnt == 1) {
@@ -160,6 +161,8 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"gap_br_set_inquiry_paramters errcode_t %d!\n",err);
 
             bool ret = gap_br_start_discovery();
+            BluetoothModel::GetInstance()->SortScansCaseRssi();
+             BluetoothModel::GetInstance()->set_test_bt_falg(true);
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"gap_br_start_discovery bool %d!\n",ret);
             #endif
             button->SetText("scaning");