下面总结下nrf52832怎么添加uuid.
可以做一下分类
这2类又各可以分成下面2个
在总结之前先看下nordic提供的关于uuid的2个结构体
typedef struct
{
uint8_t uuid128[16]; /**< Little-Endian UUID bytes. */
} ble_uuid128_t;
typedef struct
{
uint16_t uuid; /**< 16-bit UUID value or octets 12-13 of 128-bit UUID. */
uint8_t type; /**< UUID type, see @ref BLE_UUID_TYPES. If type is @ref BLE_UUID_TYPE_UNKNOWN, the value of uuid is undefined. */
} ble_uuid_t;
ble_uuid128_t 是定义128bit(16字节)uuid的结构体,采用小端模式
ble_uuid_t 是定义16bit(2字节)uuid的结构体,还用一个type元素标识是那种类型的uuid,type可以取以下值,
#define BLE_UUID_TYPE_UNKNOWN 0x00 /**< Invalid UUID type. */
#define BLE_UUID_TYPE_BLE 0x01 /**< Bluetooth SIG UUID (16-bit). */
#define BLE_UUID_TYPE_VENDOR_BEGIN 0x02 /**< Vendor UUID types start at this index (128-bit). */
比如心电的例子中
心电服务是SIG定义好的服务,所以添加SIG定义uuid
ble_uuid_t ble_uuid;
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_SERVICE);
这个宏展开如下
#define BLE_UUID_BLE_ASSIGN(instance, value) do {\
instance.type = BLE_UUID_TYPE_BLE; \
instance.uuid = value;} while(0)
就是把结构体里的type赋值成BLE_UUID_TYPE_BLE (SIG),16bit uuid赋值成BLE_UUID_HEART_RATE_SERVICE
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid,
&p_hrs->service_handle);
BLE_GATTS_SRVC_TYPE_PRIMARY 标识该申明的服务是首要服务
&ble_uuid 就是16bit uuid 结构体地址
&p_hrs->service_handle 是这个申明服务的操作句柄
ble_uuid_t ble_uuid;
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_MEASUREMENT_CHAR);
就是把结构体里的type赋值成BLE_UUID_TYPE_BLE (SIG),16bit uuid赋值成BLE_UUID_HEART_RATE_MEASUREMENT_CHAR
//属性值
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 = hrm_encode(p_hrs, INITIAL_VALUE_HRM, encoded_initial_hrm);
attr_char_value.init_offs = 0;
attr_char_value.max_len = MAX_HRM_LEN;
attr_char_value.p_value = NULL;//encoded_initial_hrm;
return sd_ble_gatts_characteristic_add(p_hrs->service_handle,
&char_md,
&attr_char_value,
&p_hrs->hrm_handles);
p_hrs->service_handle 是这个特性所属服务的句柄
&char_md 是特性标识符结构体地址
&attr_char_value 是特性值结构体地址,其中attr_char_value.p_uuid就是指向ble_uuid(16bit uuid)的地址
&p_hrs->hrm_handles 是这个特性的句柄
蓝牙串口的例子就是自定义的uuid
#define NUS_BASE_UUID {{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}}
/**< Used vendor specific UUID. */
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
这里定义的同时赋值了,注意是小端字节序
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
&p_nus->uuid_type 会返回添加进去的uuid类型
ble_uuid.type = p_nus->uuid_type; //类型
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;//服务的uuid
// Add the service.
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid,
&p_nus->service_handle);
和前面特性uuid添加一样,如下
//1.定义16bit uuid结构体
ble_uuid_t ble_uuid;
//2.赋值
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
//3.特性申明
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);
总结下,
sd_ble_uuid_vs_add
把基础uuid添加进协议栈中并获得uuid类型,然后再定义16bit uuid结构体,赋值uuid类型和uuid值,是服务的uuid就调用服务申明api,是特性uuid就调用特性申明api