Disconnection request from peripheral device.

Hi Everyone,

sdk : nrf Connect v1.7.0

ide : VS code

I was trying out central_hr sample on nrf52840 dk. I have simulated hrs server in nrf Connect mobile application. So basically our dk is Central and nrf Connect app is peripheral here.

Suppose I have disconnected in nrf Connect app,after central made a connection, I couldn't see any callback related to disconnection on central side.

This isn't the case with peripheral_hr sample in nrf52dk. 

My question is ...

1.How the Central device knows if the peripheral is still in connection?

2.Can we disconnect from peripheral side in a ble connection?

3.Suppose I have both functionalities central and peripheral running parallely in a nrf52 chip, how to handle connection and disconnection from two independent roles?

Parents
  • Please note that this timeout you set is a connection supervision timeout, and will be trigged after 4 seconds of inactivity in the connection. Are you sure that the application is inactive for 4 seconds, or else the connection timeout won't be trigged. 

    Since you're using a phone (and nRFConnect) as a peripheral you should be able to see logging information in nRFConnect where I believe the conn sup. timeout should be printed upon a connection. Can you confirm that it is set to 4 seconds?

    Best regards,

    Simon

Reply
  • Please note that this timeout you set is a connection supervision timeout, and will be trigged after 4 seconds of inactivity in the connection. Are you sure that the application is inactive for 4 seconds, or else the connection timeout won't be trigged. 

    Since you're using a phone (and nRFConnect) as a peripheral you should be able to see logging information in nRFConnect where I believe the conn sup. timeout should be printed upon a connection. Can you confirm that it is set to 4 seconds?

    Best regards,

    Simon

