A
A
afch2022-04-20 00:27:07
Arduino
afch, 2022-04-20 00:27:07

How to set up automatic BLE reconnection for a project on Mbed BLE HID for Nano 33?

Went 4th week as I can not solve the problem. So I admitted defeat and decided to ask the public a question.
I am analyzing the ble_shining_kb example from the Mbed BLE HID library for Arduino Nano 33 BLE, which, as I understand it, is based on Mbed OS.
The problem is that every time the Arduino is turned on or just when the phone approaches the arduino (at a distance sufficient to communicate via BLE), a message appears on the phone every time asking you to connect to the Arduino Nano 33, which must be manually confirmed.
Reading the Mbed OS documentation hints at using the Security Manager, BUT I can't figure out how to access it! And what parameters should he specify in order to get automatic reconnection when the connection breaks?!
ps Phone Samsung Galaxy S9+
Link to the repository : https://github.com/tcoppex/mbed-ble-hid e... .... trying....

///
///   ble_shining_kb.ino
///   
///   created: 2020-11
///
///  Simulate an evil writing machine in an isolated hotel on winter. 
///  No hardware required other than an Nano33 ble.
///

#include "Nano33BleHID.h"
#include "signal_utils.h"

/* -------------------------------------------------------------------------- */

Nano33BleKeyboard bleKb("Shining Keyboard");

// Tracking index for the end of the writing animation ticker.
int sTickerIndex = -1;

static const char kSentence[] = "All work and no play makes Jack a dull boy";
static const int kNumTickerSteps = 4;

// How long it takes for the sentence to be written.
static const int kSentenceDurationMilliseconds = 4029;

// How long it takes before the sentence is rewritten.
static const int kSentenceDelayMilliseconds = 1977;

// How long one writing animation will run.
static const int kSentenceTotalTimeMilliseconds = kSentenceDurationMilliseconds + kSentenceDelayMilliseconds;

// Safeguard to terminate this mess of an app before going crazy.
static const int kTotalRuntime = 8 * kSentenceTotalTimeMilliseconds;

// Builtin LED animation delays when disconnect. 
static const int kLedBeaconDelayMilliseconds = 1185;
static const int kLedErrorDelayMilliseconds = kLedBeaconDelayMilliseconds / 10;

// Builtin LED intensity when connected.
static const int kLedConnectedIntensity = 30;

/* -------------------------------------------------------------------------- */

/** Utility struct to send a text through the Keyboard HID for a given time. */
struct SentenceWriter {
  std::string sentence;
  int current_index;
  unsigned long duration_ms;

  SentenceWriter(const char* str, unsigned long duration_ms)
    : sentence(str)
    , current_index(-1)
    , duration_ms(duration_ms)
  {}

  void write(HIDKeyboardService &kb, unsigned long frame_time)
  {
    // Calculate the absolute time in the animation (in [0.0f, 1.0f[)
    float dt = frame_time / float(duration_ms);
    
    // Add a smooth interpolation to add "lifeness".
    dt = smoothstep(0.0f, 1.0f, dt);
    
    // Map absolute time to the letter index.
    const int index = floor(dt * sentence.size());

    // When the index change, send its letter via the keyboard service.
    if (current_index != index) {
      current_index = index;
      const uint8_t letter = sentence[index];
      kb.sendCharacter(letter);
    }
  } 
} stringWriter(kSentence, kSentenceDurationMilliseconds);

/* -------------------------------------------------------------------------- */

void setup()
{
  // General setup.
  pinMode(LED_BUILTIN, OUTPUT);

  // Initialize both BLE and the HID.
  bleKb.initialize();

  // Launch the event queue that will manage both BLE events and the loop. 
  // After this call the main thread will be halted.
  MbedBleHID_RunEventThread();
}

void loop()
{
  // When disconnected, we animate the builtin LED to indicate the device state.
  if (bleKb.connected() == false) {
    animateLED(LED_BUILTIN, (bleKb.has_error()) ? kLedErrorDelayMilliseconds 
                                                : kLedBeaconDelayMilliseconds);
    return;
  }

  // When connected, we slightly dim the builtin LED.
  analogWrite(LED_BUILTIN, kLedConnectedIntensity);

  // Stop when we reach a certain runtime (better safe than sorry).
  if (bleKb.connection_time() > kTotalRuntime) {
   // return;
  }

  // Retrieve the HIDService to update.
  auto *kb = bleKb.hid();

  // Local time in the looping animation.
  unsigned long frame_time = bleKb.connection_time() % kSentenceTotalTimeMilliseconds;

  // The animation is divided in two parts :
  if (frame_time < kSentenceDurationMilliseconds)
  {
    // Write the sentence using the StringWriter object.
    stringWriter.write(*kb, frame_time);
  }
  else
  {
    // Wait by writing dots at a different speed (using the same logic as a StringWriter).
    float dt = (frame_time-kSentenceDurationMilliseconds) / float(kSentenceDelayMilliseconds);
          dt = 1.0f - (1.0f - dt) * pow(dt, dt); // slow-out
    int index = floor(kNumTickerSteps*dt);
    if (sTickerIndex != index) {
      kb->sendCharacter('.');
      sTickerIndex = index;
    }
  }
}

/* -------------------------------------------------------------------------- */

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2022-04-20
@SilenceOfWinter

The problem is that every time the Arduino is turned on or just when the phone approaches the arduino (at a distance sufficient to communicate via BLE), a message appears on the phone every time asking you to connect to the Arduino Nano 33, which must be manually confirmed.

this is the essence of this example, not the problem)
ble_shining_kb
Simulate a ghost writer repeating a sentence over and over again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question