Send data from central device to peripheral device.

Hi,

I am attempting to modify the central_hr and peripheral_hr code to send data from the central device to the peripheral and have the peripheral device print out what it has received. So far I have modified the heart-rate characteristic so that it will receive an unsigned integer and when I connect to it via the the nRFConnect app I can send it an integer and it will print it out just fine. However, when I attempt to modify the central_hr to send an unsigned integer via bt_gatt_write() or bt_gatt_write_without_response() it doesn't seem to work. The two devices are connecting fine and the central is receiving the mock heart-rate data but as far as I can tell the peripheral is not receiving anything. Am I missing a step? Do I have to do additional setup to get the central to write data to the peripheral characteristic? I am a little stalled out here and I am not sure I understand the central code very well so any help would be appreciated. The modifications to the peripheral work as intended with the nrfConnect app so I assume the issue is in the central but I don't know what that could be.

I will show some of the relevant code below and I am using a 52840 dev kit as the peripheral and a 5340 dev kit as the central.

Where I am calling bt_gatt_write_without_response() (I have hrs.h included in the central code)

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 %u length %u\n",((uint8_t*)data)[1], length);
    //struct bt_gatt_write_params write_params;
    //bt_hrs_notify(99U);
    static uint8_t datadata = 10;
    bt_gatt_write_without_response(conn, BT_UUID_HRS_MEASUREMENT_VAL, datadata, sizeof(datadata), false);
    printk("Data %u sent\n", datadata);
    return BT_GATT_ITER_CONTINUE;
}
Here is where I edited the characteristic in hrs.c
BT_GATT_SERVICE_DEFINE(hrs_svc,
    BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS),
    BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY|BT_GATT_CHRC_WRITE_WITHOUT_RESP,
                   BT_GATT_PERM_WRITE, NULL, print_data, NULL),
    BT_GATT_CCC(hrmc_ccc_cfg_changed,
            HRS_GATT_PERM_DEFAULT),
    BT_GATT_CHARACTERISTIC(BT_UUID_HRS_BODY_SENSOR, BT_GATT_CHRC_READ,
                   HRS_GATT_PERM_DEFAULT & GATT_PERM_READ_MASK,
                   read_blsc, NULL, NULL),
    BT_GATT_CHARACTERISTIC(BT_UUID_HRS_CONTROL_POINT, BT_GATT_CHRC_WRITE,
                   HRS_GATT_PERM_DEFAULT & GATT_PERM_WRITE_MASK,
                   NULL, NULL, NULL),
);
Here is print_data function
static ssize_t print_data(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf,
             uint16_t len, uint16_t offset, uint8_t flags)
{
    LOG_DBG("Attribute write, handle: %u, conn: %p", attr->handle, (void *)conn);

    if (len != 1U) {
        //LOG_DBG("Write led: Incorrect data length");
        return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
    }

    if (offset != 0) {
        //LOG_DBG("Write led: Incorrect data offset");
        return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
    }

    printk("DATA IS: %d", *((uint8_t *)buf));

    return len;
}
Parents Reply Children
Related