From 1ca577d900fee9d5babab6847a21997fe7f24e79 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Wed, 17 Jul 2024 14:36:19 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0hfp=E5=92=8CA2DP=E5=BC=80?= =?utf8?q?=E5=85=B3=EF=BC=8C=E4=BB=A5=E5=8F=8A=E8=8E=B7=E5=8F=96=E7=8A=B6?= =?utf8?q?=E6=80=81=EF=BC=88=E7=9B=AE=E5=89=8D=E6=94=AF=E6=8C=81=E5=85=B3?= =?utf8?q?=E4=B8=80=E4=B8=AA=EF=BC=8C=E5=8F=8C=E5=85=B3=E5=90=8E=E4=BC=9A?= =?utf8?q?=E5=86=8D=E6=AC=A1=E9=87=8D=E8=BF=9E=EF=BC=8C=E5=90=8E=E7=BB=AD?= =?utf8?q?=E4=BC=98=E5=8C=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../bluetooth/include/BluetoothModel.h | 14 +++ .../nativeui/bluetooth/src/BluetoothModel.cpp | 99 +++++++++++++++++-- .../src/BluetoothServiceOptionsPresenter.cpp | 25 ++++- .../src/BluetoothServiceOptionsView.cpp | 7 +- .../nativeui/producttest/src/testbt.cpp | 3 + 5 files changed, 130 insertions(+), 18 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h index 5e21c8a61..e02a1ec3f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h @@ -16,6 +16,9 @@ #include "bts_br_gap.h" namespace OHOS { + + + typedef enum : uint16_t { CALLERSETTING, CALLERPLAYERS, @@ -29,6 +32,8 @@ typedef struct { int16_t temp; } BluetoothCaseInfo; + + class BluetoothItemView; class BluetoothModel { public: @@ -81,6 +86,7 @@ public: int ConnectRemoteDevice(const bd_addr_t *addr); bool ConnectRemoteCheck(const bd_addr_t *addr); char* getWatchName(void); + void set_test_bt_falg(bool falg); private: BluetoothModel(); @@ -126,6 +132,14 @@ void bluetooth_init(void); void InitBluetoothInfo(void); void bluetooth_enable_all(void); void bluetooth_close_all(void); +void bluetooth_set_auto_reconnect(uint8_t flag); + +int uapi_a2dp_snk_get_device_connect_state(void); +int uapi_a2dp_snk_connect(void); +int uapi_a2dp_snk_disconnect(void); +int uapi_disconnect_remote_device(void); + + #ifdef __cplusplus } #endif diff --git a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp index ff58c74ea..95714c092 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp @@ -35,6 +35,7 @@ #include "tcxo.h" #include "custom_runtime.h" #include "bleService.h" +//#include "hfp_hf_service.h" #ifdef SUPPORT_AT_OHOSFWK #include "BluetoothWrapper.h" @@ -117,6 +118,14 @@ void InitBluetoothInfo(void) } } } +//1关 0开 +void bluetooth_set_auto_reconnect(uint8_t flag) +{ + bts_feature_t bts_feature = { 0 }; + bts_feature.bt_auto_reconnect_closed = flag; + errcode_t ret = bts_set_features(&bts_feature); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt bts_set_features ret = 0x%X", ret); +} static int g_bluetooth_initialled=0; static int bluetooth_init_all(void) @@ -126,10 +135,8 @@ static int bluetooth_init_all(void) #ifndef SUPPORT_AUTO_OTA errcode_t ret; // 设置自动回连,ios一键双连依赖此配置 - bts_feature_t bts_feature = { 0 }; - bts_feature.bt_auto_reconnect_closed = 0; - ret = bts_set_features(&bts_feature); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt bts_set_features ret = %u", ret); + + bluetooth_set_auto_reconnect(0); ret = enable_bt_stack(); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt enable_bt_stack ret = %u", ret); @@ -164,13 +171,13 @@ static int bluetooth_init_all(void) // BtInitCallBacksEx(); //BLE // BtInitGatts(); //BLE + // BtInitSpp(); + BtA2dpInit(); //start reg ble server wrapper_gatts_service_start(); // do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_BLE_WRAPPERSTART,NULL,0); - // BtInitSpp(); - BtA2dpInit(); /**开始ble广播*/ // custom_ble_service_start(); @@ -757,10 +764,19 @@ void BtGapPairConfirmedCallback(const bd_addr_t *bdAddr, int req_type, int numbe req_type, number); } + +static bool test_bt_falg=false; + + +void BluetoothModel::set_test_bt_falg(bool falg) +{ + test_bt_falg=falg; +} + // 回调:扫描结果处理/发现结果上报 -void BtGapDiscoveryResultCallback(const bd_addr_t *bdAddr) +void BtGapDiscoveryResultCallback(const bd_addr_t *bdAddr ) { - if ((gap_get_device_class(bdAddr) & DEVICE_MAJOR_CLASS_PHONE) != 0) { + if ((gap_get_device_class(bdAddr) & DEVICE_MAJOR_CLASS_PHONE) != 0 && test_bt_falg==false) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BtGapDiscoveryResultCallback: Mobile Phone Device"); return; } @@ -1176,7 +1192,7 @@ static void BtGattsRequestWriteCallback(uint8_t serverId, uint16_t connId, gatts // 发起ble配对 ret = gap_ble_pair_remote_device(&g_bd_addr); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "gap_ble_pair_remote_device ret = %u ", ret); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_pair_remote_device ret = %u ", ret); } } else if (writeCbPara->length == 10) { uint8_t ctkd[] = {'c', 'l', 'o', 's', 'e', ' ', 'c', 't', 'k', 'd'}; @@ -1282,10 +1298,12 @@ static int32_t BtA2dpSnkAudioDestroy() return 0; } +//a2dp sink端 状态改变回调 static void BtA2dpSnkConnectStateChangedCb(bd_addr_t *bd_addr, int conn_state) { - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x conn_state: %x\n", + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x conn_state: %x\n", bd_addr->addr[3], bd_addr->addr[2], bd_addr->addr[1], bd_addr->addr[0], conn_state); /* 3 2 1 0 idx */ + (void)memcpy_s(&g_bd_addr, sizeof(bd_addr_t), bd_addr, sizeof(bd_addr_t)); } @@ -1544,11 +1562,72 @@ void BleAdvStart(void) adv_para.adv_type = GAP_BLE_ADV_CONN_SCAN_UNDIR; adv_para.channel_map = 0x7; adv_para.duration = 0; + gap_ble_set_adv_param(advId, &adv_para); gap_ble_set_local_appearance(GAP_BLE_APPEARANCE_TYPE_GENERIC_WATCH); errcode_t ret = gap_ble_start_adv(advId); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "BleAdvStart bt gap_ble_start_adv ret = 0x%x", ret); } +//////////////////////////////////A2DP sink端 代码/////////////////////////////////////////////// +int uapi_a2dp_snk_get_device_connect_state() +{ + int ret = a2dp_snk_get_device_connect_state(&g_bd_addr); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret); + + if(ret==PROFILE_STATE_CONNECTING || ret==PROFILE_STATE_CONNECTED) + return 1; + else + return 0; +} + +int uapi_a2dp_snk_connect() +{ + + int ret = a2dp_snk_connect(&g_bd_addr); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "addr: ****%02x%02x%02x%02x playing_state: %x\n", + 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 */ + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"state(int): %x\n", ret); + return ret; +} + +int uapi_a2dp_snk_disconnect() +{ + int ret = a2dp_snk_disconnect(&g_bd_addr); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret); + return ret; +} + +int uapi_disconnect_remote_device() +{ + bd_addr_t bdaddr={0}; + get_phone_addr(&bdaddr); + if (bt_is_acl_connected(&bdaddr)) + { + int ret = gap_disconnect_remote_device(&bdaddr); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bluetooth_close_all gap_disconnect_remote_device ret = %u", ret); + } + return 0; +} + +// /////////////////////////////////////////HFP hf端 代码/////////////////////////////////////////////////// + +// int uapi_HFP_hf_connect() +// { +// int ret = hfp_hf_connect(&g_bd_addr); +// WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret); +// return ret; +// } + +// int uapi_a2dp_snk_disconnect() +// { +// int ret = a2dp_snk_disconnect(&g_bd_addr); +// WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP,"state(int): %d\n", ret); +// return ret; +// } + + + #ifdef __cplusplus } diff --git a/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsPresenter.cpp index 3f8852387..ce09b4c41 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsPresenter.cpp @@ -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); } } } diff --git a/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsView.cpp b/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsView.cpp index 6dd5679c9..694965bc6 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/classicbluetooth/src/BluetoothServiceOptionsView.cpp @@ -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); /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/fd230app/nativeui/producttest/src/testbt.cpp b/code/sdk/application/wearable/fd230app/nativeui/producttest/src/testbt.cpp index 478c164f0..87b92d31b 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/producttest/src/testbt.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/producttest/src/testbt.cpp @@ -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"); -- 2.17.1