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

How write in custom characteristic in NRF_52 ?

I created a BLE Peripheral to read the light intensity, the purpose is connected to the iPhone device to read the update value in the BLE_NRF_52 device.

For the calibration of the sensor I added a Characteristic  “XXX” to the Service, like preset is set to 0.

For the moment the Peripheral works in simulation, it connects to iPhone correctly and performs data update correctly.

The problem arises when I try to write in the Characterist “XXX” to set a new value, I receive the following error in the console iPhone device:

Error discovering services Error: Optional(Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.})

How can I find a solution?

can you help me?

in iPhone implemented this code for write in characteristic:

func writeCharacteristic(_val val: Int8, inCharacteristic:CBCharacteristic){

        var val = val

        let ns = NSData(bytes: &val, length: MemoryLayout<Int8>.size)

        blePeripheral!.writeValue(ns as Data, for: inCharacteristic, type: CBCharacteristicWriteType.withResponse)

    }

Code for add characterist XXX in ble_sli.c :

static uint32_t light_intensity_point_char_add(ble_cbs_t            * p_cbs,

                                                const ble_cbs_init_t * p_cbs_init)

{

    ble_gatts_char_md_t char_md;

    ble_gatts_attr_t    attr_char_value;

    ble_uuid_t          ble_uuid;

    ble_gatts_attr_md_t attr_md;

    memset(&char_md, 0, sizeof(char_md));

    char_md.char_props.read  = 1;

    char_md.char_props.write_wo_resp  = 0;

    char_md.p_char_user_desc = NULL;

    char_md.p_char_pf        = NULL;

    char_md.p_user_desc_md   = NULL;

    char_md.p_cccd_md        = NULL;

    char_md.p_sccd_md        = NULL;

     BLE_UUID_BLE_ASSIGN(ble_uuid,  XXX);

    memset(&attr_md, 0, sizeof(attr_md));

    attr_md.read_perm  = p_cbs_init->cbs_light_point_attr_md.read_perm;

    attr_md.write_perm = p_cbs_init->cbs_light_point_attr_md.write_perm;

    attr_md.vloc       = BLE_GATTS_VLOC_STACK;

    attr_md.rd_auth    = 0;

    attr_md.wr_auth    = 0;

    attr_md.vlen       = 0;

    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &ble_uuid;

    attr_char_value.p_attr_md = &attr_md;

    attr_char_value.init_len  = sizeof (uint8_t);

    attr_char_value.init_offs = 0;

    attr_char_value.max_len   = sizeof (uint8_t);

    attr_char_value.p_value   = p_cbs_init->light_point_v;

    return sd_ble_gatts_characteristic_add(p_cbs->service_handle,

                                           &char_md,

                                           &attr_char_value,

                                           &p_cbs->point_handles);

}

uint32_t ble_cbs_init(ble_cbs_t * p_cbs, const ble_cbs_init_t * p_cbs_init)

{

 ………….

if (p_cbs_init->light_point_v != NULL)

    {

       // Add Brake Pressure  Point characteristic

        err_code = light_intensity_point_char_add(p_cbs, p_cbs_init);

        if (err_code != NRF_SUCCESS)

        {

           return err_code;

       } 

}

void ble_cbs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)

{

    ble_cbs_t * p_cbs = (ble_cbs_t *) p_context;

    switch (p_ble_evt->header.evt_id)

    {

        case BLE_GAP_EVT_CONNECTED:

            on_connect(p_cbs, p_ble_evt);

            break;

        case BLE_GAP_EVT_DISCONNECTED:

            on_disconnect(p_cbs, p_ble_evt);

            break;

        case BLE_GATTS_EVT_WRITE:

            on_write(p_cbs, p_ble_evt);

            break;

        // Add Frank 13-06-2018

        case BLE_GATTS_EVT_HVC:

            on_hvc(p_cbs, p_ble_evt);

            break;

        default:

            // No implementation needed.

            break;

    }

}

/**@brief Function for handling the Write event */

static void on_write(ble_cbs_t * p_cbs, ble_evt_t const * p_ble_evt)

{

    ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

    if (p_evt_write->handle == p_cbs->point_handles.cccd_handle)

    {

        on_bpm_cccd_write(p_cbs, p_evt_write);

    }

}

In main.c 

static void services_init(void)

{

    ret_code_t         err_code;

    ble_cbs_init_t     cbs_init;

    ble_bas_init_t     bas_init;

    ble_dis_init_t     dis_init;

    ble_dis_sys_id_t   sys_id;

    nrf_ble_qwr_init_t qwr_init = {0};

    uint8_t            light_sensor_location;

    uint8_t            light_point_init;

    // Initialize Queued Write Module.

    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);

    APP_ERROR_CHECK(err_code);

    // Initialize Light Service.

    light_sensor_location = BLE_CBS_LIGHT_SENSOR_LOCATION_A;

    light_point_init =  LIGHT_POINT_START_VALUE;

    memset(&cbs_init, 0, sizeof(cbs_init));

    cbs_init.evt_handler                 = NULL;

    cbs_init.is_sensor_contact_supported = true;

    cbs_init.p_light_sensor_location     = &light_sensor_location;

    cbs_init.light_point_v            = &light_point_init;

    // Here the sec level for the Service can be changed/increased.

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cbs_init.bps_meas_attr_md.cccd_write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&cbs_init.bps_meas_attr_md.read_perm);

    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&cbs_init.bps_meas_attr_md.write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cbs_init.cts_light_type_attr_md.read_perm);

    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&cbs_init.cts_light_type_attr_md.write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cbs_init.cbs_light_point_attr_md.read_perm);

     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cbs_init.cbs_light_point_attr_md.write_perm);

    err_code = ble_cbs_init(&m_cbs, &cbs_init);

    APP_ERROR_CHECK(err_code)

Parents Reply Children
No Data
Related