-
Notifications
You must be signed in to change notification settings - Fork 210
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
Communication of One Central Device and More than One Peripheral Device #50
Comments
Hi @vyohai , --- Peripheral device handling more centrals: #include <ArduinoBLE.h>
BLEService batteryService("180F");
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLEWrite);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
// Start BLE
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.debug(Serial);
// Configure peripheral
BLE.setLocalName("peripheral");
BLE.setAdvertisedService(batteryService);
batteryService.addCharacteristic(batteryLevelChar);
BLE.addService(batteryService);
BLE.advertise();
Serial.println("peripheral configured - central connected: 0");
}
void loop() {
BLE.poll();
// Led on if some central is connected
if (BLE.centralCount() > 0) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
// Check number of connected devices and enable advertising
static auto timeRef = millis();
if (millis() - timeRef >= 5000) {
timeRef = millis();
auto count = BLE.centralCount();
Serial.print("Central count: ");
Serial.println(count);
if (count < 3) {
BLE.advertise();
}
}
} --- Central device handling more peripherals: #include <ArduinoBLE.h>
void setup() {
Serial.begin(115200);
BLE.begin();
BLE.scanForName("peripheral");
}
void connectionLoop()
{
// If not already connected to 2 peripherals, try to connect to a new found peripheral.
if (BLE.peripheralCount() < 2) {
BLEDevice peripheral = BLE.available();
if (peripheral) {
BLE.stopScan();
if (!peripheral.connect()) {
Serial.println("Failed to connect!");
return;
}
if (!peripheral.discoverAttributes()) {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
}
}
}
auto timeRef = millis();
void loop() {
connectionLoop();
if (millis() - timeRef >= 5000) {
timeRef = millis();
int periphCount = BLE.peripheralCount();
Serial.print(" Peripheral connected: ");
Serial.println(periphCount);
if (periphCount < 2) {
BLE.scanForName("peripheral");
}
// Loop through all connected peripherals
for (int periphIdx = 0; periphIdx < periphCount; periphIdx++) {
BLEDevice peripheral = BLE.peripheral(periphIdx);
if (peripheral) {
BLECharacteristic batteryLevelChar = peripheral.characteristic("2A19");
if (!batteryLevelChar) {
Serial.println("Peripheral does not have battery level characteristic!");
peripheral.disconnect();
} else {
Serial.print("Peripheral connected, value: ");
batteryLevelChar.read();
Serial.println(*batteryLevelChar.value());
}
}
}
}
} |
Hi Polldo |
@vyohai to compile the example you should checkout the |
thank you it seems like it's working |
Hi @vyohai |
Hi @sprasadkrish , |
Hi @vyohai |
this is part of my code that runs on the master, hope you will find it halpfull
and than when you want to use it
|
Hi @vyohai , |
Hi @vyohai |
I will upload the whole master code:
|
Hi @vyohai |
Hey @vyohai |
Does the library support connection and communication between one central device and more than one peripheral device?
And how could it be implemented?
The text was updated successfully, but these errors were encountered: