当前位置:网站首页>nRF52832自定義服務與特性
nRF52832自定義服務與特性
2022-06-12 05:45: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);
边栏推荐
- 16. 最接近的三数之和
- Rtmp/rtsp/hls public network real available test address
- UBI details and JFFS2 square FS UBIFS
- Codis 3. X expansion and contraction
- Chapter 8 - structure
- About architecture (in no particular order)
- March 4, 2021
- XML参数架构,同一MTK SW版本兼容两套不同的音频参数
- Individual application for ov type SSL certificate
- GRP development: four communication modes of GRP
猜你喜欢

Identification of campus green plants based on tensorflow

WebRTC AEC 流程解析

16. sum of the nearest three numbers

Why should state-owned enterprises go public
![[gin] gin framework for golang web development](/img/15/68c4fd217555f940b3cd3d10fcd54f.jpg)
[gin] gin framework for golang web development

Towards End-to-End Lane Detection: an Instance SegmentationApproach

How Wireshark decrypts WiFi data packets

Special materials | household appliances, white electricity, kitchen electricity

深入理解异步编程

yolov5
随机推荐
RTMP streaming +rtmp playback low delay solution in unity environment
Towards End-to-End Lane Detection: an Instance SegmentationApproach
Beginning is an excellent emlog theme v3.1, which supports emlog Pro
Performance test - GTI application service performance monitoring platform
Laravel8 when search
Win10 desktop unlimited refresh
Go interface implementation principle [advanced level]
16. 最接近的三數之和
Webrtc AEC process analysis
How long is the company's registered capital subscribed
Lesson 5: data warehouse construction (III)
38. appearance series
Redis transaction
yolov5
Date ()
Simple introduction to key Wizard
Why should state-owned enterprises go public
Details of FPGA syntax
[fastapi] use pycharm to configure and use environment variables for fastapi projects
C语言-数组的定义方式