1.添加写和通知的特征
1.1调用方法
//read sensors state
err_code = nus_read_or_notice_char_add(p_nus, p_nus_init,4,0,&sensor_handle);
VERIFY_SUCCESS(err_code);
// //设备断开通知
err_code = nus_read_or_notice_char_add(p_nus, p_nus_init,7,1,&disconnect_handle_n);
VERIFY_SUCCESS(err_code);
1.3 read响应
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
BLE_RTT("rw1\r\n");
if(p_ble_evt->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_READ)
{
BLE_RTT("rw2\r\n");
/* reply with SUCCESS/ALLOWED */
ble_gatts_rw_authorize_reply_params_t rw_authorize_reply_params;
memset(&rw_authorize_reply_params, 0, sizeof(rw_authorize_reply_params));
rw_authorize_reply_params.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
rw_authorize_reply_params.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
rw_authorize_reply_params.params.read.update = 1;
if(p_ble_evt->evt.gatts_evt.params.authorize_request.request.read.uuid.uuid== SENSOR_STATE_UUID)
{
rw_authorize_reply_params.params.read.p_data=buf_sensor;
buf_sensor[0]++;
rw_authorize_reply_params.params.read.len=3;
BLE_RTT("read sensor:0x%x handel:0x%x\r\n",p_ble_evt->evt.gatts_evt.conn_handle,buf_sensor[0]);
err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle, &rw_authorize_reply_params);
APP_ERROR_CHECK(err_code);
}
}
break;
1.4 notice发送
uint32_t ble_notice_send(uint16_t value_handle,uint8_t n_enable, uint8_t * p_string, uint16_t * p_length)
{
ble_gatts_hvx_params_t hvx_params;
if ((connect_handle == BLE_CONN_HANDLE_INVALID) || (!n_enable))
{
return NRF_ERROR_INVALID_STATE;
}
if (*p_length > BLE_NUS_MAX_DATA_LEN)
{
return NRF_ERROR_INVALID_PARAM;
}
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = value_handle;//p_nus->tx_handles.value_handle;
hvx_params.p_data = p_string;
hvx_params.p_len = p_length;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
return sd_ble_gatts_hvx(connect_handle, &hvx_params);
}
1.5 notice响应开启notice
case BLE_GATTS_EVT_WRITE:
on_write(p_nus, p_ble_evt);
break;
else if ( (p_evt_write->handle == disconnect_handle_n.cccd_handle)
&& (p_evt_write->len == 2))
{
if (ble_srv_is_notification_enabled(p_evt_write->data))
{
disconnect_handle_notice = true;
BLE_RTT("notice on\r\n");
len=3;
ble_notice_send(disconnect_handle_n.value_handle,disconnect_handle_notice,"123",&len);
}
else
{
disconnect_handle_notice= false;
BLE_RTT("notice off\r\n");
}
}
1.2实现代码
static uint32_t nus_read_or_notice_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init,uint8_t uuid,uint8_t notice,ble_gatts_char_handles_t *pcharhandle)
{
uint32_t err_code;
ble_gatts_char_md_t char_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t ble_uuid;
ble_gatts_attr_md_t attr_md;
ble_gatts_attr_md_t cccd_md;
memset(&char_md, 0, sizeof(char_md));
memset(&attr_char_value, 0, sizeof(attr_char_value));
memset(&ble_uuid, 0, sizeof(ble_uuid));
memset(&attr_md, 0, sizeof(attr_md));
//read
if(notice==0)
{
char_md.char_props.read = 1;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;//BLE_GATTS_VLOC_STACK;// BLE_GATTS_VLOC_USER;//p_char_init->vloc;
attr_md.rd_auth = 1;//p_char_init->rd_auth;
attr_md.vlen = 1;//p_char_init->vlen;
}
//notice
else if(1==notice)
{
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;
char_md.char_props.notify = 1;
char_md.p_cccd_md = &cccd_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;
}
else //write
{
char_md.char_props.write = 1;
char_md.char_props.write_wo_resp = 1;
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.vlen = 1;
}
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = uuid;
attr_char_value.p_uuid = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = 1;
attr_char_value.max_len = 20;
err_code = sd_ble_gatts_characteristic_add(p_nus->service_handle,
&char_md,
&attr_char_value,
pcharhandle);
return err_code;
}