1、添加C文件:
nRF5_SDK_16.0.0_98a08e2\components\ble\ble_services\ble_dfu\ble_dfu.c
nRF5_SDK_16.0.0_98a08e2\components\ble\ble_services\ble_dfu\ble_dfu_bonded.c
nRF5_SDK_16.0.0_98a08e2\components\ble\ble_services\ble_dfu\ble_dfu_unbonded.c
nRF5_SDK_16.0.0_98a08e2\components\libraries\bootloader\dfu\nrf_dfu_svci.c
2、添加头文件路径
…\Libraries\components\libraries\svc
…\Libraries\components\libraries\bootloader
…\Libraries\components\libraries\bootloader\dfu
…\Libraries\components\libraries\bootloader\ble_dfu
…\Libraries\components\ble\ble_services\ble_dfu
…\Libraries\components\softdevice\mbr\nrf52832\headers
3、添加宏
1)BLE_SETTINGS_ACCESS_ONLY
2)NRF_DFU_SVCI_ENABLED
3)NRF_DFU_TRANSPORT_BLE=1
4.修改sdk_config.h文件
1)使能DFU模块
#ifndef BLE_DFU_ENABLED
#define BLE_DFU_ENABLED 1
#endif
2)修改使用的自定义UUID数量,每添加一个都要修改工程中RAM的开始地址和使用大小,1个UUID占用的RAM大小为0x10 根据实际使用的自定义UUID个数修改
#ifndef NRF_SDH_BLE_VS_UUID_COUNT
#define NRF_SDH_BLE_VS_UUID_COUNT 2
#endif
5、修改主程序代码
1)包含头文件
#include "nrf_power.h"
#include "nrf_bootloader_info.h"
#include "nrf_dfu_ble.h"
#include "ble_dfu.h"
#include "nrf_dfu_ble_svci_bond_sharing.h"
#include "nrf_svci_async_function.h"
#include "nrf_svci_async_handler.h"
2)添加DFU初始化代码并将其加入服务初始化函数
//获取广播参数
static void advertising_config_get(ble_adv_modes_config_t * p_config)
{
memset(p_config, 0, sizeof(ble_adv_modes_config_t));
p_config->ble_adv_fast_enabled = true;
p_config->ble_adv_fast_interval = APP_ADV_INTERVAL;
p_config->ble_adv_fast_timeout = APP_ADV_DURATION;
}
//断开连接
static void disconnect(uint16_t conn_handle, void * p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_WARNING("Failed to disconnect connection. Connection handle: %d Error: %d", conn_handle, err_code);
}
else
{
NRF_LOG_DEBUG("Disconnected connection handle %d", conn_handle);
}
//初始化DFU服务
static void dfu_s_init(void)
{
uint32_t err_code;
ble_dfu_buttonless_init_t dfus_init = {
0};
memset(&dfus_init, 0, sizeof(dfus_init));
dfus_init.evt_handler = ble_dfu_evt_handler; //注册DFU回调函数
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
}
//将DFU服务添加进services_init()函数
static void services_init(void)
{
dfu_s_init();
}
//DFU回调函数
static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event)
{
switch (event)