0

nRF52 setWriteCallback never calling writeCallback
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

nRF52 setWriteCallback never calling writeCallback

by tpitman on Wed Dec 05, 2018 5:16 pm

I have the nRF52 Feather board from Adafruit. I am trying to make a custom service and be able to write to the characteristic.

Here is the code:

Code: Select all | TOGGLE FULL SIZE
void writeCallback(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset)
{
  Serial.println(1);
}

uint8_t commandData[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void setupPulsar(void)
{
  pulsars.begin();

  pulsarc_command.setProperties(CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
  pulsarc_command.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
  pulsarc_command.setMaxLen(10);
  pulsarc_command.setWriteCallback(writeCallback);
  pulsarc_command.begin();
  pulsarc_command.notify(commandData, sizeof(commandData));
}



When I use my iPhone LightBlue app I can connect to it and look at the characteristic. It shows the correct properties. If I try to write to it I don't get any errors, but my writeCallback never gets called.

I have also added the cccdWriteCallback and that works. It prints out my notify enable and disable, so I assume callbacks in general are working.

I also tried calling notify in the loop function and it does notify the iPhone correctly.

I actually have a version of the code that does actual work inside the writeCallback function, but took it out and just have the serial println because nothing was every happening.

So why won't my writeCallback get called?

tpitman
 
Posts: 66
Joined: Mon Sep 21, 2015 3:42 pm

Re: nRF52 setWriteCallback never calling writeCallback

by hathach on Thu Dec 06, 2018 2:15 pm

First can you tell us your bsp version ? And if possible create a simpliest sketch that could help us to reproduce your issue that could help us to run the sketch to check,

hathach
 
Posts: 799
Joined: Tue Apr 23, 2013 1:02 am

Re: nRF52 setWriteCallback never calling writeCallback

by tpitman on Thu Dec 06, 2018 2:30 pm

I think you just mean the version in the board manager. That is: Adafruit nRF52 version 0.9.1.

Here is a simple sketch that creates 2 characteristics. When I subscribe using my phone I get that callback, but when I write to either characteristic the writeCallback never gets called.

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <bluefruit.h>

uint8_t const _pulsars_uuid[16]         = {0xEB, 0xB1, 0xE9, 0x51, 0x42, 0x52, 0x5E, 0x95, 0x0B, 0x40, 0xB8, 0x7A, 0x00, 0x00, 0x29, 0x94};
uint8_t const _pulsarc_command_uuid[16] = {0xEB, 0xB1, 0xE9, 0x51, 0x42, 0x52, 0x5E, 0x95, 0x0B, 0x40, 0xB8, 0x7A, 0x01, 0x00, 0x29, 0x94};
uint8_t const _pulsarc_data_uuid[16]    = {0xEB, 0xB8, 0xB1, 0x51, 0x42, 0x52, 0x5E, 0x95, 0x0B, 0x40, 0xB8, 0x7A, 0x02, 0x00, 0x29, 0x94};

BLEUuid pulsars_uuid(_pulsars_uuid);
BLEUuid pulsarc_command_uuid(_pulsarc_command_uuid);
BLEUuid pulsarc_data_uuid(_pulsarc_data_uuid);

BLEService        pulsars         = BLEService(pulsars_uuid);
BLECharacteristic pulsarc_command = BLECharacteristic(pulsarc_command_uuid);
BLECharacteristic pulsarc_data    = BLECharacteristic(pulsarc_data_uuid);

BLEDis bledis;    // DIS (Device Information Service) helper class instance

void startAdv(void);
void setupPulsar(void);
void connect_callback(uint16_t conn_handle);
void disconnect_callback(uint16_t conn_handle, uint8_t reason);

void setup() {
  Serial.begin(115200);
  SPI.begin();

  Bluefruit.begin();
  Bluefruit.setName("ABC Pulsar");
  Bluefruit.setConnectCallback(connect_callback);
  Bluefruit.setDisconnectCallback(disconnect_callback);

  bledis.setManufacturer("ABC Co");
  bledis.setModel("Pulsar Controller");
  bledis.begin();

  setupPulsar();
  startAdv();
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  Bluefruit.Advertising.addService(pulsars);
  Bluefruit.Advertising.addName();
 
  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   *
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html   
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds 
}

void cccdCallback(BLECharacteristic& chr, uint16_t cccd_value)
{
  // Display the raw request packet
  Serial.print("CCCD Updated: ");
  //Serial.printBuffer(request->data, request->len);
  Serial.print(cccd_value);
  Serial.println("");

  // Check the characteristic this CCCD update is associated with in case
  // this handler is used for multiple CCCD records.
  if (chr.uuid == pulsarc_command.uuid) {
    if (chr.notifyEnabled()) {
      Serial.println("Command 'Notify' enabled");
    }
    else {
      Serial.println("Command 'Notify' disabled");
    }
  }
  else if (chr.uuid == pulsarc_data.uuid) {
    if (chr.notifyEnabled()) {
      Serial.println("Data 'Notify' enabled");
    }
    else {
      Serial.println("Data 'Notify' disabled");
    }
  }
}

uint8_t commandData[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dataData[] = {0, 0, 0, 0, 0, 0, 0, 0};

void setupPulsar(void)
{
  pulsars.begin();

  pulsarc_command.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
  pulsarc_command.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
  pulsarc_command.setMaxLen(10);
  pulsarc_command.setWriteCallback(writeCallback);
  pulsarc_command.setCccdWriteCallback(cccdCallback);
  pulsarc_command.begin();
  pulsarc_command.notify(commandData, sizeof(commandData));

  pulsarc_data.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
  pulsarc_data.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
  pulsarc_data.setMaxLen(8);
  pulsarc_data.setWriteCallback(writeCallback);
  pulsarc_data.setCccdWriteCallback(cccdCallback);
  pulsarc_data.begin();
  pulsarc_command.notify(dataData, sizeof(dataData));
}

void connect_callback(uint16_t conn_handle)
{
  char central_name[32] = { 0 };
  Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

/**
 * Callback invoked when a connection is dropped
 * @param conn_handle connection where this event happens
 * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
 */
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println("Disconnected");
  Serial.println("Advertising!");
}

void writeCallback(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset)
{
  Serial.println("got write callback");
}

void loop() {
  yield();
  delay(100);
}

tpitman
 
Posts: 66
Joined: Mon Sep 21, 2015 3:42 pm

Please be positive and constructive with your questions and comments.