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

Inconsistent behavior with db discovery between resets, Service Not Found.

Chip: stock NRF52840DK
SDK: 16.0.0

I am using nRF52840 DK to connect to a third party peripheral, I used the HRS Central example as a template for adding central functionality, and removed the Peer Manager as my peripheral does not support bonding. It is also using incorrect service IDs, its primary service advertises a 16 bit ID that is not registered with Bluetooth SIG (0xFFF0), hence why I am using BLE_UUID_TYPE_BLE. 

I am noticing some strange and inconsistent behavior when running ble_db_discovery_start

After flashing the application from a blank chip, on the first run the discovery will always halt after connecting, no other events will occur.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
<info> app: Setting vector table to bootloader: 0x000F8000
<info> app: Setting vector table to main app: 0x00027000
<info> app_timer: RTC: initialized.
<debug> ble_scan: Adding filter on Viecar name
<info> app: Buttonless DFU Application started.
<debug> app: advertising is started
<info> app: Starting scan.
<debug> ble_scan: Scanning
<debug> ble_scan: Connecting
<debug> ble_scan: Connection status: 0
<info> app: Connected.
<debug> nrf_ble_gq: Registering connection handle: 0x0000
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

After a reset, the application will have a couple of random outcomes. 

Fullscreen
1
2
3
4
5
6
7
8
9
10
<info> app: Connected.
<debug> nrf_ble_gq: Purging request queue with id: 0
<debug> nrf_ble_gq: Registering connection handle: 0x0000
<debug> ble_db_disc: Starting discovery of service with UUID 0xFFF0 on connection handle 0x0.
<debug> nrf_ble_gq: Adding item to the request queue
<debug> nrf_ble_gq: GATTC Primary Services Discovery Request
<debug> nrf_ble_gq: SD GATT procedure (2) succeeded on connection handle: 0.
<debug> nrf_ble_gq: Processing the request queue...
<debug> ble_db_disc: Service UUID 0xFFF0 not found.
<info> ble_elm327_c: disc service id FFF0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The first, is saying again service 0xFFF0 not found, and then dispatching db_discovery events for that service (as you can see from the nrf log of p_evt->params.discovered_db.srv_uuid.uuid inside of the db_discovery event handler) it seems strange to me that the discovery module would log that the service could not be found and then immediately dispatch events for that service. 

The other outcome is like the above, except no discovery events are emitted, not even ones for unable to find service. I dont believe this is at fault of the peripheral, as discovery via nrf connect works as expected. 

My custom service file: 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "ble_elm327_interface.h"
#include "ble_db_discovery.h"
#include "ble_types.h"
#include "ble_gattc.h"
#define NRF_LOG_MODULE_NAME ble_elm327_c
#include "nrf_log.h"
NRF_LOG_MODULE_REGISTER();
/**@brief Function for interception of the errors of GATTC and the BLE GATT Queue.
*
* @param[in] nrf_error Error code.
* @param[in] p_ctx Parameter from the event handler.
* @param[in] conn_handle Connection handle.
*/
static void gatt_error_handler(uint32_t nrf_error,
void * p_ctx,
uint16_t conn_handle)
{
ble_elm327_c_t * p_ble_elm327_c = (ble_elm327_c_t *)p_ctx;
NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

My Main.c: 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX