RedBearLab / nRF51822-Arduino

Moved to https://github.com/redbear/nRF5x
251 stars 108 forks source link

nRF51822+S130+HRSensor= disconnection #74

Closed gaucho1978 closed 8 years ago

gaucho1978 commented 8 years ago

I'm using nRF51822 (RedBear Nano board) with MBED library that uses S130 Nordik library.

There is an implementation that let's me use mbed library over arduino:

I use the nRF51822 simoultaeously as master role (in order to connect a HR sensor, a Polar H7 HRS) and as peripherial role (to let tablets to connect to my custom device).

This is what I do:

1) I perform a BT scan and I start a Connection from a tablet and then I successfully start to receive notifications from nRF51822 viewing them on the tablet log.

2) I Perform a BT scan from nRF51822 and it finds the HR device (at this point everything continues to work correctly)

3) I perform a BT connection from nRF51822 to the HR sensor and i register for notifications and i start to receive Heart Rate Data from the HRS, viewing them on a serial print of the RedBearNano. At the same moment the notifications stops to be received on the tablet.

I can't understand why this happens. In order to start to receive again the notifications on the tablet, i must request, from the tablet, a disconnection of the peripherial (the nRF51822) and then request again a Connection and register again for notifications.

Looking on the sniffed packets, it seems there is nothing strange (because I also own a bluefruit sniffer).

I add 2 important links to my previous message:

1) the MBED + Arduino library that I am using (this library ): https://github.com/RedBearLab/nRF51822-Arduino

2) the code that I am running for this DEBUG (here annexed) treadmill_nano.zip

gaucho1978 commented 8 years ago

Can you guide me to check the variables responsible for the problem?

the routine that sends the notifications to the tablet is the updateCharacteristicValue that you can find in my code. that routine corresponds to the write() routine in https://github.com/RedBearLab/nRF51822-Arduino/blob/60d6139e11ab065b8c5af7d4bfbd75f90bc36fea/arduino-1.6.x/hardware/RBL/RBL_nRF51822/cores/RBL_nRF51822/ble-nrf51822-master/source/nRF5xGattServer.cpp (see from line 212).

this write function calls the NORDIC sd_ble_gatts_hvx function .

when the call goes fine it returns 0. When the problem occours it returns ERROR_BLEGATTS_SYS_ATTR_MISSING.

How could I check variables to understand what is going wrong?

gaucho1978 commented 8 years ago

I read here ( https://devzone.nordicsemi.com/documentation/nrf51/4.3.0/html/group___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html#gafc37f2050903a4dc91f4527b59e6457a ) that when you get the ATTR_MISSING error you should use the sd_ble_gatts_sys_attr_set function but I suspect that in my case there is a memory corruption problem..

gaucho1978 commented 8 years ago

Problem solved: When you call the updateCharacteristicValue you can not avoid to set the connection handle. It seems that when you have multiple connections it is mandatory to specify it. In fact if I call the .updateCharacteristicValue(connectionHandle,characteristicHandle, buffer, bufferLength) instead of .updateCharacteristicValue(characteristicHandle, buffer, bufferLength) the problem disappers.