The problem that I'm having is that after it connects to one device, then it stops scanning for any other devices until the first device is disconnected.
When the first device is disconnected, then it will continue scanning and connect to the next device and connect to that one.
I would like it to continue scanning after each device is connected so that multiple peripherals can be connected at the same time.
Here is the code that I'm using:
Code: Select all
#include <Arduino.h>
#include <bluefruit.h>
/**
* Callback invoked when an connection is established
* @param conn_handle
*/
void connect_callback(uint16_t conn_handle) {
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char peer_name[32] = { 0 };
connection->getPeerName(peer_name, sizeof(peer_name));
Serial.print("Connected to ");
Serial.println(peer_name);
Serial.println("Done Connecting.");
}
/**
* Callback invoked when a connection is terminated.
* @param conn_handle
* @param reason
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
(void) conn_handle;
(void) reason;
Serial.println("Disconnected!");
}
/**
* Callback invoked when scanner finds a device.
* @param report
*/
void scan_callback(ble_gap_evt_adv_report_t* report) {
uint8_t buffer[32];
memset(buffer, 0, sizeof(buffer));
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer))) {
// Check to see if device name is what we want to connect to.
if ((char) buffer[0] == 'V' && (char)buffer[1] == '7') {
Serial.printf("%14s %s\n", "Found Device ", buffer);
// MAC is in little endian --> print reverse
Serial.printBufferReverse(report->peer_addr.addr, 6, ':');
Serial.print("\n");
Serial.println("Attempting to connect...");
Bluefruit.Central.connect(report);
}
}
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner resume() to continue scanning
Bluefruit.Scanner.resume();
}
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 4
// SRAM usage required by SoftDevice will increase dramatically with number of connections
Bluefruit.begin(0, 4);
Bluefruit.setName("Bluefruit52");
// Start Central Scan
Bluefruit.setConnLedInterval(250);
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
Bluefruit.Scanner.useActiveScan(true);
Bluefruit.Scanner.start(0);
Serial.println("Scanning ...");
}
void loop() {
// nothing to do
}