您好,我正在尝试创建一个简单的BLE服务,我可以用来将消息记录到移动客户端。我正在从CTS服务中工作,但试图简化它。我已经包含下面的代码。我运行的问题是,当我通过手机连接到外围设备时,我不会看到下面定义的服务。但是,我确实查看了我设置的所有其他服务(设备信息,立即警报,链接丢失,TX电源,电池服务等)。我错过了什么?
log_service.h.
#include“osal.h”
#include“ble_service.h”
#include“ble_uuid.h”
#define uuid_service_log“6c8f78d9-292e-4b0d-b583-1616a777edbb”
#define uuid_current_log“b8ce3c4f-3ea0-47f3-8fb-f48732f99ba9”
typedef struct {
ble_service_t svc;
//处理
uint16_t log_val_h;//当前日志值
log_service_t;
ble_service_t * ls_init()
{
log_service_t * logservice;
返回&logservice-> svc;
uint16_t num_attr;
ATT_UUID_T UUID;
logservice = os_malloc(sizeof(* logservice));
memset(logservice,0,sizeof(* logservice));
/ *
*日志值的一个特征
* /
num_attr = ble_gatts_get_num_attr(0,1,0);
BLE_UUID_CREATE16(UUID_SERVICE_LOG,&UUID);
ble_gatts_add_service(&uuid,gatt_service_primary,num_attr);
// write是可选的,如果set_time_cb不为null使特性读/写
BLE_UUID_CREATE16(UUID_CURRENT_LOG,&UUID);
BLE_GATTS_ADD_CHAROURATIC(&UUID,GATT_PROP_READ | GATT_PROP_NOTIFY,
ATT_PERM_READ,512,
gatts_flag_char_read_req,null,&logservice-> log_val_h);
ble_gatts_register_service(&logservice-> svc.start_h,&logservice-> log_val_h,0);
logservice-> svc.end_h = logservice-> svc.start_h + num_attr;
ble_service_add(&logservice-> svc);
返回&logservice-> svc;
}
bluetooth_task.c.
......
ble_service_t * logservice = ls_init();
......

划伤。我想到了。对于尝试做类似的人来说,这是我可以提出的最赤裸的骨骼代码:
#包括
#包括
#include“osal.h”
#include“ble_att.h”
#include“ble_bufops.h”
#include“ble_common.h”
#include“ble_gatts.h”
#include“ble_uuid.h”
#include“ble_storage.h”
#include“ble_service.h”
#define uuid_service_log“您的服务UUID”
#define uuid_charactery_log“你的特征uuid”
static const char log_desc [] =“日志数据”;
typedef struct {
ble_service_t svc;
uint16_t log_val_h;// 当前时间
uint16_t log_ccc_h;
log_service_t;
静态void清理(ble_service_t * svc)
{
log_service_t * ls =(log_service_t *)svc;
ble_storage_remove_all(ls-> log_val_h);
ble_storage_remove_all(ls-> log_ccc_h);
OS_FREE(SVC);
}
staticat att_error_t handle_log_ccc_write(log_service_t * ls,uint16_t conn_idx,
UINT16_T偏移,UINT16_T长度,CONSS UINT8_T *值)
{
UINT16_T CCC;
if(offset){
returnat att_error_attribute_not_long;
}
if(length!= sizeof(ccc)){
returnat att_error_application_error;
}
ccc = get_u16(价值);
ble_storage_put_u32(conn_idx,ls-> log_cc_h,ccc,true);
返回att_error_ok;
}
静态void handle_write_req(ble_service_t * svc,const ble_evt_gatts_write_req_t * evt)
{
log_service_t * ls =(log_service_t *)svc;
ATT_ERROR_T Status = ATT_ERROR_ATTRIBUTE_NOT_FOUND;
uint16_t handle = evt->手柄;
if(handle == ls-> log_ccc_h){
status = handle_log_ccc_write(ls,evt-> conn_idx,EVT->偏移,EVT->长度,EVT-> Value);
}
BLE_GATTS_WRITE_CFM(EVT-> CONN_IDX,EVT->句柄,状态);
}
ble_service_t * log_init()
{
log_service_t * ls;
uint16_t num_attr,log_desc_h;
ATT_UUID_T UUID;
ls = os_malloc(sizeof(* ls));
memset(ls,0,sizeof(* ls));
num_attr = ble_gatts_get_num_attr(0,1,2);
BLE_UUID_FROM_STRING(UUID_SERVICE_LOG,&UUID);
ble_gatts_add_service(&uuid,gatt_service_primary,num_attr);
BLE_UUID_FROM_STRING(UUID_CHAROURATIC_LOG,&UUID);
ble_gatts_add_Charactery(
&Uuid,
gatt_prop_read |gatt_prop_notify,
att_perm_read,
512,
0,
空值,
&ls-> log_val_h
);
ble_uuid_create16(UUID_Gatt_Client_Char_Char_Configuration,&UUID);
ble_gatts_add_descriptor(&uuid,att_perm_rw,2,0,&ls-> log_ccc_h);
BLE_UUID_CREATE16(UUID_GATT_CHAR_USER_DESCRIPTION,&UUID);
ble_gatts_add_descriptor(&uuid,att_perm_read,sizeof(log_desc),0,&log_desc_h);
ble_gatts_register_service(&ls-> svc.start_h,&ls-> log_val_h,&ls-> log_ccc_h,&log_desc_h,0);
ble_gatts_set_value(ls-> log_val_h,4,“test”);
ble_gatts_set_value(log_desc_h,sizeof(log_desc),log_desc);
ls-> svc.cleanup =清理;
ls-> svc.end_h = ls-> svc.start_h + num_attr;
ls-> svc.write_req = handle_write_req;
ble_service_add(&ls-> svc);
返回ls-> svc;
}
void log_notify(ble_service_t * svc,uint16_t conn_idx,uint8_t * data,uint16_t len){
log_service_t * logservice =(log_service_t *)svc;
ble_gatts_send_event(conn_idx,logservice-> log_val_h,gatt_event_notification,len,data);
}
嗨alarner,
很高兴你识上你的问题。感谢您的指示,谢谢您接受对其他客户的非常有用的答案。
谢谢,PM_DIALOG.