Unable to connect to Adafruit Feather nRF52832 via BLE

I can not connect to an Adafruit nRF52 Feather using Blynk’s BLE widget.

I tried following the instructions posted by slobodan in the following thread Adafruit Feather nRF52 Bluefruit LE - nRF52832

Just like the last person who posted in that thread before it was marked as Solved/Closed, I am also unable to connect to the nRF52 using the BLE widget.

No matter what, I simply get the following…

“Can’t connect. Please retry or choose another device”

Adafruit Feather nRF52 Bluefruit LE - nRF52832
Blynk server
Blynk v0.6.0
Adafruit nRF52 BSP v0.9.3
iOS 12.1.2

Here’s an example sketch with the additions proposed by slobodan:

 This is an example for our nRF52 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <bluefruit.h>

#include <BlynkSimpleSerialBLE.h>
#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

char auth[] = "XXX";

BLEUart bleuart;

// Function prototypes for packetparser.cpp
uint8_t readPacket (BLEUart *ble_uart, uint16_t timeout);
float   parsefloat (uint8_t *buffer);
void    printHex   (const uint8_t * data, const uint32_t numBytes);

// Packet buffer
extern uint8_t packetbuffer[];

void setup(void)
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Serial.println(F("Adafruit Bluefruit52 Controller App Example"));
  Serial.println(F("-------------------------------------------"));

  Bluefruit.begin();
  // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  Bluefruit.setTxPower(4);
  Bluefruit.setName("Bluefruit52");

  // Configure and start the BLE Uart service
  bleuart.begin();

  // Set up and start advertising
  startAdv();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
  Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
  Serial.println();

  Blynk.begin(auth,bleuart);
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();
  
  // Include the BLE UART (AKA 'NUS') 128-bit UUID
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.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  
}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  Blynk.run();
}

In contrast, Adafruit’s Bluefruit LE Connect app works perfectly.

Did you follow the directions in the “solution” post?

Make sure you are using the latest Beta App for Blynk iOS as that might help.

Thanks for the quick reply.

Yes. I’ve been trying variations of that solution with zero success connecting via the BLE widget in Blynk.

I tried the latest iOS beta. No change. I get the same message: “Can’t connect. Please retry or choose another device.”

Well, all I can suggest is to send the error logging to the developers then, that is what the Beta App is for.

Back out of the project, as if to scroll to another one, click on the [i] in the upper right corner and choose Send Logs. Reference this topic URL.

There has been a change in the 0.6.0 library version, you should try using 0.5.4 library - it should work with current ios app.

Please switch to library v0.6.1, that also contains a fix for Bluetooth connectivity: https://github.com/blynkkk/blynk-library/releases/tag/v0.6.1

The latest version of the library works! I was able to connect and get the value of V1 echoed into the Serial Console after flashing the following code on Adafruit’s nRF52

#include <BlynkSimpleSerialBLE.h>
#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial
char auth[] = "96b0e13ab2d94bbab68b1f6baad2654f";

#include <bluefruit.h>

// BLE Service
BLEDis  bledis;  // device information
BLEUart bleuart; // uart over ble
BLEBas  blebas;  // battery

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
}

void setup()
{
  Serial.begin(115200);
//  while ( !Serial ) delay(10);   // for nrf52840 with native usb
  
  Serial.println("Blynk Bluefruit52 Example");
  Serial.println("---------------------------\n");

  // Setup the BLE LED to be enabled on CONNECT
  // Note: This is actually the default behaviour, but provided
  // here in case you want to control this LED manually via PIN 19
  Bluefruit.autoConnLed(true);

  // Config the peripheral connection with maximum bandwidth
  // more SRAM required by SoftDevice
  // Note: All config***() function must be called before begin()
  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);

  Bluefruit.begin();
  // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  Bluefruit.setTxPower(4);
  Bluefruit.setName("Bluefruit52");
  //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
  Bluefruit.setConnectCallback(connect_callback);
  Bluefruit.setDisconnectCallback(disconnect_callback);

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

  // Configure and Start BLE Uart Service
  bleuart.begin();

  // Start BLE Battery Service
  blebas.begin();
  blebas.write(100);

  // Set up and start advertising
  startAdv();

  // Init Blynk
  Blynk.begin(auth,bleuart);
}

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

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.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 loop()
{
  Blynk.run();
}

// callback invoked when central connects
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
 * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h
 */
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.println("Disconnected");

  Blynk.run();
}

Connecting to the nRF52 with the Blynk app frequently takes multiple attempts and is currently a far cry from Adafruit’s Bluefruit Connect app, at least on iOS. But it’s definitely working!

Huge thanks!

Thank you for this @01ivr3, very helpful.

I used this with Adafruit nRF52840 feather board.

I was able to connect with the Bluefruit Connect App on iOS and I observed over the UART connection the hardware was spitting out the Auth code in ascii repeatedly.

Even after trying naming the device “Blynk”, I found that the Beta version of the Blynk app would not connect automatically.

I finally used the BLE widget to connect and then the code started working.

So in summary:

  • Use this code as the example.
  • Get the beta Blynk app.
  • Use the BLE widget
  • Don’t forget to connect your widgets to virtual pins! :slight_smile:

This is the proper way to connect with BT/BLE. If you have any further issues, please create a new topic of your own with full details. Thank you.

@Gunner, any reference to clearer how-to and theory of operation to use BLE properly on Blynk would be much appreciated. Thanks!