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

i initialize a characteristic write-only, but that can not write,,help me!

image description

  static uint32_t jawbone_char_TriggerEventHistory_add(ble_jawbone_t * p_jawbone, const ble_jawbone_init_t * p_jawbone_init)
    {
        uint32_t            err_code;
        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;
    		ble_srv_cccd_security_mode_t  char_attr_security_mode_md;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_security_mode_md.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&char_attr_security_mode_md.write_perm);
    
        memset(&char_md, 0, sizeof(char_md));
        char_md.char_props.read   = 1;
        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, UUID_TRIGGER_EVENT_HISTORY_CHAR);
    
        memset(&attr_md, 0, sizeof(attr_md));
    
        attr_md.read_perm  = char_attr_security_mode_md.read_perm;
        attr_md.write_perm = char_attr_security_mode_md.cccd_write_perm;
        attr_md.vloc       = BLE_GATTS_VLOC_STACK;
        attr_md.rd_auth    = 1;
        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  = TRIGGER_EVENT_DATA_LEN;
        attr_char_value.init_offs = 0;
        attr_char_value.max_len   = TRIGGER_EVENT_DATA_LEN;
    //		attr_char_value.p_value = "abcde"; /////////////////////////////////////////////////////
    
        err_code = sd_ble_gatts_characteristic_add(p_jawbone->service_handle, &char_md,
                                                   &attr_char_value,
                                                   &p_jawbone->TriggerEventHistory_handle);	
    		if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    		return NRF_SUCCESS;
    }	

that is my code, but when i write value by maste control panel,but it return Erro code:write not permitted! where is my code error?

  • Where in that code do you believe you set the characteristic to be writable? The only characteristic property you set true is char_props.read, so it's only readable

  • You have mixed up something in your code:

    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&char_attr_security_mode_md.write_perm);
    

    This sets write permission to NO ACCESS, i.e. not writable.

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_security_mode_md.read_perm);
    

    This sets read permissions to OPEN, i.e. no access restrictions.

    You probably want the opposite.

    Afterwards, you also set

    attr_md.write_perm = char_attr_security_mode_md.cccd_write_perm;
    

    Which is not the write permission, but the cccd write permissions, which looks uninitialized.

    Additionally, as RK mentions in a comment, you should set the proper characteristic properties to give a hint to the peer about the permissions.

Related