1.蓝牙相关协议对接
authordxy <1731678236qq.com>
Thu, 14 Aug 2025 01:33:09 +0000 (09:33 +0800)
committerdxy <1731678236qq.com>
Thu, 14 Aug 2025 01:33:09 +0000 (09:33 +0800)
2.code/1.sdk/apps/BK_app/BK_drive/bk_audio.c
2.code/1.sdk/apps/BK_app/BK_drive/bk_uart.c
2.code/1.sdk/apps/BK_app/BK_drive/bk_uart.h
2.code/1.sdk/apps/common/third_party_profile/jieli/trans_data_demo/le_trans_data.c
2.code/1.sdk/apps/common/third_party_profile/jieli/trans_data_demo/le_trans_data.h
2.code/1.sdk/apps/common/user_protocol/ble_protocol.c
2.code/1.sdk/apps/watch/task_manager/bt/bt_emitter.c
2.code/1.sdk/apps/watch/task_manager/bt/bt_event_fun.c

index 67a22c9..3947dcd 100644 (file)
@@ -359,7 +359,7 @@ int audio_link_demo_post_mic(char *data, int len)
         if(rlen< len*2)//出掉多余数据
         {
 
-            log_info("%s:cbuf_write  fill rlen:%d!!!!!!",__FUNCTION__,rlen);
+            //log_info("%s:cbuf_write  fill rlen:%d!!!!!!",__FUNCTION__,rlen);
 
             char *tmp_buf = malloc(len*2);
             if(tmp_buf)
@@ -396,7 +396,7 @@ int write_iis_mic_data(char *data, int len)
         if(cbuf_is_write_able(adc_cbuffer,len)< len)//出掉多余数据
         {
 
-            log_info("%s:cbuf_write  fill!!!!!!",__FUNCTION__);
+            //log_info("%s:cbuf_write  fill!!!!!!",__FUNCTION__);
 
             char *tmp_buf = malloc(len);
             if(tmp_buf)
@@ -430,7 +430,7 @@ int audio_link_demo_RX_data(char *data, int len)
         if(rlen< len)//出掉多余数据
         {
 
-            log_info("%s:cbuf_write  fill rlen:%d!!!!!!",__FUNCTION__,rlen);
+            //log_info("%s:cbuf_write  fill rlen:%d!!!!!!",__FUNCTION__,rlen);
 
             char *tmp_buf = malloc(len);
             if(tmp_buf)
index 52abec1..9be46c2 100644 (file)
@@ -7,7 +7,7 @@
 #define LOG_INFO_ENABLE
 #define LOG_DUMP_ENABLE
 #include "debug.h"
-
+#include "le_trans_data.h"
 #include <stdlib.h>
 #include "cJSON.h"
 #include "bk_app.h"
@@ -270,7 +270,6 @@ int uart_send_AI_key_even(void)
         return -1;
     }
     // 打印JSON字符串
-    printf("JSON数据: %s\n", json_string);
     uart_send_blocking(ut, json_string, strlen(json_string), 20);
     // 释放内存
     cJSON_free(json_string);
@@ -288,6 +287,7 @@ int uart_send_PWR_key_even(void)
         return -1;
     }
     // 添加字符串字段 "event"
+
     cJSON_AddStringToObject(root, "event", "AI_PWR_PRESS");
     // 将JSON对象转换为字符串
     char *json_string = cJSON_Print(root);
@@ -296,7 +296,56 @@ int uart_send_PWR_key_even(void)
         cJSON_Delete(root);
         return -1;
     }
