Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite connect/disconnect loop when trying to reconnect #367

Open
asegurala opened this issue Aug 27, 2024 · 1 comment
Open

Infinite connect/disconnect loop when trying to reconnect #367

asegurala opened this issue Aug 27, 2024 · 1 comment
Labels
type: imperfection Perceived defect in any part of project

Comments

@asegurala
Copy link

asegurala commented Aug 27, 2024

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

@per1234 per1234 changed the title Bug on ArduinoBLE with Arduino Nano 33 BLE Sense. Infinite loop when trying to reconnect Infinite connect/disconnect loop when trying to reconnect Aug 27, 2024
@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Aug 27, 2024
@siddux

This comment was marked as duplicate.

@arduino-libraries arduino-libraries locked as too heated and limited conversation to collaborators Sep 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

3 participants