為了更好的實(shí)現(xiàn)DFU控制升級功能,為設(shè)備增加一個DFU Service,實(shí)現(xiàn)手機(jī)連接設(shè)備后修改此service中的Characteristic值,能夠直接重啟設(shè)備至BootLoader實(shí)現(xiàn)不需要手動重啟就能夠自動進(jìn)入Bootloader進(jìn)行DFU的功能.
Nordic自身的DFU Service增加非常簡單:
正式release時:
// Initialize the async SVCI interface to bootloader.
err_code = ble_dfu_buttonless_async_svci_init();
APP_ERROR_CHECK(err_code);
dfus_init.evt_handler = ble_dfu_evt_handler;
//Initialize the duf
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
如果設(shè)備當(dāng)前沒有bootloader,只有softdevice和application,那么ble_dfu_buttonless_async_svci_init
會返回NRF_ERROR_NO_MEM
錯誤,最開始開發(fā)時可能并不會同時吧bootloader一并燒錄進(jìn)去,所以在調(diào)試時增加了一個判斷返回的錯誤是否是NRF_ERROR_NO_MEM
的判斷.
此處最好再增加#ifdef來判斷實(shí)現(xiàn)release時編譯使用上面那段正式的代碼防止出錯
// Initialize the async SVCI interface to bootloader.
err_code = ble_dfu_buttonless_async_svci_init();
if(NRF_ERROR_NO_MEM!=err_code) {
//APP_ERROR_CHECK(err_code);
dfus_init.evt_handler = ble_dfu_evt_handler;
//Initialize the duf
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
}
補(bǔ)上需要的頭文件及sdk_config.h配置:
時間太久,可能有遺漏
頭文件
#include "ble_dfu.h"
sdk_config.h:
使能 nrf_DFU
/ble_dfu
/BLE_DFU_ENABLED
增加這些后,設(shè)備的ble service中就會多出這個