From e0291723114a09bbd195be0543a4fb5cdc03a127 Mon Sep 17 00:00:00 2001 From: SnickerSun Date: Thu, 4 Jul 2024 19:05:07 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E5=90=8D?= =?utf8?q?=E7=A7=B0=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../bluetooth/include/BluetoothModel.h | 2 +- .../nativeui/bluetooth/src/BluetoothModel.cpp | 54 +++++++++++-------- .../ble_service/bleservice/bleService.c | 30 +++-------- .../ble_service/bleservice/bleService.h | 3 ++ 4 files changed, 42 insertions(+), 47 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 6d6ccdf6a..5e21c8a61 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/include/BluetoothModel.h @@ -88,7 +88,6 @@ private: void SetUpListCase(); void SetUpConnedDevicesList(); bool FindNameByAddr(BluetoothCaseInfo &listInfo, BluetoothCaseInfo *caseInfo); - void InitBluetoothInfo(void); bool aclConnected{false}; BluetoothCaseInfo tempMacValue{}; bd_addr_t lastConnedAddr{}; @@ -124,6 +123,7 @@ void BtInitGatts(void); void BtInitSpp(void); void BleAdvStart(void); void bluetooth_init(void); +void InitBluetoothInfo(void); void bluetooth_enable_all(void); void bluetooth_close_all(void); #ifdef __cplusplus 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 742767e4e..94b6b4df9 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothModel.cpp @@ -79,31 +79,39 @@ char* BluetoothModel::getWatchName(void) return watchName; } -void BluetoothModel::InitBluetoothInfo(void) +void InitBluetoothInfo(void) { - errcode_t ret; unsigned char nameLen = BD_NAME_LEN; + uint8_t ble_name[BD_NAME_LEN] = {0}; + uint8_t ble_name_len = BD_NAME_LEN; + errcode_t ret = 0; + + ret = bluetooth_get_local_name(ble_name, &ble_name_len); + if (ret != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_get_local_name fail, name:%s,len:%u,ret:%u.", ble_name, ble_name_len, ret); + memset_s(ble_name, BD_NAME_LEN, 0, BD_NAME_LEN); + ble_name_len = strlen(BLE_DEFAULT_NAME) + 1; + memcpy_s(ble_name, BD_NAME_LEN,BLE_DEFAULT_NAME,ble_name_len); + } + unsigned char addr[6] = {0}; + unsigned int len = sizeof(addr); + memset(addr,0,6); + bluetooth_get_local_addr(&addr[0],len); + //如果蓝牙名称不正确,按照规则重命名 + char watchName[BD_NAME_LEN]= {0}; memset_s(watchName, sizeof(watchName), 0, sizeof(watchName)); - ret = bluetooth_get_local_name((unsigned char *)watchName, &nameLen); - // 为了实验室测试需要,客户需要自己修改 - if ((nameLen == strlen("WatchDemo") + 1) && (strcmp("WatchDemo", watchName) == 0)) { - uint32_t currentUs = uapi_tcxo_get_us(); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "currentUs 0x%x", currentUs); - // 设置蓝牙名称 - (void)sprintf_s(watchName, sizeof(watchName), "Watch%x", (currentUs & 0xFFFF)); - ret = bluetooth_set_local_name((unsigned char*)watchName, strlen((char*)watchName) + 1); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bluetooth_set_local_name %s ret = %u", watchName, ret); - - // 设置蓝牙地址 - bd_addr_t addr = {{0x0, 0x0, 0x33, 0x44, 0x55, 0x66} ,0}; - addr.addr[0] = currentUs & 0xFF; - addr.addr[1] = (currentUs & 0XFF00) >> 8; - ret = bluetooth_set_local_addr(addr.addr, BD_ADDR_LEN); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bluetooth_set_local_addr [0x%x:0x%x] ret = %u", addr.addr[0], addr.addr[1], ret); - } else { - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "watchName: %s", watchName); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "nameLen: %u", nameLen); - } + (void)sprintf_s(watchName, sizeof(watchName), "%s %02X%02X",BLE_DEFAULT_NAME,addr[1],addr[0]); + int watch_name_len = strlen(watchName); + /**表名长度不对,或者内容不对,都要重命名*/ + if (watch_name_len != strlen((char*)ble_name) || strncmp((char*)ble_name,watchName,watch_name_len) != 0 ) + { + ret = bluetooth_set_local_name((unsigned char*)watchName, watch_name_len+1); + if (ret == ERRCODE_SUCC) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bt SetLocalName %s ", watchName); + }else{ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bt SetLocalName %s ret = 0x%x", watchName,ret); + } + } } static int g_bluetooth_initialled=0; @@ -136,6 +144,8 @@ static int bluetooth_init_all(void) // ret = gap_ble_set_sec_param(&sec_param); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bt gap_ble_set_sec_param ret = %u", ret); + InitBluetoothInfo(); + BtInitCallBacks(); wrapper_ble_register(); #ifdef SUPPORT_ALIPAY_SEC diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c index e88e10bdc..dd3f19504 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c @@ -71,7 +71,7 @@ extern "C" { #define BLE_SCAN_DATA_LENTH 31 //扫描数据 #define BLE_ADV_TYPE_LENTH 3 #define BLE_ADV_UUID_LENTH 4 -#define BLE_DEFAULT_NAME "WatchSoar"//"KUMI U6" + #define BLE_DEFAULT_NAME_LEN 32 #define BLE_MAC_MAX_LEN 6 //蓝牙mac地址最大个数6个 @@ -164,33 +164,15 @@ static void svr_ble_adv_data_set(int adv_id) unsigned int len = BLE_MAC_MAX_LEN; errcode_t ret = 0; + memset(addr,0,6); + bluetooth_get_local_addr(&addr[0],len); ret = bluetooth_get_local_name(ble_name, &ble_name_len); if (ret != 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_get_local_name fail, name:%s,len:%u,ret:%u.", ble_name, ble_name_len, ret); memset_s(ble_name, BLE_NAME_LENGTH, 0, BLE_NAME_LENGTH); - memcpy_s(ble_name, BLE_NAME_LENGTH,BLE_DEFAULT_NAME, BLE_DEFAULT_NAME_LEN); - ble_name_len = BLE_DEFAULT_NAME_LEN + 1; + (void)sprintf_s((char*)ble_name, BLE_NAME_LENGTH, "%s %02X%02X",BLE_DEFAULT_NAME,addr[1],addr[0]); + ble_name_len = strlen((char*)ble_name) + 1; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_ble_get_local_name fail,rename name:%s,len:%u,ret:%u.", ble_name, ble_name_len, ret); } - - memset(addr,0,6); - bluetooth_get_local_addr(&addr[0],len); -#if 1 //如果蓝牙名称不正确,按照规则重命名 - char watchName[BLE_NAME_LENGTH]; - memset_s(watchName, sizeof(watchName), 0, sizeof(watchName)); - (void)sprintf_s(watchName, sizeof(watchName), "%s %02X%02X",BLE_DEFAULT_NAME,addr[1],addr[0]); - // (void)sprintf_s(watchName, sizeof(watchName), "%s",BLE_DEFAULT_NAME); - int watch_name_len = strlen(watchName); - int cmp_len = watch_name_len>ble_name_len?ble_name_len:watch_name_len; - if (strncmp((char*)ble_name,watchName,cmp_len) != 0 ) - { - ret = bluetooth_set_local_name((unsigned char*)watchName, strlen((char*)watchName) + 1); - if (ret == false) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bt SetLocalName ret = 0x%x", ret); - } - memcpy_s(ble_name,BLE_NAME_LENGTH,watchName,BLE_NAME_LENGTH); - } -#endif - int k = BD_ADDR_LEN; for (int i = 0; i < BD_ADDR_LEN; i++) { ble_data_mac_switch[--k] = addr[i]; diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h index 6e6142c39..c0e143962 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h @@ -26,6 +26,9 @@ extern uint16_t ble_nus_max_data_len; extern uint16_t alipay_handle; extern uint16_t g_ble_conn_id; extern uint16_t g_ble_server_handle; + +#define BLE_DEFAULT_NAME "WatchSoar"//"KUMI U6" + void svr_ble_bluetooth_init(void);//蓝牙服务初始化 void custom_ble_service_stop(void);//主动关闭蓝牙 void custom_ble_service_start(void);//主动开启蓝牙 -- 2.17.1