From dd9c63500e8d2d03ddedafcc2fbec0527e275cf7 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Tue, 13 Aug 2024 12:02:37 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0ios=E4=B8=8EAndroid=E5=88=A4?= =?utf8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../service/ble_service/blemoder/bletest.cpp | 16 ++++++ .../service/ble_service/blemoder/bletest.h | 5 ++ .../service/ble_service/bleservice/bleEvent.c | 24 ++++++--- .../ble_service/bleservice/bleService.c | 49 ++++++++++++++++--- .../ble_service/bleservice/bleService.h | 3 ++ .../peripheral/config/include/ConfigModel.h | 4 ++ 6 files changed, 87 insertions(+), 14 deletions(-) diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp index e98e7ab23..53099b79d 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp @@ -87,6 +87,7 @@ void get_music_path(char *buf,uint8_t max_len) memcpy_s(buf,max_len,path.c_str(),path.length()); } + //在此处实现,给UI回复消息。 void ble_app_reply(bool data) { @@ -125,6 +126,21 @@ char* ble_get_ai_audio_path_str(void) return (char *)OHOS::AiVoiceModel::GetInstance().GetAIAudioPath().c_str(); } +void ble_set_pair(uint8_t falg) +{ + OHOS::ConfigModel *p_ConfigModel = OHOS::ConfigModel::GetInstance(); + + p_ConfigModel->set_condigData_Int(OHOS::ConfigModel::CONFIGMODEL_ITEM_BLE_IOS_OR_ANDROID,falg); + return; +} + +uint8_t get_ble_pair(void) +{ + OHOS::ConfigModel *p_ConfigModel = OHOS::ConfigModel::GetInstance(); + + return p_ConfigModel->get_configData_int(OHOS::ConfigModel::CONFIGMODEL_ITEM_BLE_IOS_OR_ANDROID); + +} diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h index e87a66665..16c4cafa5 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h @@ -314,6 +314,11 @@ char * dir_to_json_buf_malloc_(uint8_t type); void get_music_path(char *buf,uint8_t max_len); +/////////////////设备绑定相关////////////// +void ble_set_pair(uint8_t falg); + +uint8_t get_ble_pair(void); + /////////////////////数据同步//////////////////////////// diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c index ed22363cd..169f2d8dc 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c @@ -642,7 +642,7 @@ void alarmData(void) } -void getPhoneNameLen(uint8_t *data,uint8_t *datalen)//获取手机设备名称长度 +void getPhoneNameLen(uint8_t *data,uint8_t *datalen,uint8_t *type)//获取手机设备名称长度 { uint8_t len = 0; uint8_t *json = data; @@ -655,6 +655,9 @@ void getPhoneNameLen(uint8_t *data,uint8_t *datalen)//获取手机设备名称 len = strlen(pn->valuestring)+1; printf("设备名称: %s len:%d\n", pn->valuestring,len); } + cJSON* tp = cJSON_GetObjectItem(root, "tp"); + *type=tp->valueint; + *datalen = len; cJSON_Delete(root); } else{ @@ -948,7 +951,7 @@ void sendBindInfor(uint8_t *data) uint8_t devnamelen = 0; uint8_t mac_tmp[6]; memset(mac_tmp,0,6); - + uint8_t phone_type; uint8_t bond = 1;//未与任何手机绑定过 errcode_t ret = 0; first_bond = 0; @@ -973,25 +976,32 @@ void sendBindInfor(uint8_t *data) comfirmPaire(1); #else devnamelen = 0; - getPhoneNameLen(data,&devnamelen);//获取手机下发的手机设备名称长度 + getPhoneNameLen(data,&devnamelen,&phone_type);//获取手机下发的手机设备名称长度 if(devnamelen > 0 ) { devname = getPhoneName(data); printf("devname!!!!!!!!!:%s devnamelen:%d",devname,devnamelen); if (devname != NULL){ - printf("phone devname is name!!!!!!!!!:"); + printf("phone devname is name!!!!!!!!!\r\n"); uint8_t buf[devnamelen+2]; memset(buf,0,devnamelen+2); buf[0] = DEVICEBIND ; buf[1] = devnamelen;//设备名称长度 0不显示 memcpy(buf+2,devname,devnamelen); - if(strcmp("iPhone",(char *)devname)==0) + if(phone_type==1) { + printf("IOS\r\n"); + ble_set_pair(1); + errcode_t ret = gap_ble_set_ctkd_enable(true); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "iPhone gap_ble_set_ctkd_enable ret = %x", ret); set_ctkd_flag(true); } + else + { + ble_set_pair(0); + } CustomEventPublish(TOPIC_EVENT_DEVICE_BIND,(uint8_t *)buf,devnamelen+2); if(devname) @@ -6960,9 +6970,9 @@ errcode_t combine_data_internal(ble_read_data *buffer_handle,uint8_t *totalData, if (header->direction == MSG_DIRECTION_RECV_ANDROID) { - uapi_dev_runtime_set_android_connection_status(1); + //uapi_dev_runtime_set_android_connection_status(1); }else{ - uapi_dev_runtime_set_android_connection_status(0); + // uapi_dev_runtime_set_android_connection_status(0); } buffer_handle->cmd_header.direction = header->direction; buffer_handle->cmd_header.cmdtype = header->cmdtype; 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 5045c54f7..455a9eeb1 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c @@ -339,18 +339,34 @@ void svr_pair_result_cbt_cb(uint16_t conn_id, const bd_addr_t *addr, errcode_t s { devpair_state = 1;//代表配对成功了 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "svr_pair_result_cbt_cb!!! addr 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x status:0x%x\r\n",addr->addr[0],addr->addr[1], + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "svr_pair_result_cbt_cb!!! addr 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x status:0x%x\r\n",addr->addr[0],addr->addr[1], addr->addr[2],addr->addr[3],addr->addr[4],addr->addr[5],status); //if(get_notify_switch()) if(status==ERRCODE_SUCC) { + if(get_ble_pair()) ble_ancs_client_cmd_setup_and_subscribe(g_ble_conn_id); if(get_ctkd_flag() && cm_custom_ble_service_on_flag()) { - - int ret=gap_reconnect_media_device(&phone_addr); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_reconnect_media_device!!! %X\r\n",ret); + gap_br_set_bt_scan_mode(GAP_SCAN_MODE_NONE, 20); + + if(!uapi_dev_runtime_get_android_connection_status()) + { + int ret=gap_reconnect_media_device(&phone_addr); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_reconnect_media_device!!! %X\r\n",ret); + //uapi_a2dp_snk_connect(); + + + }else{ + // int ret = gap_connect_remote_device(addr); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_connect_remote_device!!! %X\r\n",ret); + + + } + //uapi_a2dp_snk_connect(); + } hand_watch_first_time_Paired(); svr_ble_adv_stop(); @@ -1366,13 +1382,32 @@ void custom_ble_service_stop(void) svr_ble_adv_stop();//停止广播 不发送广播数据 就是app搜索不到该设备了 free_ble_data(); //删除所有协议缓存数据 } + +void uapi_ble_disconnect_device() +{ + errcode_t ret = 0; + + ret = gap_ble_disconnect_remote_device((const bd_addr_t *)&phone_addr);//与固定的mac设置地址进行断开连接 + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "gap_ble_disconnect_remote_device ret = %X", ret); + +} //开启蓝牙 void custom_ble_service_start(void) { - AdverFlag = true; - svr_ble_adv_start_param(); - gap_br_set_bt_scan_mode(GAP_SCAN_MODE_NONE, 0); + AdverFlag = true; + svr_ble_adv_start_param(); + + gap_br_set_bt_scan_mode(GAP_SCAN_MODE_NONE, 0); + +} + +void custom_ble_service_start_(uint8_t data) +{ + AdverFlag = true; + svr_ble_adv_start_param(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "custom_ble_service_start_ "); + gap_br_set_bt_scan_mode(GAP_SCAN_MODE_NONE, data); } #define MODE_BLE_ADV_SRV (0) 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 999b72b93..06adf911b 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.h @@ -54,6 +54,9 @@ void srv_ble_stop_scan(void); void blesrv_service_recv_msg_handler(uint16_t srv_handle, uint8_t * msg_data, uint32_t msg_len); errcode_t uapi_blesrv_service_init(void); + +void custom_ble_service_start_(uint8_t data); +void uapi_ble_disconnect_device(void); #ifdef __cplusplus } #endif diff --git a/code/sdk/drivers/peripheral/config/include/ConfigModel.h b/code/sdk/drivers/peripheral/config/include/ConfigModel.h index dc3906c2b..dc55be716 100644 --- a/code/sdk/drivers/peripheral/config/include/ConfigModel.h +++ b/code/sdk/drivers/peripheral/config/include/ConfigModel.h @@ -130,7 +130,10 @@ namespace OHOS CONFIGMODEL_ITEM_FIRSTTIMESTART,//手表是否是第一次启动 CONFIGMODEL_ITEM_OPEN_AI_API_KEYS,//API Keys CONFIGMODEL_ITEM_APPLIST_STYLE,//applist的显示样式 + CONFIGMODEL_ITEM_BLE_IOS_OR_ANDROID,//绑定设备为安卓或者ios 0:安卓 1:苹果 CONFIGMODEL_ITEM_MAX, + + } ; struct configmodel_item{ @@ -381,6 +384,7 @@ namespace OHOS {ConfigDataType::DATATYPE_BOOL, "is_first_time_start", "true" }, //CONFIGMODEL_ITEM_FIRSTTIMESTART true :手表第一次启动,否则不是 {ConfigDataType::DATATYPE_STRING, "open_ai_api_keys", "-1" }, //CONFIGMODEL_ITEM_OPEN_AI_API_KEYS {ConfigDataType::DATATYPE_STRING, "applist_style", "list" }, //CONFIGMODEL_ITEM_APPLIST_STYLE + {ConfigDataType::DATATYPE_INT, "android_or_ios", "0" },//CONFIGMODEL_ITEM_BLE_IOS_OR_ANDROID }; ConfigModel(); // 私有构造函数,确保无法在外部实例化 ConfigModel(const ConfigModel &) = delete; // 禁止拷贝构造 -- 2.17.1