-    // 打印JSON字符串
+    
+    printf("JSON数据: %s\n", json_string);
+    uart_send_blocking(ut, json_string, strlen(json_string), 20);
+    // 释放内存
+    cJSON_free(json_string);
+    cJSON_Delete(root);
+    return 0;
+}
+
+int uart_send_single_bt_mac(u8 *name,u8 *mac,u8 connect)
+{
+    // {"event":"bt_connect_single",
+    //     "data":{
+    //         "name":"yeiwyei",
+    //         "mac":"89:89:78:78"
+    //     }
+    // }
+    cJSON *root = cJSON_CreateObject();
+    if (root == NULL) {
+        printf( "Failed to create JSON object\n");
+        return -1;
+    }
+    // 添加字符串字段 "event"
+    
+    cJSON_AddStringToObject(root, "event", "bt_connect_single");
+    cJSON *data = cJSON_CreateObject();
+    if (data == NULL) {
+        printf( "Failed to create JSON object\n");
+        cJSON_Delete(data);
+        return -1;
+    }
+    char mac_tmp[18]={0};
+    memset(mac_tmp,0,18);
+    sprintf((char *)mac_tmp,"%02X:%02X:%02X:%02X:%02X:%02X",mac[5],mac[4],mac[3],mac[2],mac[1],mac[0]);
+
+    cJSON_AddStringToObject(data, "name", (char *)name);
+    cJSON_AddStringToObject(data, "mac", (char *)mac_tmp);
+    if(connect){
+        cJSON_AddStringToObject(data, "state", "connected");
+    }else{
+        cJSON_AddStringToObject(data, "state", "disconnected");
+    }
+    cJSON_AddItemToObject(root, "data", data);
+    
+    char *json_string = cJSON_Print(root);
+    if (json_string == NULL) {
+        printf( "Failed to print JSON\n");
+        cJSON_Delete(root);
+        return -1;
+    }
     printf("JSON数据: %s\n", json_string);
     uart_send_blocking(ut, json_string, strlen(json_string), 20);
     // 释放内存
@@ -304,6 +353,42 @@ int uart_send_PWR_key_even(void)
     cJSON_Delete(root);
     return 0;
 }
