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

ANCS notification problem

Hello everybody,

I'm working in a project & need to listen for notification from installed applications in the mobile phone & update the mobile app with some info about the notification like app identifier for example, that would work for the android platform, but in IOS, it's a bit difference due to ANCS, ANCS listens for notifications, do analysis for the notification & get the app identifier & I need to back this notifcation analysis to the mobile phone again, the problem here when I receive any notifcation, I got in ios lightblue "dismiss connection is stale" & after that it fails to connect to the device again:

here is the code example :

	/**@brief Function for setting up GATTC notifications from the Notification Provider.
 *
 * @details This function is called when a successful connection has been established.
 */
static void apple_notification_setup(void)
{
		uint32_t err_code;
		nrf_delay_ms(100); // Delay because we cannot add a CCCD to close to starting encryption. iOS specific.
		err_code = ble_ancs_c_notif_source_notif_enable(&m_ancs_c);
		app_error_check ( err_code )

		err_code = ble_ancs_c_data_source_notif_enable(&m_ancs_c);
		app_error_check ( err_code )
}

/**@brief Function for handling the Apple Notification Service client errors.
 *
 * @param[in] nrf_error  Error code containing information about what went wrong.
 */
static void apple_notification_error_handler(uint32_t nrf_error)
{
		APP_ERROR_HANDLER(nrf_error);
}
static void notify_update(ble_ancs_c_evt_notif_attr_t * p_attr,
                         ble_ancs_c_attr_list_t      * ancs_attr_list)
{
	 uint32_t err_code;
	 nrf_gpio_pin_toggle(LED_2);
	 if (p_attr->attr_len != 0 )
	 {	
		err_code = ble_notification_update(&m_notify, ancs_attr_list[p_attr->attr_id].p_attr_data);
		if ((err_code != NRF_SUCCESS) &&
				(err_code != NRF_ERROR_INVALID_STATE) &&
				(err_code != BLE_ERROR_NO_TX_BUFFERS) &&
				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
			)
			{
					APP_ERROR_HANDLER(err_code);
			}
		} 

}

/**@brief Function for handling the Apple Notification Service client.
 *
 * @details This function is called for all events in the Apple Notification client that
 *          are passed to the application.
 *
 * @param[in] p_evt  Event received from the Apple Notification Service client.
 */
static void on_ancs_c_evt(ble_ancs_c_evt_t * p_evt)
{

		switch (p_evt->evt_type)
		{
				case BLE_ANCS_C_EVT_DISCOVER_COMPLETE:
						nrf_gpio_pin_toggle(LED_1);
						apple_notification_setup();
						nrf_gpio_pin_toggle(LED_3);
						break;
				case BLE_ANCS_C_EVT_NOTIF:
			    	m_notification_latest = p_evt->notif;
						nrf_gpio_pin_toggle(LED_4);
						ble_ancs_c_request_attrs(&m_notification_latest);
						break;
				
				case BLE_ANCS_C_EVT_NOTIF_ATTRIBUTE:
          notify_update(&p_evt->attr, p_evt->ancs_attr_list);
						
				 // recive app notification and notify the application with app_ID  
						break;
				default:
						// No implementation needed.
						break;
		}
}

/**@brief Function for initializing the Apple Notification Center Service.
*/
static void ancs_init(void)
{
		ble_ancs_c_init_t ancs_init_obj;
		ble_uuid_t        service_uuid;
		uint32_t          err_code;

		err_code = sd_ble_uuid_vs_add(&ble_ancs_base_uuid128, &m_ancs_uuid_type);

		err_code = sd_ble_uuid_vs_add(&ble_ancs_cp_base_uuid128, &service_uuid.type);
		err_code = sd_ble_uuid_vs_add(&ble_ancs_ns_base_uuid128, &service_uuid.type);
		err_code = sd_ble_uuid_vs_add(&ble_ancs_ds_base_uuid128, &service_uuid.type);
		memset(&ancs_init_obj, 0, sizeof(ancs_init_obj));

		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER, m_attr_id_app, ATTR_DATA_SIZE);	
	err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_TITLE, m_attr_title, ATTR_DATA_SIZE);		
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_SUBTITLE,m_attr_subtitle, ATTR_DATA_SIZE);
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_MESSAGE, m_attr_message, ATTR_DATA_SIZE);
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_MESSAGE_SIZE, m_attr_message_size,ATTR_DATA_SIZE);
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_DATE, m_attr_date, ATTR_DATA_SIZE);
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_POSITIVE_ACTION_LABEL, m_attr_posaction, ATTR_DATA_SIZE);
		err_code = ble_ancs_c_attr_add(BLE_ANCS_NOTIF_ATTR_ID_NEGATIVE_ACTION_LABEL, m_attr_negaction, ATTR_DATA_SIZE);
	
		ancs_init_obj.evt_handler   = on_ancs_c_evt;
		ancs_init_obj.error_handler = apple_notification_error_handler;
		err_code = ble_ancs_c_init(&m_ancs_c, &ancs_init_obj);
		if ( err_code != NRF_SUCCESS )
				nrf_gpio_pin_toggle(LED_1);
}

& kindly check this.

Related