Children
  • Hi

    I can see that connection parameters updated: 4000ms in mobile app's log. 

    I tried with customer's ble sensor, pretty much the same central_hrs example, I've modified just some service uuids that's it.

    The sensor would go to sleep immediately after notifying some data.

    Now I got the disconnect callback.

    when I connected customer's sensor with nrfConnect App then I got...

    " Connection terminated by peer (status 19) " response on log.

    same code different results. I wish to know why I couldn't do it from mobile app. Some flag setting might be missed. Please help me to know where I went wrong.

    /* main.c - Application main entry point */
    
    /*
     * Copyright (c) 2015-2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/types.h>
    #include <stddef.h>
    #include <errno.h>
    #include <zephyr.h>
    #include <sys/printk.h>
    
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>
    #include <bluetooth/conn.h>
    #include <bluetooth/uuid.h>
    #include <bluetooth/gatt.h>
    #include <sys/byteorder.h>
    
    #define BT_UUID_TMU_SERVICE				BT_UUID_DECLARE_16(0xA100) /* TMU characteristic */
    #define BT_UUID_TMU_WR_CHRC				BT_UUID_DECLARE_16(0xA101)
    #define BT_UUID_TMU_NFY_CHRC			BT_UUID_DECLARE_16(0xA102)
    
    static void start_scan(void);
    
    static struct bt_conn *default_conn;
    
    static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0);
    static struct bt_gatt_discover_params discover_params;
    static struct bt_gatt_subscribe_params subscribe_params;
    
    
    
    static uint8_t notify_func(struct bt_conn *conn,
    			   struct bt_gatt_subscribe_params *params,
    			   const void *data, uint16_t length)
    {
    	if (!data) {
    		printk("[UNSUBSCRIBED]\n");
    		params->value_handle = 0U;
    		return BT_GATT_ITER_STOP;
    	}
    
    	printk("[NOTIFICATION] data %p length %u\n", data, length);
    
    	return BT_GATT_ITER_CONTINUE;
    }
    
    static uint8_t discover_func(struct bt_conn *conn,
    			     const struct bt_gatt_attr *attr,
    			     struct bt_gatt_discover_params *params)
    {
    	int err;
    
    	if (!attr) {
    		printk("Discover complete\n");
    		(void)memset(params, 0, sizeof(*params));
    		return BT_GATT_ITER_STOP;
    	}
    
    	printk("[ATTRIBUTE] handle %u\n", attr->handle);
    	#if 0
    	if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_TMU_SERVICE)) {
    		memcpy(&uuid, BT_UUID_TMU_WR_CHRC, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.start_handle = attr->handle + 1;
    		discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
    
    		err = bt_gatt_discover(conn, &discover_params);
    		if (err) {
    			printk("Discover failed (err %d)\n", err);
    		}
    	}else if(!bt_uuid_cmp(discover_params.uuid, BT_UUID_TMU_WR_CHRC)) {
    		memcpy(&uuid, BT_UUID_TMU_NFY_CHRC, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.start_handle = attr->handle + 1;
    		discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
    
    		err = bt_gatt_discover(conn, &discover_params);
    		if (err) {
    			printk("Discover failed (err %d)\n", err);
    		}
    	}else if(!bt_uuid_cmp(discover_params.uuid, BT_UUID_TMU_NFY_CHRC)) {
    		memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.start_handle = attr->handle + 2;
    		discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
    		subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
    
    		err = bt_gatt_discover(conn, &discover_params);
    		if (err) {
    			printk("Discover failed (err %d)\n", err);
    		}
    	}else {
    		subscribe_params.notify = notify_func;
    		subscribe_params.value = BT_GATT_CCC_NOTIFY;
    		subscribe_params.ccc_handle = attr->handle;
    
    		err = bt_gatt_subscribe(conn, &subscribe_params);
    		if (err && err != -EALREADY) {
    			printk("Subscribe failed (err %d)\n", err);
    		} else {
    			printk("[SUBSCRIBED]\n");
    		}
    		return BT_GATT_ITER_STOP;
    	}
    	#endif
    #if 1
    	if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_HRS)) {
    		memcpy(&uuid, BT_UUID_HRS_MEASUREMENT, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.start_handle = attr->handle + 1;
    		discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
    
    		err = bt_gatt_discover(conn, &discover_params);
    		if (err) {
    			printk("Discover failed (err %d)\n", err);
    		}
    	} else if (!bt_uuid_cmp(discover_params.uuid,
    				BT_UUID_HRS_MEASUREMENT)) {
    		memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.start_handle = attr->handle + 2;
    		discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
    		subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
    
    		err = bt_gatt_discover(conn, &discover_params);
    		if (err) {
    			printk("Discover failed (err %d)\n", err);
    		}
    	} else {
    		subscribe_params.notify = notify_func;
    		subscribe_params.value = BT_GATT_CCC_NOTIFY;
    		subscribe_params.ccc_handle = attr->handle;
    
    		err = bt_gatt_subscribe(conn, &subscribe_params);
    		if (err && err != -EALREADY) {
    			printk("Subscribe failed (err %d)\n", err);
    		} else {
    			printk("[SUBSCRIBED]\n");
    		}
    
    		return BT_GATT_ITER_STOP;
    	}
    #endif
    	return BT_GATT_ITER_STOP;
    }
    
    static bool eir_found(struct bt_data *data, void *user_data)
    {
    	 bt_addr_le_t *addr = user_data;
    	int i;
    uint8_t array[20] = {0};
    	printk("[AD]: %u data_len %u\n", data->type, data->data_len);
    
    	switch (data->type) {
    	case BT_DATA_UUID16_SOME:
    	case BT_DATA_UUID16_ALL:
    		if (data->data_len % sizeof(uint16_t) != 0U) {
    			printk("AD malformed\n");
    			return true;
    		}
    
    		for (i = 0; i < data->data_len; i += sizeof(uint16_t)) {
    			struct bt_le_conn_param *param;
    			struct bt_uuid *uuid;
    			uint16_t u16;
    			int err;
    
    			memcpy(&u16, &data->data[i], sizeof(u16));
    			uuid = BT_UUID_DECLARE_16(sys_le16_to_cpu(u16));
    			if (bt_uuid_cmp(uuid, BT_UUID_HRS)) {
    				continue;
    			}
    
    			err = bt_le_scan_stop();
    			if (err) {
    				printk("Stop LE scan failed (err %d)\n", err);
    				continue;
    			}
    
    			param = BT_LE_CONN_PARAM_DEFAULT;
    			err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    						param, &default_conn);
    			if (err) {
    				printk("Create conn failed (err %d)\n", err);
    				// bt_start_scan();
    			}
    
    			return false;
    		}
    	break;
    	case BT_DATA_NAME_COMPLETE:
    			
    	memcpy(array, data->data,data->data_len);
    	printk("Complete local Name: %s\r\n",array);
    	if(strcmp(array,"TMU") == 0)
    	{
    	int err = bt_le_scan_stop();
    	printk("BLE SCAN STOP\r\n");
    			if (err) {
    				printk("Stop LE scan failed (err %d)\n", err);
    				// continue;
    			}
    			struct bt_le_conn_param *param;
    			struct bt_uuid *uuid;
    			param = BT_LE_CONN_PARAM_DEFAULT;
    			err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    						param, &default_conn);
    			if (err) {
    				printk("Create conn failed (err %d)\n", err);
    				// bt_start_scan();
    			}
    			return false;
    	}
    		break;
    	}
    
    	return true;                   
    	#if 0
    	bt_addr_le_t *addr = user_data;
    	int i;
    	uint8_t array[20] = {0};
    
    	printk("[AD]: %u data_len %u\n", data->type, data->data_len);
    
    	switch (data->type) {
    	case BT_DATA_UUID16_SOME:
    	case BT_DATA_UUID16_ALL:
    		if (data->data_len % sizeof(uint16_t) != 0U) {
    			printk("AD malformed\n");
    			return true;
    		}
    
    		for (i = 0; i < data->data_len; i += sizeof(uint16_t)) {
    			struct bt_le_conn_param *param;
    			struct bt_uuid *uuid;
    			uint16_t u16;
    			int err;
    
    			memcpy(&u16, &data->data[i], sizeof(u16));
    			uuid = BT_UUID_DECLARE_16(sys_le16_to_cpu(u16));
    			if (bt_uuid_cmp(uuid, BT_UUID_HRS)) {
    				continue;
    			}
    
    			err = bt_le_scan_stop();
    			if (err) {
    				printk("Stop LE scan failed (err %d)\n", err);
    				continue;
    			}
    
    			param = BT_LE_CONN_PARAM_DEFAULT;
    			err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    						param, &default_conn);
    			if (err) {
    				printk("Create conn failed (err %d)\n", err);
    				start_scan();
    			}
    
    			return false;
    		}
    		break;
    	case BT_DATA_NAME_COMPLETE:
    			
    	memcpy(array, data->data,data->data_len);
    	printk("Complete local Name: %s\r\n",array);
    	if(strcmp(array,"Viswa's A52") == 0)
    	{
    	int err = bt_le_scan_stop();
    	printk("BLE SCAN STOP\r\n");
    			if (err) {
    				printk("Stop LE scan failed (err %d)\n", err);
    				// continue;
    			}
    			struct bt_le_conn_param *param;
    			struct bt_uuid *uuid;
    			param = BT_LE_CONN_PARAM_DEFAULT;
    			err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
    						param, &default_conn);
    			if (err) {
    				printk("Create conn failed (err %d)\n", err);
    				// bt_start_scan();
    
    			}
    	}
    		break;
    	}
    
    	return true;
    	#endif
    }
    
    static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
    			 struct net_buf_simple *ad)
    {
    	char dev[BT_ADDR_LE_STR_LEN];
    
    	bt_addr_le_to_str(addr, dev, sizeof(dev));
    	// printk("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
    	    //    dev, type, ad->len, rssi);
    
    	/* We're only interested in connectable events */
    	if (type == BT_GAP_ADV_TYPE_ADV_IND ||
    	    type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
    		bt_data_parse(ad, eir_found, (void *)addr);
    	}
    }
    
    static void start_scan(void)
    {
    	int err;
    
    	/* Use active scanning and disable duplicate filtering to handle any
    	 * devices that might update their advertising data at runtime. */
    	struct bt_le_scan_param scan_param = {
    		.type       = BT_LE_SCAN_TYPE_ACTIVE,
    		.options    = BT_LE_SCAN_OPT_FILTER_DUPLICATE,
    		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
    		.window     = BT_GAP_SCAN_FAST_WINDOW,
    	};
    
    	err = bt_le_scan_start(&scan_param, device_found);
    	if (err) {
    		printk("Scanning failed to start (err %d)\n", err);
    		return;
    	}
    
    	printk("Scanning successfully started\n");
    }
    
    static void connected(struct bt_conn *conn, uint8_t conn_err)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    	int err;
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	if (conn_err) {
    		printk("Failed to connect to %s (%u)\n", addr, conn_err);
    
    		bt_conn_unref(default_conn);
    		default_conn = NULL;
    
    		start_scan();
    		return;
    	}
    
    	printk("Connected: %s\n", addr);
    
    	if (conn == default_conn) {
    		memcpy(&uuid, BT_UUID_HRS, sizeof(uuid));
    		discover_params.uuid = &uuid.uuid;
    		discover_params.func = discover_func;
    		discover_params.start_handle = BT_ATT_FIRST_ATTTRIBUTE_HANDLE;
    		discover_params.end_handle = BT_ATT_LAST_ATTTRIBUTE_HANDLE;
    		discover_params.type = BT_GATT_DISCOVER_PRIMARY;
    
    		err = bt_gatt_discover(default_conn, &discover_params);
    		if (err) {
    			printk("Discover failed(err %d)\n", err);
    			return;
    		}
    	}
    }
    
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
    
    	if (default_conn != conn) {
    		return;
    	}
    
    	bt_conn_unref(default_conn);
    	default_conn = NULL;
    
    	start_scan();
    }
    
    static struct bt_conn_cb conn_callbacks = {
    	.connected = connected,
    	.disconnected = disconnected,
    };
    
    void main(void)
    {
    	int err;
    	err = bt_enable(NULL);
    
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	printk("Bluetooth initialized\n");
    
    	bt_conn_cb_register(&conn_callbacks);
    
    	start_scan();
    }
    

    Config File.

    CONFIG_BT=y
    CONFIG_BT_DEBUG_LOG=y
    CONFIG_BT_CENTRAL=y
    CONFIG_BT_SMP=n
    CONFIG_BT_GATT_CLIENT=y
    

Related