+int uart_send_bt_list(u8 *data)
+{
+    u16 len_tmp = 0;
+    u16 data_flag = 1;
+    u16 date_len = strlen(data);
+    if (date_len >= 512){
+        while (data_flag)
+        {
+            //531
+            if (date_len >= 512){
+                printf("uart_send_bt_list 1");
+                
+                uart_send_blocking(ut, data+len_tmp, 512, 200);
+                len_tmp+=512;
+            }else{
+                if(date_len >= 0){
+                    printf("uart_send_bt_list 2");
+                    uart_send_blocking(ut, data+len_tmp, date_len, 200);
+                    len_tmp+=date_len;
+                }
+                break; //最后一包
+            }
+            //532
+            date_len=date_len-512;
+            if(date_len <= 0){
+                data_flag = 0;
+                break;
+            }
+            // delay_2ms(100);
+            //mdelay(100);
+        }
+        return 0;
+    }
+    uart_send_blocking(ut, data, strlen(data), 20);
+    return 0;
+}
 int uart_send_ack(void)
 {
     cJSON *root = cJSON_CreateObject();
@@ -348,7 +433,7 @@ int parse_uart_data(char *json_string)
     
     cJSON *event = cJSON_GetObjectItemCaseSensitive(root, "event");
     if (cJSON_IsString(event) && (event->valuestring != NULL)) {
-        printf("解析结果: event = %s\n", event->valuestring);
+        printf("解析结果: event = %s json_string:%s\n", event->valuestring,json_string);
         // if(strcmp(event->valuestring,"AI_start"))//ai开始
         // {
 
@@ -394,6 +479,90 @@ int parse_uart_data(char *json_string)
             ack_flag = 1;
 
         }
+        if(!strcmp(event->valuestring,"switch_bt_state")) //收到4g传过来的ble开关状态 0 打开蓝牙扫描 1关闭蓝牙扫描
+        {
+            // {
+            //     "event":"switch_bt_state",
+            //     "data":{
+            //         "switch_bt_state":"0"
+            //     }
+            // }
+            printf(": json_string = %s\n", json_string);
+
+            // 获取"data"对象
+            cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
+            if (cJSON_IsObject(data)) {
+                // 从data对象中获取"switch_bt_state"字段
+                cJSON *switch_bt_state = cJSON_GetObjectItemCaseSensitive(data, "switch_bt_state");
+                if (cJSON_IsString(switch_bt_state) && (switch_bt_state->valuestring != NULL)) {
+                    printf("蓝牙状态: %s\n", switch_bt_state->valuestring);
+                    if (strcmp(switch_bt_state->valuestring, "0") == 0) {
+                        set_adv_flag_fun(0) ;//开始蓝牙广播
+                        // uart_send_PWR_key_even();
+                    }else if (strcmp(switch_bt_state->valuestring, "1") == 0)
+                    {
+                        set_adv_flag_fun(1); //关闭蓝牙广播
+                    }
+                }
+            }
+            
+        }
+        if(!strcmp(event->valuestring,"bt_scan")) 
+        {
+            // "event":"bt_scan",
+            // "data":"0/1" 0开始扫描 1停止扫描
+            cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
+            if (cJSON_IsString(data) && (data->valuestring != NULL)) {
+                printf("蓝牙扫描: %s\n", data->valuestring);
+                if (strcmp(data->valuestring, "0") == 0) {
+                    set_scan_bt(0);
+                    start_scan_bt();//开始扫描
+                }else if (strcmp(data->valuestring, "1") == 0)
+                {
+                    set_scan_bt(1);
+                    stop_scan_bt(); //关闭扫描
+                }
+            }
+        }
+        if(!strcmp(event->valuestring,"connect_bt")) {
+            // json_data = \
+            // {
+            //     "event": "connect_bt",
+            //     "data":msg
+            // }
+            cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
+            if (cJSON_IsString(data) && (data->valuestring != NULL)) {
+                printf("蓝牙连接: %s\n", data->valuestring);
+                connect_bt(json_string,0);//开始扫描
+                
+            }
+        }
+        if(!strcmp(event->valuestring,"imei")) {
+            // json_data = \
+            // {
+            //     "event": "connect_bt",
+            //     "data":msg
+            // }
+            cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
+            if (cJSON_IsString(data) && (data->valuestring != NULL)) {
+                printf("imei: %s\n", data->valuestring);
+                set_adv_imei(data->valuestring);//开始扫描
+                
+            }
+        }
+        if(!strcmp(event->valuestring,"disconnect_bt")) {
+            // json_data = \
+            // {
+            //     "event": "disconnect_bt",
+            //     "data":msg
+            // }
+            cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
+            if (cJSON_IsString(data) && (data->valuestring != NULL)) {
+                printf("断开连接: %s\n", data->valuestring);
+                connect_bt(json_string,1);//断开连接
+                
+            }
+        }
         if(ack_flag == 1)
         {
             ack_flag = 0;
index 3c00b16..ff8d8b5 100644 (file)
@@ -15,4 +15,6 @@ void bk_uart_init();
 int uart_send_AI_key_even(void);
 int uart_send_PWR_key_even(void);
 void bk_uart_send_smg(bk_uart_msg_event event,char *data,int data_len);
+int uart_send_bt_list(u8 *data);
+int uart_send_single_bt_mac(u8 *name,u8 *mac,u8 connect);
 #endif
index f448caf..1254e54 100644 (file)
 #include "custom_cfg.h"
 #include "btstack/btstack_event.h"
 #include "ble_protocol.h"
-
-
+#include "bt_emitter.h"
+#include "bk_drive/bk_uart.h"
+#include "cJSON.h"
+#include "btstack/avctp_user.h"
 #if (TCFG_BLE_DEMO_SELECT == DEF_BLE_DEMO_TRANS_DATA)
 // #include "rcsp_bluetooth.h" // 该头文件属于旧RCSP模块,后续开发需要废弃接入新的RCSP模块,by lingxuanfeng
 
@@ -157,7 +159,7 @@ static u8 scan_rsp_data[ADV_RSP_PACKET_MAX];//max is 31
 static char gap_device_name[BT_NAME_LEN_MAX] = "jl_ble_test";
 static u8 gap_device_name_len = 0; //名字长度,不包含结束符
 static u8 ble_work_state = 0;      //ble 状态变化
-static u8 adv_ctrl_en;             //广播控制
+static u8 adv_ctrl_en = 0;             //广播控制
 
 static u8 test_read_write_buf[4];
 
@@ -183,7 +185,253 @@ extern void clr_wdt(void);
 extern void sys_auto_shut_down_disable(void);
 extern void sys_auto_shut_down_enable(void);
 extern u8 get_total_connect_dev(void);
+static u8 set_adv_flag = 0;//默认开启蓝牙广播
+static char adv_imei[17]={0};
+//4G发送蓝牙扫描标识 0 开启 1关闭
+void set_adv_flag_fun(u8 flag){
+    set_adv_flag = flag;
+    printf("set_adv_flag_fun!!!!!!!!!!!%d",flag);
+    switch (flag)
+    {
+        case 0: //需要打开蓝牙广播 
+            if (!adv_ctrl_en){
+                printf("bt_ble_init!!!!!!!!!!!");
+                bt_ble_init();
+                //当前广播已开启需要
+            }
+            break;
+        case 1://需要关闭蓝牙广播
+            if (adv_ctrl_en){
+                printf("bt_ble_exit!!!!!!!!!!!");
+                bt_ble_exit();
+                //已经关闭了广播就不需要关了
+            }
+            break;
+        
+        default:
+            break;
+    }
+    
+}
+static u8 get_adv_flag_fun(void){
+    return set_adv_flag;
+}
+
+//
+static u8 scan_state = 0;
+void set_scan_bt(u8 flag){
+    scan_state=flag;
+}
+extern u8 bt_connect;
+void start_scan_bt(void){
+    if (scan_state){
+        return;
+    }
+    if (bt_connect) { //搜索前断开已连接得蓝牙耳机
+        user_emitter_cmd_prepare(USER_CTRL_DISCONNECTION_HCI, 0, NULL);
+    }
+    bt_emitter_start_search_device(); //开始扫描
+}
+void set_adv_imei(char *imei){
+    memcpy((void *)adv_imei,(void *)imei,16);
+    bt_ble_init();
+}
+void stop_scan_bt(void){
+    
+    bt_emitter_stop_search_device();//停止扫描
+    scan_state=0;
+}
+///ble回复扫描耳机列表
+void bt_scan_list(void) //扫描完成发送设备列表->小程序
+{
+    if(!scan_state)
+    {
+        return;
+    }
+    printf("scan_state:%d",scan_state);
+    // if(!scan_state){
+    //     return;
+    // }
+     // 遍历链表
+    //     {
+    //     "cmd":"bt_scan_rest",
+    //     "data":[
+    //         {
+    //             "mac":"mac地址",
+    //             "name":"设备名称",
+    //             "rssi":"信号强度"
+    //         }
+    //     ]
+    // }
+    scan_state = 0;
+    cJSON *root = cJSON_CreateObject();
+    if(root ==NULL){
+        clear_dev_list();
+        return;
+    }
+
+    cJSON_AddStringToObject(root,"event","bt_scan_rest");
+    cJSON *data_array = cJSON_CreateArray();
+    if(data_array == NULL)
+    {
+        return;
+    }
+    cJSON_AddItemToObject(root, "data", data_array);
+     // 4. 向数组中添加对象
+    // 第一个设备对象
+    
+    
+    struct inquiry_noname_remote *remote1;
+    if(!list_empty(&inquiry_noname_list)){
+        char addr_tmp[18] = {0};//09:98:99:97:67:78
+        char rssi_tmp[5] = {0};//-128-128
+        list_for_each_entry(remote1, &inquiry_noname_list, entry) {
+            memset(addr_tmp,0,18);
+            memset(rssi_tmp,0,5);
+            cJSON *device1 = cJSON_CreateObject();
+            if(device1 == NULL){
+                continue;
+            }
+            printf("addr: %s rssi:%d\n", remote1->addr, remote1->rssi);
+            snprintf(rssi_tmp,4,"%d", remote1->rssi);
+            snprintf(addr_tmp,18,"%02x:%02x:%02x:%02x:%02x:%02x", remote1->addr[5], remote1->addr[4], remote1->addr[3], remote1->addr[2], remote1->addr[1], remote1->addr[0]);
+            // cJSON_AddStringToObject(device1, "rssi", rssi_tmp);
+            cJSON_AddNumberToObject(device1, "rssi", remote1->rssi);
+            cJSON_AddStringToObject(device1, "mac", addr_tmp);
+            cJSON_AddStringToObject(device1, "name", remote1->name);
+            cJSON_AddItemToArray(data_array, device1);
+        }
+    }
+    
+    // 5. 转换为JSON字符串
+    char *json_str = cJSON_PrintUnformatted(root);
+    if (json_str == NULL) {
+        printf("json_str is not\r\n");
+        cJSON_Delete(root);
+        return;
+    }
+    // 6. 输出JSON字符串并释放内存
+    //to->uart
+    uart_send_bt_list(json_str);
+    printf("生成的JSON:\n%s json_str:%d\n", json_str,strlen(json_str));
+    cJSON_Delete(root); // 释放整个JSON结构的内存
+    //使用完之后清空数据
+    clear_dev_list();
+    cJSON_free(json_str);
+}
+static int str_to_u8_buf(const char *str, u8 *buf) {
+    if (str == NULL || buf == NULL) {
+        return -1; // 空指针错误
+    }
+
+    int str_len = strlen(str);
+    int buf_idx = 0;  // 数组索引(0~5)
+    int str_idx = 0;  // 字符串索引
+
+    while (buf_idx < 6 && str_idx < str_len) {
+        // 读取高位十六进制字符(如"89"中的'8')
+        if (str_idx >= str_len) break;
+        char high = str[str_idx++];
+        // 读取低位十六进制字符(如"89"中的'9')
+        if (str_idx >= str_len) break;
+        char low = str[str_idx++];
+
+        // 转换高位字符为数值
+        u8 h_val;
+        if (high >= '0' && high <= '9') {
+            h_val = high - '0';
+        } else if (high >= 'A' && high <= 'F') {
+            h_val = 10 + high - 'A';
+        } else if (high >= 'a' && high <= 'f') {
+            h_val = 10 + high - 'a';
+        } else {
+            return -2; // 高位字符无效
+        }
+
+        // 转换低位字符为数值
+        u8 l_val;
+        if (low >= '0' && low <= '9') {
+            l_val = low - '0';
+        } else if (low >= 'A' && low <= 'F') {
+            l_val = 10 + low - 'A';
+        } else if (low >= 'a' && low <= 'f') {
+            l_val = 10 + low - 'a';
+        } else {
+            return -3; // 低位字符无效
+        }
 
+        // 组合为一个字节(高位<<4 + 低位)
+        buf[buf_idx++] = (h_val << 4) | l_val;
+
+        // 跳过分隔符':'(最后一组后无分隔符)
+        if (buf_idx < 6) {
+            if (str_idx < str_len && str[str_idx] == ':') {
+                str_idx++; // 跳过':'
+            } else {
+                return -4; // 缺少分隔符
+            }
+        }
+    }
+
+    // 检查是否成功填充6个字节
+    return (buf_idx == 6) ? 0 : -5;
+}
+extern u8 bt_connect;
+extern u8 bt_conn_addr[6];//蓝牙连接成功的地址
+void connect_bt(char *json_data,u8 conect){
+    //  json_data = \
+    //     {
+    //         "event": "connect_bt",
+    //         "data":msg
+    //     }
+    if(conect){ //断开连接
+        if (bt_connect) { //搜索前断开已连接得蓝牙耳机
+            user_emitter_cmd_prepare(USER_CTRL_DISCONNECTION_HCI, 0, NULL);
+        }
+        return;
+    }
+    //连接耳机
+    cJSON *proot = cJSON_Parse(json_data);
+    if (proot == NULL) {
+        printf("JSON解析失败\n");
+        return;
+    }
+    cJSON *data = cJSON_GetObjectItemCaseSensitive(proot, "data");
+    if (cJSON_IsString(data) && (data->valuestring != NULL)) {
+        cJSON *root = cJSON_CreateObject();
+        if(root == NULL){
+            return;
+        }   
+        u8 maddr[6]; // 目标数组
+        u8 reversed_mac[6]; // 反序数组
+        memset(reversed_mac,0,6);
+        memset(maddr,0,6);
+        printf("mac->valuestring %s\r\n",data->valuestring);
+        //75:e8:8c:62:9a:df
+        
+        int ret = str_to_u8_buf(data->valuestring, maddr);
+        if (ret != 0) {
+            printf("转换失败,错误码:%d\n", ret);
+            return;
+        }
+        for(u8 i=0;i<6;i++)//需要反序
+        {
+            reversed_mac[i]=maddr[5-i];
+        }
+        printf("bt_connect:%d bt_conn_addr %x %x %x %x %x %x\r\n",bt_connect,bt_conn_addr[0],bt_conn_addr[1],bt_conn_addr[2],bt_conn_addr[3],bt_conn_addr[4],bt_conn_addr[5]);
+        printf("maddr %x %x %x %x %x %x\r\n",maddr[0],maddr[1],maddr[2],maddr[3],maddr[4],maddr[5]);
+        if(bt_connect == 1) //已经连上了看看连接的地址是否一致
+        {
+            if (memcmp(bt_conn_addr, reversed_mac, 6) != 0){//如果连上了并且和小程序下发得mac地址是一致不需要重新连接
+                user_emitter_cmd_prepare(USER_CTRL_DISCONNECTION_HCI, 0, NULL); //
+                emitter_bt_connect(reversed_mac);//连接地址
+            }
+        }else{
+            emitter_bt_connect(reversed_mac);//连接地址
+        }  
+    }
+}
+extern u8 get_total_connect_dev(void);
 //------------------------------------------------------
 //ANCS
 #if TRANS_ANCS_EN
@@ -590,7 +838,7 @@ static void cbk_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p
             ble_op_att_send_init(con_handle, 0, 0, 0);
             set_ble_work_state(BLE_ST_DISCONN);
 
-            if (!ble_update_get_ready_jump_flag()) {
+            if (get_adv_flag_fun()) { //
                 bt_ble_adv_enable(1);
             }
             connection_update_cnt = 0;
@@ -870,7 +1118,7 @@ static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_h
         //收发测试,自动发送收到的数据;for test
         if (app_send_user_data_check(buffer_size)) {
         
-            ble_app_recvice_process_fun(handle,buffer,buffer_size);
+            // ble_app_recvice_process_fun(handle,buffer,buffer_size);
             // app_send_user_data(ATT_CHARACTERISTIC_FF0A_01_VALUE_HANDLE, buffer, buffer_size, ATT_OP_AUTO_READ_CCC);
         }
         break;
@@ -975,15 +1223,18 @@ static int make_set_adv_data(void)
     manufacturer[manufacturer_len++] = 0xff;
 
     //mac 6个字节
+
     const uint8_t *edr_addr = bt_get_mac_addr();//bt_get_mac_addr();
-    manufacturer[manufacturer_len++] = edr_addr[5];
-    manufacturer[manufacturer_len++] = edr_addr[4];
-    manufacturer[manufacturer_len++] = edr_addr[3];
-    manufacturer[manufacturer_len++] = edr_addr[2];
-    manufacturer[manufacturer_len++] = edr_addr[1];
-    manufacturer[manufacturer_len++] = edr_addr[0];
-
-    //END
+    // char adv_imei[16] ="86 01 32 06 60 87 81 88"; 86 01 32 06 61 07 04 6
+    // manufacturer[manufacturer_len++] = edr_addr[5];
+    // manufacturer[manufacturer_len++] = edr_addr[4];
+    // manufacturer[manufacturer_len++] = edr_addr[3];
+    // manufacturer[manufacturer_len++] = edr_addr[2];
+    // manufacturer[manufacturer_len++] = edr_addr[1];
+    // manufacturer[manufacturer_len++] = edr_addr[0];
+    // char *adv_imei1 = "860132066107046";
+    memcpy(manufacturer+manufacturer_len,(void *)adv_imei,strlen(adv_imei));
+    manufacturer_len=manufacturer_len+strlen(adv_imei);
     manufacturer[manufacturer_len++] = 0xAB;
     manufacturer[manufacturer_len++] = 0xCD;
 
@@ -1001,6 +1252,7 @@ static int make_set_adv_data(void)
 
     if (offset > ADV_RSP_PACKET_MAX) {
         printf("***adv_data overflow!!!!!!\n");
+        log_info_hexdump(buf, offset);
         return -1;
     }
     printf("adv_data(%d):", offset);
@@ -1431,9 +1683,22 @@ static void timer_trans_flow_test(void)
 }
 
 static const char ble_ext_name[] = "(BLE)";
-
+static u8 adv_start = 0;
 void bt_ble_init(void)
 {
+    printf("set_adv_flag:%d adv_imei:%s",set_adv_flag,adv_imei);
+    if((memcmp(adv_imei, "\0\0\0\0\0\0", 6) == 0)) //没有imei广播 
+    {
+        return;
+    }
+    if(set_adv_flag == 1)
+    {
+        return;
+    }
+    if(adv_start)
+    {
+        return;
+    }
     log_info("***** ble_init******\n");
     log_info("ble_file: %s", __FILE__);
     char *name_p;
@@ -1472,6 +1737,7 @@ void bt_ble_init(void)
 #if TEST_SEND_DATA_RATE
     server_timer_start();
 #endif
+    adv_start = 1;
 }
 
 void bt_ble_exit(void)
@@ -1479,6 +1745,7 @@ void bt_ble_exit(void)
     log_info("***** ble_exit******\n");
 
     ble_module_enable(0);
+    adv_start = 0;
 
 #if TEST_SEND_DATA_RATE
     server_timer_stop();
index 3f6f36c..b56c219 100644 (file)
@@ -79,4 +79,12 @@ static const uint8_t profile_data[] =
 void bt_ble_init(void);
 int app_send_user_data_todo(u16 handle, u8 *data, u16 len, u8 handle_type);
 ble_state_e usr_get_ble_work_state(void);
+void bt_ble_exit(void); //关闭蓝牙扫描
+void set_adv_flag_fun(u8 flag);//设置广播开启状态
+void start_scan_bt(void);
+void stop_scan_bt(void);
+void set_scan_bt(u8 flag);
+void bt_scan_list(void);
+void connect_bt(char *json_data,u8 conect);//连接目标蓝牙耳机
+void set_adv_imei(char *imei);
 #endif
index 869d361..9cfa524 100644 (file)
@@ -99,7 +99,7 @@ u16 get_mtu_size(void){
 void set_mtu_size(u16 mtu){\r
     mtu_size = mtu;\r
 }\r
-int str_to_u8_buf(const char *str, u8 *buf) {\r
+static int str_to_u8_buf(const char *str, u8 *buf) {\r
     if (str == NULL || buf == NULL) {\r
         return -1; // 空指针错误\r
     }\r
index 1211d32..3f76d98 100644 (file)
@@ -73,7 +73,7 @@
 
 #define EMITTER_CONNECT_TIMEOUT_MS                     (60*1000)
 
-#define BK_USER_CUSTOM_FUN  (0) //用户更改的 
+#define BK_USER_CUSTOM_FUN  (1) //用户更改的 
 
 struct list_head inquiry_noname_list;
 struct remote_name {
@@ -138,6 +138,7 @@ void bt_search_device(void)
         /* log_info("bt_search_busy >>>>>>>>>>>>>>>>>>>>>>>\n"); */
         //return;
     }
+    printf("bt_search_device:");
     read_name_start = 0;
     bt_search_busy = BT_SCAN_SCANING;
     u8 inquiry_length = SEARCH_DEVICE_TIMER_OUT;   // inquiry_length * 1.28s
@@ -252,6 +253,36 @@ void bt_emitter_start_search_device()
     if (bt_user_priv_var.emitter_or_receiver != BT_EMITTER_EN) {
         return ;
     }
+
+    while (hci_standard_connect_check() == 0x80) {
+        //wait profile connect ok;
+        if (get_curr_channel_state()) {
+            break;
+        }
+        os_time_dly(10);
+    }
+
+    ////断开链接
+    if (get_curr_channel_state() != 0) {
+        user_send_cmd_prepare(USER_CTRL_POWER_OFF, 0, NULL);
+    } else {
+        if (hci_standard_connect_check()) {
+            user_send_cmd_prepare(USER_CTRL_PAGE_CANCEL, 0, NULL);
+            user_send_cmd_prepare(USER_CTRL_CONNECTION_CANCEL, 0, NULL);
+        }
+    }
+    /* if there are some connected channel ,then disconnect*/
+    while (hci_standard_connect_check() != 0) {
+        //wait disconnect;
+        os_time_dly(10);
+    }
+
+    ////关闭可发现可链接
+    user_send_cmd_prepare(USER_CTRL_WRITE_SCAN_DISABLE, 0, NULL);
+    user_send_cmd_prepare(USER_CTRL_WRITE_CONN_DISABLE, 0, NULL);
+    ////切换样机状态
+    bt_search_device();
+    return;
 #if 0
     while (hci_standard_connect_check() == 0x80) {
         //wait profile connect ok;
@@ -382,13 +413,14 @@ void emitter_or_receiver_switch(u8 flag)
 #if !TCFG_USER_BLE_CTRL_BREDR_EN
 #if 1
         if (connect_last_source_device_from_vm())
+        // if(0)
         {
-            /* log_info("start connect device vm addr\n"); */
+            printf("start connect device vm addr\n");
         } else {
 #ifdef BT_EMITTER_TEST
             bt_search_device();
 #endif
-                       bt_search_device();
+                       // bt_search_device();
             user_send_cmd_prepare(USER_CTRL_WRITE_SCAN_ENABLE, 0, NULL);
             user_send_cmd_prepare(USER_CTRL_WRITE_CONN_ENABLE, 0, NULL);
         }
@@ -550,7 +582,7 @@ void emitter_search_stop(u8 result)
     search_spp_device = 0;
     set_start_search_spp_device(0);
     #if BK_USER_CUSTOM_FUN
-    ble_ack_bt_scan_list();
+    bt_scan_list();
     printf("emitter_search_stop:%d",result);
     return;
     #endif
index 487e39c..c33a6f7 100644 (file)
@@ -523,6 +523,7 @@ void phone_sync_vol(void *priv)
                         命令来
 */
 /*----------------------------------------------------------------------------*/
+#include "bk_drive/bk_uart.h"
 char userremote_name[32]={0};
 u8 bt_conn_addr[6]={0};//蓝牙连接成功的地址
 void bt_read_remote_name(u8 status, u8 *addr, u8 *name)
@@ -534,9 +535,12 @@ void bt_read_remote_name(u8 status, u8 *addr, u8 *name)
     }
 
     log_debug_hexdump(addr, 6);
-
+    if(strlen(name) == 0)
+    {
+        sprintf(name,"%s","bt_ble");
+    }
     sprintf(bt_remote_name, "%s", name);
-     u8 name_len = 0;
+    u8 name_len = 0;
     name_len = strlen((char *)name);
     if(name_len > 31)
     {
@@ -545,10 +549,16 @@ void bt_read_remote_name(u8 status, u8 *addr, u8 *name)
     memset(userremote_name,0,32);
     memcpy(userremote_name,(char *)name,name_len);
     userremote_name[name_len]='\0';
-    memset(bt_conn_addr,0,6);
-    
-    memcpy(bt_conn_addr, addr, 6);
     printf("addr!!!!!!!!!%x %x %x %x %x %x",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
+    if(memcmp(bt_conn_addr,addr,6) != 0)
+    {
+        
+        uart_send_single_bt_mac(name,addr,1);
+    }
+    memcpy(bt_conn_addr, addr, 6);
+    
+
+    
     printf("bt_conn_addr!!!!!!!!!%x %x %x %x %x %x",bt_conn_addr[0],bt_conn_addr[1],bt_conn_addr[2],bt_conn_addr[3],bt_conn_addr[4],bt_conn_addr[5]);
 #if TCFG_USER_EMITTER_ENABLE
     emitter_search_noname(status, addr, name);
@@ -1243,10 +1253,11 @@ u8 bt_connect = BT_CONN_STATE_INIT;
    @note
 */
 /*----------------------------------------------------------------------------*/
+static u8 mac_addr[6]={0};
 void bt_status_connect(struct bt_event *bt)
 {
     bt_connect = BT_CONN_STATE_CONNECTED;
-    connect_bt_cb(bt_connect);
+    // connect_bt_cb(bt_connect);
     sys_auto_sniff_controle(1, NULL);
     sys_auto_shut_down_disable();
 
@@ -1258,9 +1269,13 @@ void bt_status_connect(struct bt_event *bt)
     }
 #endif
 
-    static u8 mac_addr[6];
-    memcpy(mac_addr, bt->args, 6);
-    UI_MSG_POST("bt_connect_info:mac=%4", mac_addr);
+    // static u8 mac_addr[6];
+    // if(memcmp(mac_addr,bt->args,6) != 0)
+    // {
+
+    // }
+    // memcpy(mac_addr, bt->args, 6);
+    printf("bt_connect_info:mac=%x %x %x %x %x %x",bt->args[0],bt->args[1],bt->args[2],bt->args[3],bt->args[4],bt->args[5]);
 
 #if TCFG_USER_EMITTER_ENABLE
 #if TCFG_UI_ENABLE && TCFG_SPI_LCD_ENABLE
@@ -2435,12 +2450,12 @@ void bt_hci_event_page_timeout(struct bt_event *bt)
 #if TCFG_USER_TWS_ENABLE
     bt_tws_phone_page_timeout();
 #else
-    if (bt_emitter_en) {
-        if (bt_user_priv_var.emitter_or_receiver == BT_EMITTER_EN) {
-            bt_emitter_start_search_device();
-            return;
-        }
-    }
+    // if (bt_emitter_en) {
+    //     if (bt_user_priv_var.emitter_or_receiver == BT_EMITTER_EN) {
+    //         bt_emitter_start_search_device();
+    //         return;
+    //     }
+    // }
     /* if (!bt_user_priv_var.auto_connection_counter) { */
     if (bt_user_priv_var.auto_connection_timer) {
         sys_timeout_del(bt_user_priv_var.auto_connection_timer);