Skip to content

Infinite connect/disconnect loop when trying to reconnect #367

Open
@asegurala

Description

@asegurala

I’m trying to create a Bluetooth HID keyboard with a Nano 33 BLE Sense

Everything works well on the first connection, but if I try to reconnect later, the Nano 33 BLE Sense enters an infinite loop of connecting/disconnecting.

The only way to reconnect on the aforementioned operating systems is to remove the paired device first and then pair it again as if it were the first connection. The connect/disconnect loop does not occur when I do that.

#include <ArduinoBLE.h>

#define HID_SERVICE_UUID        "1812"
#define HID_INFORMATION_UUID    "2A4A"
#define HID_CONTROL_POINT_UUID  "2A4C"
#define HID_REPORT_UUID         "2A4D"
#define HID_REPORT_MAP_UUID     "2A4B"
#define BATTERY_SERVICE_UUID    "180F"
#define BATTERY_LEVEL_UUID      "2A19"

// Report map para un teclado simple
const uint8_t reportMap[] = {
  0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  0x09, 0x06, // Usage (Keyboard)
  0xA1, 0x01, // Collection (Application)
  0x05, 0x07, // Usage Page (Kbrd/Keypad)
  0x19, 0xE0, // Usage Minimum (0xE0)
  0x29, 0xE7, // Usage Maximum (0xE7)
  0x15, 0x00, // Logical Minimum (0)
  0x25, 0x01, // Logical Maximum (1)
  0x75, 0x01, // Report Size (1)
  0x95, 0x08, // Report Count (8)
  0x81, 0x02, // Input (Data,Var,Abs)
  0x95, 0x01, // Report Count (1)
  0x75, 0x08, // Report Size (8)
  0x81, 0x01, // Input (Cnst,Var,Abs)
  0x95, 0x05, // Report Count (5)
  0x75, 0x01, // Report Size (1)
  0x05, 0x08, // Usage Page (LEDs)
  0x19, 0x01, // Usage Minimum (Num Lock)
  0x29, 0x05, // Usage Maximum (Kana)
  0x91, 0x02, // Output (Data,Var,Abs)
  0x95, 0x01, // Report Count (1)
  0x75, 0x03, // Report Size (3)
  0x91, 0x01, // Output (Cnst,Var,Abs)
  0x95, 0x06, // Report Count (6)
  0x75, 0x08, // Report Size (8)
  0x15, 0x00, // Logical Minimum (0)
  0x25, 0x65, // Logical Maximum (101)
  0x05, 0x07, // Usage Page (Kbrd/Keypad)
  0x19, 0x00, // Usage Minimum (0)
  0x29, 0x65, // Usage Maximum (0x65)
  0x81, 0x00, // Input (Data,Array,Abs)
  0xC0        // End Collection
};

BLEService hidService(HID_SERVICE_UUID);
BLECharacteristic hidInformationCharacteristic(HID_INFORMATION_UUID, BLERead, 4);
BLECharacteristic hidControlPointCharacteristic(HID_CONTROL_POINT_UUID, BLEWriteWithoutResponse, 1);
BLECharacteristic hidReportMapCharacteristic(HID_REPORT_MAP_UUID, BLERead, sizeof(reportMap));
BLECharacteristic hidReportCharacteristic(HID_REPORT_UUID, BLERead | BLENotify, 8);

BLEService batteryService(BATTERY_SERVICE_UUID);
BLECharacteristic batteryLevelCharacteristic(BATTERY_LEVEL_UUID, BLERead | BLENotify, 1);

bool isConnected = false;

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

  if (!BLE.begin()) {
    Serial.println("Error initializing BLE!");
    while (1);
  }

  BLE.setLocalName("BLE Keyboard");
  BLE.setAppearance(0x03C1); // keyboard
  BLE.setPairable(1);
  BLE.setConnectable(true);

  BLE.setConnectionInterval(30, 100);
  BLE.setSupervisionTimeout(1000);
  BLE.setAdvertisedService(hidService);
  hidService.addCharacteristic(hidInformationCharacteristic);
  hidService.addCharacteristic(hidControlPointCharacteristic);
  hidService.addCharacteristic(hidReportMapCharacteristic);
  hidService.addCharacteristic(hidReportCharacteristic);

  BLE.addService(hidService);

  uint8_t hidInfo[] = {0x01, 0x01, 0x00, 0x02}; // HID Version 1.1, Country Code, Not using RemoteWake, Normally Connectable
  hidInformationCharacteristic.writeValue(hidInfo, sizeof(hidInfo));
  hidReportMapCharacteristic.writeValue(reportMap, sizeof(reportMap));

  BLE.setAdvertisedService(batteryService);
  batteryService.addCharacteristic(batteryLevelCharacteristic);
  BLE.addService(batteryService);
  uint8_t batteryLevel = 100;
  batteryLevelCharacteristic.writeValue(batteryLevel);
    
  BLE.setEventHandler(BLEConnected, onBLEConnected);
  BLE.setEventHandler(BLEDisconnected, onBLEDisconnected);

  BLE.advertise();
  Serial.println("Waiting for a connection...");
}

void loop() {
  BLE.poll(); 

}

void onBLEConnected(BLEDevice central) {
  Serial.print("Connected to: ");
  Serial.println(central.address());
}

void onBLEDisconnected(BLEDevice central) {
   Serial.println("Waiting for a new connection...");
   BLE.advertise();
}

ArduinoBLE version

1.3.7

Additional context

I have reproduced the fault when pairing to the Nano 33 BLE Sense from Linux, Windows 11, and Android 13 machines.

Additional reports

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions