Bonding nrf52832 iOS bluetooth settings or otherwise

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mchambe1
 
Posts: 13
Joined: Tue Jul 10, 2018 9:08 pm

Bonding nrf52832 iOS bluetooth settings or otherwise

Post by mchambe1 »

Related to a previous forum question I asked a few months ago. Don't know if in this case it's recommended to comment again on the same thread or to start a new one, could someone advise on that?

Anyways, I'm working with the nrf52832 and ultimately am hoping to use it in tandem with a custom iOS app for logging user data. My first issue is that I haven't been able to figure out how to get the board advertise such that it's visible within the iOS bluetooth settings and able to pair (or bond) from there. I can connect through various companion apps (Bluefruit, LightBlue) but always with those apps, the connection is broken anytime the app is closed and must be manually created once again each time the app is re-opened.

I'm hoping for some guidance regarding how I might go about having the board advertise such that a bond can be created. If not being done through iOS settings, is this a connection event that I can specify from the server side, or is it driven by the client and so I'd need to have the code of the app written to facilitate that?

The BLEHID class allows bonding through the iOS settings in this way. But I have not been successful in trimming the BLEHID example code in such a way that I can have my board bond through iOS settings without the HIDKeyboard part actually being initiated. https://learn.adafruit.com/bluefruit-nr ... idadafruit

If anyone could advise on this or point me in the right direction, I would be extremely grateful. Thanks!

In case relevant or of interest, below is the advertising code I have at the moment:

Advertising code:

Code: Select all

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


  // Include HRM Service UUID
  Bluefruit.Advertising.addService(isvs);

  // Include Name
  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
}
Full code:

Code: Select all

#include <bluefruit.h>
#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

// MYSERVICE_UUID: 67C57C9D-08E3-4AFE-886D-5058D5983F1B, written as an array in little endian

const uint8_t MYSERVICE_UUID[] =
{
  0x1B, 0x3F, 0x98, 0xD5, 0x58, 0x50, 0x6D, 0x88,
  0xFE, 0x4A, 0xE3, 0x08, 0x9D, 0x7C, 0xC5, 0x67
};

// MYCHR_UUID: 67C57C9D-08E3-4AFE-886D-5058D5983F1C, written as an array in little endian

const uint8_t MYCHR_UUID[] =
{
  0x1C, 0x3F, 0x98, 0xD5, 0x58, 0x50, 0x6D, 0x88,
  0xFE, 0x4A, 0xE3, 0x08, 0x9D, 0x7C, 0xC5, 0x67
};

byte Count = 0;
int New_Max;
int Volume;

BLEService isvs(MYSERVICE_UUID); // Custom IS Volume Service
BLECharacteristic isvc(MYCHR_UUID); // Custom IS Volume Characteristic
BLEHidAdafruit blehid;


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

// Advanced function prototypes
void startAdv(void);
void setupVolTrack(void);
void connect_callback(uint16_t conn_handle);
void disconnect_callback(uint16_t conn_handle, uint8_t reason);

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

  Serial.println("I.S. Trial Bluetooth");

  lox.begin();

  // Initialise the Bluefruit module
  Serial.println("Initialise the Bluefruit nRF52 module");
  Bluefruit.begin();

  Bluefruit.setTxPower(4);

  // Set the advertised device name (keep it short!)
  Serial.println("Setting Device Name to 'IS Puck2'");
  Bluefruit.setName("IS PUCK2");

  // Set the connect/disconnect callback handlers
  Bluefruit.setConnectCallback(connect_callback);
  Bluefruit.setDisconnectCallback(disconnect_callback);

  // Configure and Start the Device Information Service
  Serial.println("Configuring the Device Information Service");
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  // Setup the IS Volume Service service using
  // BLEService and BLECharacteristic classes
  Serial.println("Configuring the IS Volume Service");
  setupVolTrack();

  // Setup the advertising packet(s)
  Serial.println("Setting up the advertising payload(s)");
  startAdv();

  Serial.println("Advertising");
}

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


  // Include HRM Service UUID
  Bluefruit.Advertising.addService(isvs);

  // Include Name
  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 setupVolTrack(void)
{
  isvs.begin();

  isvc.setProperties(CHR_PROPS_NOTIFY); // Setup as notify
  isvc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); // First value sets read permissions, second write
  isvc.setFixedLen(2); // Indicates how many  bytes the characteristic has. Can be 1-20 for 'notify'.
  isvc.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates
  isvc.begin();
  isvc.notify32(Volume);
}

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 cccd_callback(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 == isvc.uuid) {
    if (chr.notifyEnabled()) {
      Serial.println("Volume measurement 'Notify' enabled");
    } else {
      Serial.println("Volume measurement 'Notify' disabled");
    }
  }
}

void loop()
{
  digitalToggle(LED_RED);

  VL53L0X_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {
    Volume = round(0.2078 * sq(measure.RangeMilliMeter) + 10.679 * measure.RangeMilliMeter - 319.4);

    if (Bluefruit.connected() ) {
      if (isvc.notify32(Volume)) {
        Serial.print("Volume captured: "); Serial.println(Volume);
      }
      else {
        Serial.println("ERROR: Notify not set in the CCCD or not connected!");
      }
    }
  }

  delay(1000);

}

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”