I have the following sketch that I am running on an Arduino Primo.
The sketch scans for BLE Beacons that have the Device_Name as "ECO".
This works great, but once I have received an advertising packet from a Beacon that has the Device_Name "ECO", I must stop the scan.
With the following code, the scan is continuous:
/*
scanner.ino
Written by Chiara Ruggeri (chiara@arduino.org)
This example for the Arduino Primo board implements the
Observer role of the BLE protocol.
Once scan parameters are set, the sketch continuously
listen for advertising packets.
This example code is in the public domain.
*/
#include "BLECentralRole.h"
// download ArduinoLowPower library from library manager to enter in low power mode
#include "ArduinoLowPower.h"
// create central instance
BLECentralRole bleCentral = BLECentralRole();
void setup() {
Serial.begin(9600);
// assign event handler for scanReceived event
bleCentral.setEventHandler(BLEScanReceived, receiveAdvPck);
// set scan parameters
// interval and window in 0.625 ms increments
bleCentral.setScanInterval(3200); // 2 sec
//bleCentral.setScanWindow(800); // 0.5 sec
bleCentral.setScanWindow(1600); // 1 sec
// timeout in seconds. 0 disables timeout
bleCentral.setScanTimeout(0);
// active scan true to ask for scan response packet
bleCentral.setActiveScan(true);
// begin initialization and start scanning
bleCentral.begin();
}
void loop() {
// since we want to realize a low power application we don't handle the
// BLE_LED in order to save power but put the board in low power mode instead.
LowPower.sleep();
}
void receiveAdvPck(BLEPeripheralPeer& peer){
char advertisedName[31];
byte len;
// search for a device that advertises "ECO" name
peer.getFieldInAdvPck(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, advertisedName, len);
if(len == 0) // field not found
peer.getFieldInAdvPck(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, advertisedName, len);
if((len != 0)&&(len == 3)){ // the field was found
if(!strcmp(advertisedName, "ECO")) // Name found.
{
// Correct name found - Now get RSSI
int8_t rssi = peer.rssi();
const char *address = peer.address();
Serial.print("Name ECO found: " );
Serial.println(advertisedName);
Serial.print("RSSI: " );
Serial.println(rssi);
Serial.print("MAC: " );
Serial.println(address);
Serial.println("------------------------------------------");
// I must now STOP the scan process.
bleCentral.setActiveScan(false);
}
else // Name NOT found.
{
Serial.print("Name ECO not found: ");
Serial.println(advertisedName);
}
}
}