Hello there everyone. I am running into a weird issue. When I connect my central to my two peripherals. It stays connected (which it ideally shouldn't, I will share the code and explain my logic). But when I disconnect one of the peripherals. It keeps getting connected and disconnected from the peripheral whilst sending some information.
So my trouble boils down to:
- Is the logic wrong in my code?
- Is this an issue with the BLE ?
- does the BLE not connect with the second device (I have made the in built LED to turn on whenever it connects to the peripheral. The 2nd peripheral does not glow after I have disconnected it and reconnected it to the central)
Steps to recreate my issue:
- turn both the peripheral on by connecting it to your PC (Change the service ID of each though, one Is 180f and another is 181f)
- Connect the central
- wait for both the peripheral to connect to the central (both of the peripheral lights will turn orange)
- Reset one of the peripheral
- observe the serial monitor from the central port
- The peripheral that has been reset continues to connect and disconnect. (which should not be happening)
Peripheral Code
#include <ArduinoBLE.h>
// Bluetooth® Low Energy Battery Service
BLEService helloWorldService("180F");
// Bluetooth® Low Energy Battery Level Characteristic
BLEUnsignedCharCharacteristic helloWorldChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
void setup() {
Serial.begin(9600); // initialize serial communication
// while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
/* Set a local name for the Bluetooth® Low Energy device
This name will appear in advertising packets
and can be used by remote devices to identify this Bluetooth® Low Energy device
The name can be changed but maybe be truncated based on space left in advertisement packet
*/
int helloWorld = 0;
BLE.setLocalName("Hello World 1");
BLE.setAdvertisedService(helloWorldService); // add the service UUID
helloWorldService.addCharacteristic(helloWorldChar); // add the battery level characteristic
BLE.addService(helloWorldService); // Add the battery service
helloWorldChar.writeValue(helloWorld); // set initial value for this characteristic
/* Start advertising Bluetooth® Low Energy. It will start continuously transmitting Bluetooth® Low Energy
advertising packets and will be visible to remote Bluetooth® Low Energy central devices
until it receives a new connection */
// start advertising
BLE.advertise();
Serial.println("Bluetooth® device active, waiting for connections...");
}
void loop() {
// wait for a Bluetooth® Low Energy central
BLEDevice central = BLE.central();
if(!central){
BLE.advertise();
digitalWrite(LED_BUILTIN, LOW);
}
// if a central is connected to the peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("Hello World from ");
int newValue = 1;
Serial.println(newValue);
helloWorldChar.writeValue(1);
delay(1000);
// and update the battery level characteristic
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
// Serial.print("Disconnected from central: ");
// Serial.println(central.address());
}
Central Code
#include <ArduinoBLE.h>
// variables for button
// const int buttonPin = 2;
// int oldButtonState = LOW;
// Bluetooth® Low Energy Battery Service
// BLEService helloWorldService("180F");
// // Bluetooth® Low Energy Battery Level Characteristic
// BLEUnsignedCharCharacteristic helloWorldChar("2A19", // standard 16-bit characteristic UUID
// BLERead | BLENotify);
void setup() {
Serial.begin(9600);
// while (!Serial);
// configure the button pin as input
// pinMode(buttonPin, INPUT);
// initialize the Bluetooth® Low Energy hardware
BLE.begin();
Serial.println("Bluetooth® Low Energy Central - LED control");
// start scanning for peripherals
// BLE.scanForUuid("180F");
BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum
}
void loop() {
// check if a peripheral has been discovered
char arr[3][30] = {"180F","181F", "Peripheral_3"};
int n = 2;
for(int i = 0;i<n;i++){
BLE.scanForUuid(arr[i]);
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
// stop scanning
BLE.stopScan();
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
BLECharacteristic helloWorldChar = peripheral.characteristic("2A19");
Serial.println("Hello World from ");
char value = '0';
// printData(helloWorldChar.value(), helloWorldChar.valueLength());
Serial.println(helloWorldChar.value()[0]);
// if(helloWorldService){
// Serial.println("Hello World from ");
// char value = '0';
// helloWorldChar.readValue(value);
// }
}
}
}
void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];
if (b < 16) {
Serial.print("0");
}
Serial.print(b, HEX);
}
}
My problem is that I am still receiving the data from the peripheral that keeps connecting and disconnecting. But according to my logic, the peripheral should stay connected for 1 second, which it does not.
I am new to the forum and programming in BLE, kindly let me know if I have made any mistakes. Thank you for taking the time to go through my question.
