当前位置:网站首页>nRF52832自定义服务与特性
nRF52832自定义服务与特性
2022-06-12 05:44:00 【m0_7788】
实验目的:自定义Service私有服务并传输温湿度传感器数据。本实验基于SDK17.02中ble_app_uart示例代码进行更改。
一、背景
1.1GAP初始化:
主要完成三个工作:
a.GAP初始化安全模式配置,安全模式指连接时是否需要身份验证。
b.蓝牙设备名称设置,即设置蓝牙广播的名字,注意名称长度有限制,最好不要超过十八个字节
c.连接参数设置,连接参数设置,主要是设置:最小连接间隔、最大连接间隔、从机设备潜伏周期、连接超时时间。这些值代表了外围设备针对连接的期望参数。
1.2GATT初始化
GATT称为通用属性规范(Generic Attribute profile,GATT),GATT层是传输真正数据所在的 层。包括一个数据传输和存储框架以及其基本操作。其大部分自定义函数在.C写的,在主函数中只需要初始化和配置函数间的关系。
GATT使用了 ATT(Attribute Protocol)协议,ATT 协议把 Service,Characteristic 对应的数据保存在一个查找表中,查找表使用 16bit ID 作为每一项的索引。GATT定义的多层数据结构简要概括起来就是 服务(Service) 可以包含多个 特征(Characteristic),每个特征包含 属性(Properties) 和 值(Value),还可以包含多个 描述(Descriptor)。
二、ble_lbs.c函数
battery_level为模拟电量的value
2.1主机客户端事件处理
ble_lbs.h观察者模式NRF_SDH_BLE_OBSERVER—>>>ble_lbs_on_ble_evt—>>>lbs_c_evt_handler()
case BLE_GATTS_EVT_WRITE: //LED特征被写入数据时,产生了GATT写事件,就会调用on_write函数。
lbs_c_evt_handler(p_lbs, p_ble_evt);
ble_lbs_on_ble_evt—>>>lbs_c_evt_handler()
p_lbs->led_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_lbs, p_evt_write->data[0]);
ble_srv_is_notification_enabled(p_evt_write->data)
2.2主机客户端初始化ble_lbs_init()
// Initialize service structure
p_lbs->evt_handler = p_lbs_init->evt_handler;
p_lbs->is_notification_supported = p_lbs_init->support_notification;
p_lbs->battery_level_last = INVALID_BATTERY_LEVEL;
// Add service
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BATTERY_SERVICE);
// Add battery level characteristic
err_code = battery_level_char_add(p_lbs, p_lbs_init);
特征添加函数battery_level_char_add()
memset(&cccd_md, 0, sizeof(cccd_md));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
memset(&char_md, 0, sizeof(char_md));
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BATTERY_LEVEL_CHAR);
memset(&attr_md, 0, sizeof(attr_md));
initial_battery_level = p_bas_init->initial_batt_level;
memset(&attr_char_value, 0, sizeof(attr_char_value));
err_code = sd_ble_gatts_characteristic_add(p_bas->service_handle, &char_md,
&attr_char_value,
&p_bas->battery_level_handles);
2.3 主机客户端温度数据处理函数
main.c定时器调用battery_level_update():
memset(&gatts_value, 0, sizeof(gatts_value));
err_code = sd_ble_gatts_value_set(BLE_CONN_HANDLE_INVALID,
p_bas->battery_level_handles.value_handle,
&gatts_value);
err_code = battery_notification_send(&hvx_params, conn_handles.conn_handles[i]);
battery_level_update()—>>>battery_notification_send()
err_code = sd_ble_gatts_hvx(conn_handle, p_hvx_params);
三.main.c函数
3.1.宏定义
APP_TIMER_DEF(m_battery_timer_id);
BLE_LBS_mpu6050_DEF(m_mpu6050);//观察者模式
static ble_lbs_t m_mpu6050;
ble_bas_battery_level_update(&m_mpu6050, TIME_TEMP, BLE_CONN_HANDLE_ALL);
3.2.services_init
主要是初始化ble_lbs.c里的ble_lbs_init
ble_lbs_init_t bas_init_obj;
memset(&bas_init_obj, 0, sizeof(bas_init_obj));
err_code = ble_lbs_init(&m_mpu6050, &bas_init_obj);
3.3.定时器
回调函数measure_timer_handler
TIME_TEMP为模拟值
ble_bas_battery_level_update(&m_mpu6050, TIME_TEMP, BLE_CONN_HANDLE_ALL);
边栏推荐
- 38. appearance series
- C language - how to define arrays
- flex/fixed上中下(移动端)
- Thesis reading_ Figure neural network gin
- AddUser add user and mount hard disk
- Halcon 3D 1 Reading 3D data
- Performance test - Analysis of performance test results
- 38. 外观数列
- Beginning is an excellent emlog theme v3.1, which supports emlog Pro
- The relation between virtual function and pure virtual function
猜你喜欢

Is the individual industrial and commercial door a legal person enterprise

16. 最接近的三數之和

The application could not be installed: INSTALL_FAILED_TEST_ONLY

Introduction to redis cluster

WiFi protocol and ieee905 protocol learning details
![[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation](/img/12/27531fc791b3f49306385831309c5e.png)
[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation

16. Somme des trois plus proches

Introduction to sringmvc

16. sum of the nearest three numbers

Redis transaction
随机推荐
Why can't NAND flash be used as RAM while nor flash can
深入理解异步编程
按键精灵的简单入门
Memory model, reference and function supplement of program
Available RTMP and RTSP test addresses of the public network (updated in March, 2021)
Research Report on market supply and demand and strategy of China's digital camera lens industry
Individual application for ov type SSL certificate
Test work summary - performance test related issues
Stack and queue classic interview questions
网络加速谁更猛?CDN领域再现新王者
Reverse linked list
Introduction to redis cluster
Necessary for Test Engineer -- package capturing tool fiddler
16. sum of the nearest three numbers
Laravel8 authentication login
UBI details and JFFS2 square FS UBIFS
Halcon 3D 深度图转换为3D图像
Identification of campus green plants based on tensorflow
38. 外观数列
Vivado HLS introductory notes