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

Bond a BLE Peripheral with an IOS Device

Hello,

I have a project that started out as a Peripheral BLE_HRS example. I am currently running SDK 9.0.0.

I am able to Bond a nRF51 Central with a nRF51 Peripheral running on Eval boards. The Device Manager stores the System Context the first time they connect and bonds are maintained appropriately. However, my device will also be controlled by an IOS application and this is where the problem arises.

I have read that in order to force IOS to bond with a peripheral, you have to place security on one of the Characteristics. I have two Characteristics; one dedicated to receiving commands FROM the central and the other dedicated to sending Alerts TO the central. This all works fine.

All of your examples seem to use open security or no security:

BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);

Here is the initialization code for the Characteristics:

static uint32_t send_to_app_char_add(ble_qiqi_t * p_qiqi, const ble_qiqi_init_t * p_qiqi_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  = 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.type = p_qiqi->uuid_type;
    ble_uuid.uuid = QIQI_UUID_SEND_CHAR;
    
    memset(&attr_md, 0, sizeof(attr_md));

    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm);

    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm);

    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 1;
    
    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     = 20;
    attr_char_value.init_offs    = 0;
    attr_char_value.max_len      = 20; // sizeof(uint8_t);
    attr_char_value.p_value      = NULL;
    
    return sd_ble_gatts_characteristic_add(p_qiqi->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_qiqi->send_to_app_char_handles);
}

/**@brief Function for adding the get_from_app characteristic.
 *
 */
static uint32_t get_from_app_char_add(ble_qiqi_t * p_qiqi, const ble_qiqi_init_t * p_qiqi_init)
{
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_uuid;
    ble_gatts_attr_md_t attr_md;

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

    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&cccd_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&cccd_md.write_perm);

    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&cccd_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&cccd_md.write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);

    cccd_md.vloc = BLE_GATTS_VLOC_STACK;
    
    memset(&char_md, 0, sizeof(char_md));
    
    char_md.char_props.read   = 1;
    char_md.char_props.notify = 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         = &cccd_md;
    char_md.p_sccd_md         = NULL;
    
    ble_uuid.type = p_qiqi->uuid_type;
    ble_uuid.uuid = QIQI_UUID_GET_CHAR;
    
    memset(&attr_md, 0, sizeof(attr_md));

    //BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm);
    //BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);

    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 1;
    
    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     = 2;//sizeof(uint8_t);
    attr_char_value.init_offs    = 0;
    attr_char_value.max_len      = 20;//sizeof(uint8_t);
    attr_char_value.p_value      = NULL;
    
    return sd_ble_gatts_characteristic_add(p_qiqi->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_qiqi->get_from_app_char_handles);
}

As you can see from the lines that are commented out, I have tried several other Security Modes.

In one case I can get it to Bond with IOS, and the System Context is Stored in the Peripheral, however, after this happens I cannot Send or Receive data from the Peripheral. Are there other settings or Events I need to handle to make this work properly?

Can you please help me with what configuration changes I need to make in order to force IOS to Bond AND still be able to communicate between the Periph and the Central?

Thank you!

Parents Reply Children
No Data
Related