When connecting to blevkit using the iPhone app, the on_receive function does not receive data

Hi 
I'm making a program to connect iPhone apps

 I'm using the ncs zephyr

This is the part, I made the cords

static struct bt_conn *my_connection;
tatic 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);
void on_cccd_changed(const struct bt_gatt_attr *attr, uint16_t value);
BT_GATT_SERVICE_DEFINE(my_service,
BT_GATT_PRIMARY_SERVICE(BT_UUID_MY_SERVICE),
BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERVICE_RX,
                   BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
                   BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
                   NULL, on_receive, NULL),
BT_GATT_CCC(on_cccd_changed,
        BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERVICE_TX,
                   BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
                   BT_GATT_PERM_READ,
                   NULL, NULL, NULL),
BT_GATT_CCC(on_cccd_changed,
        BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);
void on_cccd_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
    ARG_UNUSED(attr);
    printk("Error, CCCD has been set to an invalid value");
    switch(value)
    {
        case BT_GATT_CCC_NOTIFY:
            // Start sending stuff!
            if (my_connection) {
                on_receive(my_connection, attr, NULL, 0, 0, 0);
            }
            break;

        case BT_GATT_CCC_INDICATE:
            // Start sending stuff via indications
            if (my_connection) {
                on_receive(my_connection, attr, NULL, 0, 0, 0);
            }
            break;

        case 0:
            // Stop sending stuff
            break;
       
        default:
            printk("Error, CCCD has been set to an invalid value");    
    }
}
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)
{
    const uint8_t * p_buff = buf;
    return len;
}
static void connected(struct bt_conn *conn, uint8_t err) {
    char addr[BT_ADDR_LE_STR_LEN];
    // struct bt_conn_info info;
    my_connection = conn;
    BLE.conn_flag = true;
}
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 (reason %u)\n", reason);

    if (my_connection != conn) {
        return;
    }
}
static struct bt_conn_cb conn_callbacks = {
    .connected = connected,
    .disconnected = disconnected,
    .le_param_req       = NULL,
    .le_param_updated   = NULL
};
void main(void)
{
    err = bt_enable(NULL);
    if (err) {
        // printk("Bluetooth init failed (err %d)\n", err);
        return;
    bt_conn_cb_register(&conn_callbacks);
    }
I am connecting using iPhone app and connected function is debugged
However, the on_receive function receiving the data is not debugged
As far as I know, it goes to the on_receive function through the on_ccd_changed function, so please check if my code is correct!
Parents
  • Hi Min Hyuk, 
    Please describe how exactly you send data from the iPhone to your device ? 
    Do you do that by send a write command from the phone to the device ? 

    Please be aware that on_cccd_changed() is called only when the phone enable or disable CCCD notification / indication. It's not when the phone or the device sending data. 
    My suggestion is to follow our lessons in the Bluetooth Academy here:https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/lessons/lesson-4-bluetooth-le-data-exchange/

  • Thank you for answering

    void on_cccd_changed(const struct bt_gatt_attr *attr, uint16_t value) Please think that this is not here

    Currently, it has progressed from the app to the discovery service and discovery characteristic, and when sending data from the app, I am not receiving data from the board
    On the app
    peripheral.writeValue(data, for: self.char1, type: CBCharacteristicWriteType.withoutResponse)
    Using this, the app sent data to the board

    Why can't I get data from the on_receive?

  • HI Min Hyuk, 

    We would nee to check if the app actually sending anything to the device or not. 
    A sniffer trace would be able to confirm this. Make sure you select the advertiser when you capture sniffer trace so that the sniffer can follow the connection. 

    Please make sure you are writing to the RX characteristic. 

    My suggestion is to take a look at the peripheral_uart and test the example. If it works, you would need to check what's the different between the sample and your application. 

  • The APP sent the data to BT_UUID_MY_SERVICE_TXand wrote the on_receive function to BT_UUID_MY_SERVICE_TX
    But I have a new problem. When I update CHARACTERISTIC, I can't send data through my_service_send function
    When you use the nrf connect app

    BT_GATT_SERVICE_DEFINE(my_service,
    BT_GATT_PRIMARY_SERVICE(BT_UUID_MY_SERVICE),
    BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERVICE_RX,
                       BT_GATT_CHRC_NOTIFY |BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
                       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
                       NULL, NULL, NULL),
    BT_GATT_CCC(on_cccd_changed,
            BT_GATT_PERM_WRITE_LESC | BT_GATT_PERM_READ_LESC),
    BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERVICE_TX,
                       BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
                       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
                       NULL, on_receive, NULL),
    BT_GATT_CCC(on_cccd_changed,
            BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
    );
    Using the above code, we can even confirm sending and receiving data
    But you can't do it with the app I made
    So I debugged it, and as soon as I activated BT_GATT_CCC under BT_UUID_MY_SERVICE_RX, I checked that the data was entering the garbage value. When I deactivated it, the data went in well
    Do you happen to know why this is?

     

  • Hi Min Hyuk,

    I would suggest to copy exactly what inside peripheral_uart and try to compare it with your code to see what change make it doesn't work. 
    I don't fully understand what you meant by 

    Min Hyuk said:
    When I deactivated it, the data went in well

    What I don't understand is that why you want to have CCCD for a characteristic that doesn't  have notification property ? 


    Please take a look at the peripheral_uart service declaration. 

Reply
  • Hi Min Hyuk,

    I would suggest to copy exactly what inside peripheral_uart and try to compare it with your code to see what change make it doesn't work. 
    I don't fully understand what you meant by 

    Min Hyuk said:
    When I deactivated it, the data went in well

    What I don't understand is that why you want to have CCCD for a characteristic that doesn't  have notification property ? 


    Please take a look at the peripheral_uart service declaration. 

Children
No Data
Related