This is a follow-up to my previous blog post; adding custom commands to a BLE example.
I hope it was interesting to see an example of adding the vendor specific Nordic UART service (NUS) into another project.
For completeness, I wanted to share steps needed to keep all the functionality of the BLE UART example (ble_app_uart), instead of just using part of the service functionality like we did in the previous blog post.
This leads to a few gotchas that we will explore now. As with the previous blog post, I uploaded the files to GitHub. Looking at the diff of the original ble_app_hts and the merged result ble_app_hts_uart is probably just as educational as reading the points below.
The following steps assume you have completed the steps in the previous blog post. We can continue with the same ble_app_hts main.c and sdk_config.h file.
1. Restore nus_data_handler() to how it looks in ble_app_uart.
Posted below for convenience.
2. Changes to sdk_config.h to add UART.
UART uses app_uart and app_fifo but these are actually missing in the sdk_config used by ble_app_hts. we have to add them.
ble_app_hts was using the UART to print logs from the app.
ble_app_uart, on the other hand, was using RTT to print these logs since the UART was (fittingly enough) being used to print UART data coming over BLE from NUS.
We want that same behavior as the ble_app_uart example. Change NRF_LOG_BACKEND_UART_ENABLED form 1 to 0, and NRF_LOG_BACKEND_RTT_ENABLED from 0 to 1.
3. Changes to project and main.c to add uart
Include app_uart module and the nrf_uart driver.
Add app_uart_fifo and app_fifo source file to you project and the header file to your include path.
(app_uart.h and app_uart_fifo.c are located in <SDK>/components/libraries/uart)
(app_fifo.h and app_fifo.h are located in <SDK>/components/libraries/fifo)
Copy the entire uart_init() and uart_event_handle() from ble_app_uart. Posted below for convenience.
Add uart_init(); to your main function.
Add buffer defines for UART close to the top of your main file.
4. (optional) Enable long MTU
uart_event_handle() uses m_ble_nus_max_data_len. We have to add it near the top of main next to the other global defines.
Replace gatt_init() of ble_app_hts with the gatt_init() found in ble_app_uart.
In sdk_config.h, change the NRF_SDH_BLE_GATT_MAX_MTU_SIZE from 23 to 247.
5. Advertising data
By now the application should be able to run. You can connect to it using nRF Toolbox on iOS or Android or nRF Connect. Notice that logs are printed on the RTT and whatever you send with the UART app in nRF Toolbox is printed over UART on our pc.
The app connects to nRF toolbox without problems, but if you observe the nRF Connect app, you notice that it does not advertise with the NUS UUID. Let's add it.
If we try to add it to the m_adv_uuids, we will get the error NRF_ERROR_DATA_SIZE returned from advertising_init() because we don't have enough bytes left in our 29 bytes of advertising data to fit the 16 bytes of NUS UUID.
But we can add it to the scan response instead since that lets us send 29 additional payload bytes to any active scanner.
We do have space to change the advertising name:
Add the UUID define from ble_app_uart, and the static list of scan response UUIDs.
in advertising_init() add
Thats it! You should be able to test your application according to the steps described for ble_app_hts and ble_app_uart. Especially the the uart should be interesting to test since it should now work the same way as in ble_app_uart, with respect to input and output of UART data over terminal (unlike my last blog post).