how to do long write or reliable write from Android to nrf?

I'm close but this is not working for me yet. I'm trying to send a few hundred bytes from android to my nrf device.  So I tried to implement long write where you set prepare and execute flags.  But in Android I could never get the flags to send no matter what approach I tried. And since I"m out of gas I thought I'd ask here.  I can send data to this characteristic just fine, but without the flags it just falls out.  Is there a way to make this work or do I need to rearchitect and do it manually?

On the android side I've tried reliablewrite approach, just sending with writecharacteristic, and many others.  Thanks for any advice.

This is my nordic side code:

static ssize_t write_long_characteristic(struct bt_conn *conn,
                                         const struct bt_gatt_attr *attr,
                                         const void *buf,
                                         uint16_t len,
                                         uint16_t offset,
                                         uint8_t flags) {
    // Automatically reset the buffer if this is the first Prepare Write of a new file
    if (offset == 0 && (flags & BT_GATT_WRITE_FLAG_PREPARE)) {
        reset_long_write_buffer();  // Reset the buffer for new file transfer
        printk("Reset Buffer");
    }

    // Check for Prepare Write phase
    if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
        printk("Prepare Write received: offset=%u, len=%u\n", offset, len);

        // Ensure we don't exceed the buffer size
        if (offset + len > sizeof(long_write_buffer)) {
            printk("Error: Buffer overflow\n");
            return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);  // Return an error code
        }

        // Copy the incoming data to the buffer at the specified offset
        memcpy(long_write_buffer + offset, buf, len);
        long_write_len = offset + len;  // Update the total length

        return len;  // Return the number of bytes written
    }

    // Check for Execute Write phase
    if (flags & BT_GATT_WRITE_FLAG_EXECUTE) {
        printk("Execute Write received, total length: %zu\n", long_write_len);

        // Process the buffered data
        //process_long_write_data(long_write_buffer, long_write_len);

        // Reset the buffer for the next long write operation
        long_write_len = 0;

        return len;
    }

    printk("We don't like what you're selling\n");
    return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);  // Handle unexpected cases
}

Related