![]() |
nRF5 SDK
v15.2.0
|
Choose documentation: | nRF5 SDK | S112 SoftDevice API | S132 SoftDevice API | S140 SoftDevice API | S212 SoftDevice API |
Important: Before you run this example, make sure to program the SoftDevice.
The Nordic UART Service (NUS) Application is an example that emulates a serial port over BLE. In the example, Nordic Semiconductor's development board serves as a peer to the phone application "nRF UART", which is available for iOS from App Store and for Android from Google Play. In addition, the example demonstrates how to use a proprietary (vendor-specific) service and characteristics with the SoftDevice.
The application includes one service: the Nordic UART Service. The 128-bit vendor-specific UUID of the Nordic UART Service is 6E400001-B5A3-F393-E0A9-E50E24DCCA9E (16-bit offset: 0x0001).
This service exposes two characteristics: one for transmitting and one for receiving (as seen from the peer).
The following sections describe some important parts of the example code.
The initialization of the proprietary service and its characteristics is done in ble_nus.c
.
The Nordic UART Service is added to the SoftDevice as follows:
The TX characteristic is added to the SoftDevice as shown in the code below. The read and write permissions of the characteristic and its CCCD are set as open, which means that there are no security restrictions on this characteristic. The type of the UUID (add_char_params.uuid_type
) is the value that was returned in the call to sd_ble_uuid_vs_add(). The RX characteristic is added in a similar way.
The initialization of the application and the handling of data sent and received through BLE and UART are done in main.c
.
The UART initialization is done as shown in the code below. The code segment uses the UART module that is provided in the SDK to perform the UART configuration. Note that app_uart_comm_params_t configures the application to use hardware flow control. Therefore, RTS_PIN_NUMBER and CTS_PIN_NUMBER are used as Ready-to-Send and Clear-to-Send pins, respectively.
When initializing the service in the services_init() function, the application passes the function nus_data_handler, which is used for handling the received data. When the Nordic UART Service indicates that data has been received over BLE from the peer, the same data is relayed to the UART. The nus_data_handler function is implemented as follows:
Data that is received from the UART undergoes certain checks before it is relayed to the BLE peer using the Nordic UART Service. The code shown below is part of the app_uart event handler, which is called every time a character is received over the UART. Received characters are buffered into a string until a newline character is received or the size of the string matches the current MTU size. When one of these two conditions is met, the string is sent over BLE using the ble_nus_data_send function.
The implementation of the example requires the newline character to be '\n'. Some terminals use different characters to trigger a newline, for example '\r'. Therefore, you might need to configure your terminal to interpret only '\n' as a newline.
You can find the source code and the project file of the example in the following folder: <InstallFolder>\examples\ble_peripheral\ble_app_uart
LED assignments:
Button assignments: BSP BLE Button Assignments
Test the UART Application with the nRF UART app, which is available for iOS (App Store) and Android (Google Play).
You can also test the application with nRF Connect by performing the following steps: