Automatically Disconnected after write a message on BLE Peripheral UART

Hi

I am creating a simple firmware based on BLE Peripheral UART for nrf5340 (currently using DK). Before, I able to create notification to user app (I am currently using BLE Scanner on Android).

Now I am planning to create a response message or notification after app write to board. But after creating a flag variable (called : responding) and put it in code, suddenly it is disconnected. So below is the way my ble_write_thread works

void ble_write_thread(void)
{
	k_sem_take(&ble_init_ok, K_FOREVER);
	if (responding == 1) {
		struct uart_data_t *buf;
		char msg[4] = {'t', 'e', 's', 't'};
		buf->len = sizeof(msg);
		memcpy(buf->data, &msg, sizeof(msg));
		if (bt_nus_send(NULL, buf->data, buf->len)) {
			LOG_WRN("Failed to send data over BLE.");
		}
		responding = 0;
	}
}

So after app connected with board, I am planning to send a message then board should write back or notify app with 'test' message. But suddenly, it is disconnected. So here is the log information from RTT.

00> [00:00:00.259,216] <inf> peripheral_uart: Init fw
00> [00:00:00.259,277] <inf> peripheral_uart: Initializing BT
00> [00:00:00.265,319] <inf> fs_nvs: 2 Sectors of 4096 bytes
00> [00:00:00.265,319] <inf> fs_nvs: alloc wra: 0, fd0
00> [00:00:00.265,350] <inf> fs_nvs: data wra: 0, 1c
00> [00:00:00.289,306] <inf> bt_hci_core: No ID address. App must call settings_load()
00> [00:00:00.289,306] <inf> peripheral_uart: done
00> [00:00:00.289,337] <inf> peripheral_uart: Initializing Settings
00> [00:00:00.290,863] <inf> peripheral_uart: Done.
00> [00:00:00.290,893] <inf> peripheral_uart: Initializing BUS BLE
00> [00:00:00.291,870] <inf> peripheral_uart: Done.
00> [00:00:00.291,870] <inf> peripheral_uart: Advertizing BLEW
00> [00:00:00.295,654] <inf> peripheral_uart: Done.
00> [00:01:23.257,141] <inf> peripheral_uart: Connected 7E:1D:E0:12:BF:B1 (random)
00> [00:01:33.756,774] <inf> peripheral_uart: Disconnected: 7E:1D:E0:12:BF:B1 (random) (reason 8)
00> [00:01:51.067,352] <inf> peripheral_uart: Connected 7E:1D:E0:12:BF:B1 (random)
00> [00:01:59.289,093] <inf> peripheral_uart: Disconnected: 7E:1D:E0:12:BF:B1 (random) (reason 8)

I already upload BLE HCI Controller (from example) from manually using nrf Connect Desktop Programmer or upload it one by one, but still got same result. Please let me know if anybody have solution for this. Thank you.

Related