本文主要是介绍【BLE】TLSR8258开发记录之5--SDK添加自定义服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、说明
在SDK V3.4 BLE sample中添加自定义服务,前期使用昇润APP,故需要符合他的server UUID。
实现打印功能需要先实现模拟串口的添加。
2、操作
A.在gatt_uuid.h中,添加定义服务定义
//MG add
#define SERVICE_UUID_MG_TMHR 0x1000//MG add
#define CHARACTERISTIC_UUID_MG_TMHR_RX 0x1001
#define CHARACTERISTIC_UUID_MG_TMHR_TX 0x1002
B.在app_config.h中,在ATT Handle define添加枚举定义
//MG addTMHR /**********************************************************************************************/MG_TMHR_PS_H,MG_INCLUDE_H, MG_TMHR_INPUT_CD_H,MG_TMHR_INPUT_DP_H,MG_TMHR_OUTPUT_CD_H,MG_TMHR_OUTPUT_DP_H,MG_TMHR_INPUT_CCB_H,
C.在app_att.c中添加UUID定义
MG TMHR /
static const u16 MG_TMHR_Service_UUID = SERVICE_UUID_MG_TMHR;
static const u16 MG_TMHR_RX_CharUUID = CHARACTERISTIC_UUID_MG_TMHR_RX;
static const u16 MG_TMHR_TX_CharUUID = CHARACTERISTIC_UUID_MG_TMHR_TX;
添加数据缓冲区(输入输出)
//MG add
static u8 MG_TMHR_In[2];
static u8 MG_TMHR_InCCC[2];
static u8 MG_TMHR_Out[2];
添加特征值定义
//MG add
static const u8 MG_TMHR_CharVal_RX[5] = {CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RSP | CHAR_PROP_NOTIFY,U16_LO(MG_TMHR_INPUT_DP_H),U16_HI(MG_TMHR_INPUT_DP_H),U16_LO(CHARACTERISTIC_UUID_MG_TMHR_RX),U16_HI(CHARACTERISTIC_UUID_MG_TMHR_RX)
};
static const u8 MG_TMHR_CharVal_TX[5] = {CHAR_PROP_READ | CHAR_PROP_NOTIFY,U16_LO(MG_TMHR_OUTPUT_DP_H),U16_HI(MG_TMHR_OUTPUT_DP_H),U16_LO(CHARACTERISTIC_UUID_MG_TMHR_TX),U16_HI(CHARACTERISTIC_UUID_MG_TMHR_TX)
};
添加自定义include服务
static const u16 MG_include[3] = {MG_TMHR_PS_H,MG_TMHR_INPUT_CCB_H,SERVICE_UUID_MG_TMHR};
在my_Attributes添加属性,其中“MG_TMHR_Write_CB”为写回调函数,即APP下发数据过来后,从机接收到数据后会进入该回调函数
以下代码添加的位置须与ATT Handle define中的"MG_TMHRxx"枚举位置相对应
// // MG TMHR ///TMHR data service{6,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), (u8*)(&MG_TMHR_Service_UUID),0},// include MG service{0,ATT_PERMISSIONS_READ,2,sizeof(MG_include),(u8*)(&hidIncludeUUID), (u8*)(MG_include), 0},//data RX{0,ATT_PERMISSIONS_READ,2,sizeof(MG_TMHR_CharVal_RX),(u8*)(&my_characterUUID), (u8*)(MG_TMHR_CharVal_RX), 0}, //prop{0,ATT_PERMISSIONS_RDWR,2,2,(u8*)(&MG_TMHR_RX_CharUUID), (u8*)(MG_TMHR_Out), &MG_TMHR_Write_CB}, //value//data TX{0,ATT_PERMISSIONS_READ,2,sizeof(MG_TMHR_CharVal_TX),(u8*)(&my_characterUUID), (u8*)(MG_TMHR_CharVal_TX), 0}, //prop{0,ATT_PERMISSIONS_READ,2, 2,(u8*)(&MG_TMHR_TX_CharUUID), (u8*)(MG_TMHR_In), 0}, //value{0,ATT_PERMISSIONS_RDWR,2,sizeof(MG_TMHR_InCCC),(u8*)(&clientCharacterCfgUUID), (u8*)(MG_TMHR_InCCC), 0}
回调函数定义如下:(测试用)
//写回调函数
//static unsigned char led_status= 0;
int MG_TMHR_Write_CB(void *p)
{rf_packet_att_data_t *pw = (rf_packet_att_data_t*)p;int len = pw->l2cap - 3;int i=0;//打印数据for(i=0; i<len; i++){printf("Data[%d] is: %d \r\n",i,pw->dat[i]);}//io翻转//gpio_toggle(GPIO_PD2);//回复数据bls_att_pushNotifyData(MG_TMHR_OUTPUT_DP_H,&pw->dat,len);return 1;
}
3、现象
打开手机调试APP(这里用的昇润APP,用其他的第三方软件也可以),连接上之后选择在发送端口发送:0506,可以在串口助手看到8258转发的0506
这篇关于【BLE】TLSR8258开发记录之5--SDK添加自定义服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!