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

ANCS can't receive the event when add CTS service to project.

Can anyone help me?

Each service is working standalone.

There are two cases:

1.the BLE can't connect auto by iphone.

2.firmware can't receive notify event.

static void services_init(void)
{
usr_ancs_services_init();
usr_cts_service_init();
}


void usr_ancs_services_init(void)
{
ble_ancs_c_init_t ancs_init_obj;
ret_code_t ret;

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

ret = nrf_ble_ancs_c_attr_add(&m_ancs_c,
BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER,
m_attr_appid,
ATTR_DATA_SIZE);
APP_ERROR_CHECK(ret);

ret = nrf_ble_ancs_c_attr_add(&m_ancs_c,
BLE_ANCS_NOTIF_ATTR_ID_TITLE,
m_attr_title,
ATTR_DATA_SIZE);
APP_ERROR_CHECK(ret);

ret = nrf_ble_ancs_c_attr_add(&m_ancs_c,
BLE_ANCS_NOTIF_ATTR_ID_MESSAGE_SIZE,
m_attr_message_size,
ATTR_DATA_SIZE);
APP_ERROR_CHECK(ret);

ret = nrf_ble_ancs_c_attr_add(&m_ancs_c,
BLE_ANCS_NOTIF_ATTR_ID_MESSAGE,
m_attr_message,
ATTR_DATA_SIZE);
APP_ERROR_CHECK(ret);

ancs_init_obj.evt_handler = on_ancs_c_evt;
ancs_init_obj.error_handler = apple_notification_error_handler;

usr_db_discovery_add_handler(usr_db_disc_handler);
APP_ERROR_CHECK(ble_ancs_c_init(&m_ancs_c, &ancs_init_obj));
}

void usr_cts_service_init(void)
{
ble_cts_c_init_t cts_init_obj;

cts_init_obj.evt_handler = on_cts_c_evt;
cts_init_obj.error_handler = current_time_error_handler;

usr_db_discovery_add_handler(db_disc_handler);
APP_ERROR_CHECK(ble_cts_c_init(&m_cts_c, &cts_init_obj));
}

#include "usr_db_discovery.h"
#include "ble_types.h"
#include "ble_srv_common.h"
#include "sdk_errors.h"
#include "ble_db_discovery.h"
#include "nrf_sdh_ble.h"
#include "app_error.h"
#include <string.h>

BLE_DB_DISCOVERY_DEF(m_db_disc);

static ble_db_discovery_evt_handler_t s_handler_list[5] = {0};
static uint16_t s_handler_list_length = 0;

static void usr_db_disc_handler(ble_db_discovery_evt_t * p_evt)
{
for (uint16_t i = 0; i < s_handler_list_length; i++) {
s_handler_list[i](p_evt);
}
}

uint32_t usr_db_discovery_start(uint16_t conn_handle)
{
APP_ERROR_CHECK(ble_db_discovery_start(&m_db_disc, conn_handle));
return NRF_SUCCESS;
}

void usr_db_discovery_init(void)
{
memset(&s_handler_list, 0, sizeof(s_handler_list));
s_handler_list_length = 0;

APP_ERROR_CHECK(ble_db_discovery_init(usr_db_disc_handler));
}

void usr_db_discovery_add_handler(ble_db_discovery_evt_handler_t h)
{
s_handler_list[s_handler_list_length] = h;
s_handler_list_length++;
}

  • Hi, 

    It was a known issue. Please follow this answer from my colleague: 

    It has taken some time to dig into this. The relevant change between SDK 14.2.0 and 15.0.0 is that the ANCS example now subscribes to the service changed characteristic (and perform a service discovery when there is an indication on it). However, the example does not store the GATT database so that it can be reused between connections. Therefor the example also does a service discovery upon every reconnect. This is not the intended use of the service changed characteristic, and it seems that iOS will disconnect in this case, where the peer subscribes to the service changed characteristic but still does service discovery without an indication.

    There are several possible ways to fix this issue:

    1. The simplest is to comment out the call to nrf_ble_gatts_c_init() in main.c of the ANCS example. This will give it the same behavior as in SDK 14.2, where service changed is not subscribed to.
    2. Properly handle service changed indication, storing the database between connections. You can see how this is implemented in the GATT Service Client Example Application and use that as a reference if you want to implement this for ANCS.
  • thank you for your help. now I facing other question, if I "forget this device " on iphone then connect the buletooth , I get a PM_EVT_CONN_SEC_FAILED error. so I call to pm_peers_delete() then I get another error NRF_ERROR_CONN_COUNT,  can you give me some suggestion?

  • Hi, 

    If you set allow_repairing = False, the device will throw PM_EVT_CONN_SEC_FAILED if the phone lose bond information. 

    Set it to true, will allow repairing, but you have  the risk of attacker can spoof that it has lost bond information and re-pair to you. 

    Are you sure pm_peers_delete() throwing NRF_ERROR_CONN_COUNT ? Could you step into the code and check which function threw that and why ? 

Related