This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE large Write turns to gibberish

Greetings!

I have a central that's writing BLE messages to a peripheral. The characteristic on the peripheral is a 32-byte array, and my experiment is to write 32 bytes from the Central to the Peripheral without extending the Data Length, but only extending the ATT MTU. My understanding is that this is plausible, GATT will only need to send more messages within the same connection.

I am attempting to write:

Where bt_write_to_characteristic looks like:

and closely follows setup for the NCS NUS sample.

This is then seen to be split up into two writes on the peripheral (which makes sense since I didn't edit the DLE and it should remain 27 bytes long), and is split up into a message of 18 bytes and a message of 14 bytes (equals 32 bytes, so still makes sense). What then baffles me, is that the first message is entirely correct while the second message is seemingly gibberish:

This is output from the peripherals characteristic write callback:

Why would this be? Any idea what might be wrong? And why was the message split up into 18 and 14, and not 20 & 12, which would have made more sense regarding standard att payload is 20?

(I know i am actually overwriting the beginning of the characteristic value after receiving the BLE writes, but ignore that for now Slight smile also, I know it's peripheral functionality so ignore the fact its wrongly called 'beacon' Smile )

Current bluetooth related Kconfigs on both Central and Peripheral:

CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
CONFIG_BT_RX_BUF_LEN=255
CONFIG_BT_ATT_TX_MAX=10
CONFIG_BT_ATT_PREPARE_COUNT=2
CONFIG_BT_CONN_TX_MAX=10
CONFIG_BT_L2CAP_TX_BUF_COUNT=10
CONFIG_BT_L2CAP_TX_MTU=247
CONFIG_BT_L2CAP_RX_MTU=247
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
CONFIG_BT_CTLR_RX_BUFFERS=2
CONFIG_BT_CTLR_TX_BUFFERS=10
CONFIG_BT_CTLR_TX_BUFFER_SIZE=251

Looking forward to your response!

Best regards,

Jonas

Parents
  • I did a quick test on UART_Peripheral and UART_Central examples on NCS v 1.5.1.which your config changes set into proj.conf on both devices

    I see that the following output

    [00:00:07.010,070] <inf> peripheral_uart: Bluetooth initialized
    Starting Nordic UART service example
    [00:00:07.443,695] <inf> peripheral_uart: Connected E9:A8:9F:95:91:83 (random)
    [00:00:07.745,269] <inf> peripheral_uart: Security changed: E9:A8:9F:95:91:83 (random) level 2
    [00:00:09.044,982] <inf> bt_nus: Received data, Offet = 0X0 : BLE Msg:
    [00:00:09.045,013] <inf> bt_nus: 01  
    [00:00:09.045,013] <inf> bt_nus: 02  
    [00:00:09.045,013] <inf> bt_nus: 03  
    [00:00:09.045,043] <inf> bt_nus: 04  
    [00:00:09.045,043] <inf> bt_nus: 05  
    [00:00:09.045,043] <inf> bt_nus: 06  
    [00:00:09.045,043] <inf> bt_nus: 07  
    [00:00:09.045,043] <inf> bt_nus: 08  
    [00:00:09.045,074] <inf> bt_nus: 09  
    [00:00:09.045,104] <inf> bt_nus: 0a  
    [00:00:09.045,104] <inf> bt_nus: 0b  
    [00:00:09.045,104] <inf> bt_nus: 0c  
    [00:00:09.045,104] <inf> bt_nus: 0d  
    [00:00:09.045,135] <inf> bt_nus: 0e  
    [00:00:09.045,135] <inf> bt_nus: 0f  
    [00:00:09.045,135] <inf> bt_nus: 10  
    [00:00:09.045,135] <inf> bt_nus: 11  
    [00:00:09.045,135] <inf> bt_nus: 12  
    [00:00:09.045,166] <inf> bt_nus: 
    
    [00:00:09.045,288] <inf> peripheral_uart: Received data from: E9:A8:9F:95:91:83 (random)
    [00:00:09.045,349] <inf> bt_nus: Received data, Offet = 0X12 : BLE Msg:
    [00:00:09.045,379] <inf> bt_nus: 13  
    [00:00:09.045,379] <inf> bt_nus: 14  
    [00:00:09.045,379] <inf> bt_nus: 15  
    [00:00:09.045,379] <inf> bt_nus: 16  
    [00:00:09.045,410] <inf> bt_nus: 17  
    [00:00:09.045,410] <inf> bt_nus: 18  
    [00:00:09.045,410] <inf> bt_nus: 19  
    [00:00:09.045,410] <inf> bt_nus: 1a  
    [00:00:09.045,440] <inf> bt_nus: 1b  
    [00:00:09.045,440] <inf> bt_nus: 1c  
    [00:00:09.045,440] <inf> bt_nus: 1d  
    [00:00:09.045,440] <inf> bt_nus: 1e  
    [00:00:09.045,440] <inf> bt_nus: 1f  
    [00:00:09.045,471] <inf> bt_nus: 20  
    [00:00:09.045,471] <inf> bt_nus: 
    

    Changes on the peripheral side nus.c->on_receive

    static ssize_t on_receive(struct bt_conn *conn,
    			  const struct bt_gatt_attr *attr,
    			  const void *buf,
    			  uint16_t len,
    			  uint16_t offset,
    			  uint8_t flags)
    {
    	uint8_t *ptr = (uint8_t *)buf;
            LOG_INF("Received data, Offet = 0X%x : BLE Msg:", offset);
    
            for(uint8_t i = 0; i<len; i++)
            {
              LOG_INF("%02x  ", ptr[i]);
            }
            LOG_INF("\n");
    
    	if (nus_cb.received) {
    		nus_cb.received(conn, buf, len);
    }
    	return len;
    }

    Changes on the central side, main.c

    uint8_t write_test_data[32] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                                    0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
                                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
                                    0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                                  };
    
    void main(void)
    {
    	int err;
    
    	err = bt_conn_auth_cb_register(&conn_auth_callbacks);
    	if (err) {
    		LOG_ERR("Failed to register authorization callbacks.");
    		return;
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		LOG_ERR("Bluetooth init failed (err %d)", err);
    		return;
    	}
    	LOG_INF("Bluetooth initialized");
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    	bt_conn_cb_register(&conn_callbacks);
    
    	int (*module_init[])(void) = {uart_init, scan_init, nus_client_init};
    	for (size_t i = 0; i < ARRAY_SIZE(module_init); i++) {
    		err = (*module_init[i])();
    		if (err) {
    			return;
    		}
    	}
    
    	printk("Starting Bluetooth Central UART example\n");
    
    
    	err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
    	if (err) {
    		LOG_ERR("Scanning failed to start (err %d)", err);
    		return;
    	}
    
    	LOG_INF("Scanning successfully started");
    
    	for (;;) {
    		/* Wait indefinitely for data to be sent over Bluetooth */
    		//struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
    		//				     K_FOREVER);
    
    		err = bt_nus_client_send(&nus_client, write_test_data, sizeof(write_test_data));
    		if (err) {
    			LOG_WRN("Failed to send data over BLE connection"
    				"(err %d)", err);
    		}
    
    		err = k_sem_take(&nus_write_sem, NUS_WRITE_TIMEOUT);
    		if (err) {
    			LOG_WRN("NUS send timeout");
    		}
    	}
    }

  • Thanks for your reply Susheel.

    I get what you're saying about the offset, but I don't see you using it in your loop function, same as me. What i call buffer[i] you call ptr[i], and there is no relation to the offset other than the first print?

    Since you manage to get it running, I'll see tomorrow if I've done something else to not make mine run correctly.

    Best regards,

    Jonas

Reply Children
No Data
Related