SDK 11.0 或 低版本SDK:
添加服务时注意 services_init 及 ble_evt_dispatch。services_init:举例电量例程,ble_evt_dispatch:举例nus服务。且在添加nus服务时要将nus更改备份,以防需要添加多个服务。注:高版本不需要 ble_evt_dispatch,因为高版本SDK在定义服务函数句柄(例:BLE_NUS_DEF)已做了相同的事
SDK15.2:
VERIFY_PARAM_NOT_NULL(p_nus);
VERIFY_PARAM_NOT_NULL(p_nus_init);
// Initialize the service structure.
p_nus->data_handler = p_nus_init->data_handler;
首先更改UUID:
16bit:
ble_nus_init -> :
ble_uuid.type = BLE_UUID_TYPE_BLE;
ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
或直接设定为指定值(不附加蓝牙协议标准的基准UUID)
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_NUS_SERVICE);
128bit:
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
至于需不需要添加基准UUID还需要确定
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
VERIFY_SUCCESS(err_code);
// Add the service.
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid,
&p_nus->service_handle);
/**@snippet [Adding proprietary Service to the SoftDevice] */
VERIFY_SUCCESS(err_code);
再者添加特征:
ble_nus_init -> char_add1:
ble_add_char_params_t add_char_params;
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_CHARACTERISTIC1;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = BLE_NUS_MAX_RX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1;
add_char_params.char_props.write = 1;
add_char_params.char_props.write_wo_resp = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
return characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->handles1);
其次再对应处理函数中添加对应通道的处理句柄:
on_connect、on_write、on_hvx_tx_complete
最后建立对应通道的nus_data_send函数:
hvx_params.handle = p_nus->handles1.value_handle;
其他版本都大同小异,参照正久-wiegand项目(SDK15.2)及官方例程进行修改
若在串口服务添加两个通道后再添加服务报错(NRF_ERROR_NO_MEM):则需将 NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 改大
http://www.qfv8.com/forum.php?mod=viewthread&tid=136203&page=1#pid144140
https://devzone.nordicsemi.com/f/nordic-q-a/44861/ble_ots_init-returns-nrf_error_no_mem/176246#176246
https://devzone.nordicsemi.com/f/nordic-q-a/30024/nrf_sdh_ble_gatts_attr_tab_size
如若在自建的服务中想使用自定义的UUID,若调用sd_ble_uuid_vs_add时出错则将此NRF_SDH_BLE_VS_UUID_COUNT数值更改,暂且将其理解为设置自定义UUID的个数
https://devzone.nordicsemi.com/f/nordic-q-a/29017/device-reset-when-calling-sd_ble_uuid_vs_add-second-time
另连接间隔与通讯数量及时间有关
UUID:
广播扫描数据UUID:
16bit:
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_BLE
static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
scanrsp.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
scanrsp.uuids_complete.p_uuids = m_adv_uuids;
128bit:
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
scanrsp.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
scanrsp.uuids_complete.p_uuids = m_adv_uuids;
服务及特征通道UUID:
16bit:
ble_uuid.type = BLE_UUID_TYPE_BLE;
ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
或直接设定为指定值(不附加蓝牙协议标准的基准UUID)
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_NUS_SERVICE);
128bit:
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
至于需不需要添加基准UUID还需要确定
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
VERIFY_SUCCESS(err_code);
另,若通道要使用不同于服务的特定基准UUID(base uuid),则需更改NRF_SDH_BLE_VS_UUID_COUNT 且 在需设置UUID处调用
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
VERIFY_SUCCESS(err_code);