版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jet007_007/article/details/52935238
在这里,首先以BLE串口透析中添加的服务为例子来概述一下。
参照他人的博客,在BLE串口透析例程中是通过如下协议栈函数将服务添加到协议栈内部表中的。
uint32_t sd_ble_gatts_service_add | ( | uint8_t | type, |
ble_uuid_t const *const | p_uuid, | ||
uint16_t *const | p_handle | ||
) |
Add a service declaration to the local server ATT table.
- Parameters
-
[in] type Toggles between primary and secondary services, see BLE_GATTS_SRVC_TYPES. [in] p_uuid Pointer to service UUID. [out] p_handle Pointer to a 16-bit word where the assigned handle will be stored.
- Note
- Secondary Services are only relevant in the context of the entity that references them, it is therefore forbidden to add a secondary service declaration that is not referenced by another service later in the ATT table.
1、uint8_t type;在初级和次级服务之间的开关,其值有如下三种情况:
/** @defgroup BLE_GATTS_SRVC_TYPES GATT Server Service Types
* @{ */
#define BLE_GATTS_SRVC_TYPE_INVALID 0x00 /**< Invalid Service Type. */
#define BLE_GATTS_SRVC_TYPE_PRIMARY 0x01 /**< Primary Service. */
#define BLE_GATTS_SRVC_TYPE_SECONDARY 0x02 /**< Secondary Type. */
/** @} */
1、BLE_GATTS_SRVC_TYPE_INVALID:无效的服务类型
2、BLE_GATTS_SRVC_TYPE_PRIMARY:初级服务类型
3、BLE_GATTS_SRVC_TYPT_SECONDARY:次级服务类型
2、ble_uuid_t const * const p_uuid:服务UUID的指针
3、uint16_t *const p_handle:指向存储有分配的句柄的16位字的指针
添加特征值
一个服务包含若干个特征值,添加完服务之后,要向服务中添加需要的特征值。
还是BLE串口透析的例子,众所周知,串口肯定包含发送和接收,因此,在该例子中添加了两个特征值,接收特征值和发送特征值(rx_char_add和tx_char_add)。
1、rx_char_add,这个特征值用来将板子从串口收到的数据通过该特征值使用notify方式发送给手机。这里接收特征值的接收指的是串口的接收,对蓝牙来说是发送。
/**@brief Function for adding RX characteristic.
*
* @param[in] p_nus Nordic UART Service structure.
* @param[in] p_nus_init Information needed to initialize the service.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t rx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
/**@snippet [Adding proprietary characteristic to S110 SoftDevice] */
ble_gatts_char_md_t char_md;
ble_gatts_attr_md_t cccd_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t ble_uuid;
ble_gatts_attr_md_t attr_md;
memset(&cccd_md, 0, sizeof(cccd_md));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
cccd_md.vloc = BLE_GATTS_VLOC_STACK;
memset(&char_md, 0, sizeof(char_md));
char_md.char_props.notify = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = &cccd_md;
char_md.p_sccd_md = NULL;
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
memset(&attr_md, 0, sizeof(attr_md));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 1;
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = sizeof(uint8_t);
attr_char_value.init_offs = 0;
attr_char_value.max_len = BLE_NUS_MAX_RX_CHAR_LEN;
return sd_ble_gatts_characteristic_add(p_nus->service_handle,
&char_md,
&attr_char_value,
&p_nus->rx_handles);
/**@snippet [Adding proprietary characteristic to S110 SoftDevice] */
}
未完待续