Usage of callback functions, BLE exyample

Hi,

I have a question and I hope you can help me with that. I´m working with an Adafruit nRF52 Feather.

I´m developing a small Bluetooth Low Energy application and as basic I use the following code for Arduino:

I would like to know the connection state of my device. If a smartphone is connected to it via BLE or not. For this, in the linked code are two functions (I hope this is for that):

  • void connect_callback(uint16_t conn_handle)
  • void disconnect_callback(uint16_t conn_handle, uint8_t reason)

Now, in my loop I implemented it like this:

void loop()
{
  if (connect_callback) {
    connect = true;
  }
    else {
    connect = false;
  }
}

Unfortunaly the variable "connect" is always true, event if I connect with my smartphone or not...

I think I do something wrong in using this functions....could you say me WHAT?

Thanks in advance.

Simon

The callback functions are invoked by the library, not your code. Set your connect variable in the callbacks and check it in loop.

You did not use connect_callback as a function. Code snippets are no good. Please show ALL of your code.

http://snippets-r-us.com

If you look at the sample: you have to share the address of the callback function that you made with the library: this is done in the line below.

   Bluefruit.Periph.setConnectCallback(connect_callback);

And of course, this function must exist in your sketch as it exits in the example:

// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{  // Get the reference to current connection  
  BLEConnection* connection = Bluefruit.Connection(conn_handle);
  char central_name[32] = { 0 };  
  connection->getPeerName(central_name, sizeof(central_name));
  Serial.print("Connected to ");  
Serial.println(central_name);
}

If you want to know if you are connected set a flag in this function.
connect_callback is a function pointer, it contains the address of the function therefore it is always not 0 if the function exits in your sketch.
Best Regards,
Johi.

Hi,

thanks to you all.

I did it like wildbill said and set the variable in the callback-functions. This works very simple :slight_smile:

Thanks for your help!

Hi,

I have a similar question, maybe you can help me also with that.

I also have to know, when the device is advertising. For this, in the function

void startAdv(void)

is a line of the following code:

  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);

But how do I get the boolean value from this, if the device is advertising or not? I´m not sure, how this works...

I attached my hole code.

I hope you can help me with that. Thanks in advance.

Simon

FactoryTest6.ino (19.7 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.