Skip to content

Instantly share code, notes, and snippets.

@andkon
Created December 31, 2018 04:46
Show Gist options
  • Save andkon/f21b9692ac91effa51a784f262ed75b3 to your computer and use it in GitHub Desktop.
Save andkon/f21b9692ac91effa51a784f262ed75b3 to your computer and use it in GitHub Desktop.
Using a Redbear Duo to watch and store the last 100 BLE addresses.
#include <Adafruit_NeoPixel.h>
/*
* Defaultly disabled. More details: https://docs.particle.io/reference/firmware/photon/#system-thread
*/
//SYSTEM_THREAD(ENABLED);
/*
* SYSTEM_MODE:
* - AUTOMATIC: Automatically try to connect to Wi-Fi and the Particle Cloud and handle the cloud messages.
* - SEMI_AUTOMATIC: Manually connect to Wi-Fi and the Particle Cloud, but automatically handle the cloud messages.
* - MANUAL: Manually connect to Wi-Fi and the Particle Cloud and handle the cloud messages.
*
* SYSTEM_MODE(AUTOMATIC) does not need to be called, because it is the default state.
* However the user can invoke this method to make the mode explicit.
* Learn more about system modes: https://docs.particle.io/reference/firmware/photon/#system-modes .
*/
#if defined(ARDUINO)
SYSTEM_MODE(AUTOMATIC);
#endif
int led1 = D0;
int led2 = D7;
#define addressArrayLength 50
bd_addr_t seenAddresses[addressArrayLength];
/*
* BLE scan parameters:
* - BLE_SCAN_TYPE
* 0x00: Passive scanning, no scan request packets shall be sent.(default)
* 0x01: Active scanning, scan request packets may be sent.
* 0x02 - 0xFF: Reserved for future use.
* - BLE_SCAN_INTERVAL: This is defined as the time interval from when the Controller started its last LE scan until it begins the subsequent LE scan.
* Range: 0x0004 to 0x4000
* Default: 0x0010 (10 ms)
* Time = N * 0.625 msec
* Time Range: 2.5 msec to 10.24 seconds
* - BLE_SCAN_WINDOW: The duration of the LE scan. The scan window shall be less than or equal to the scan interval.
* Range: 0x0004 to 0x4000
* Default: 0x0010 (10 ms)
* Time = N * 0.625 msec
* Time Range: 2.5 msec to 10240 msec
*/
#define BLE_SCAN_TYPE 0x00 // Passive scanning
#define BLE_SCAN_INTERVAL 0x0060 // 60 ms
#define BLE_SCAN_WINDOW 0x0030 // 30 ms
int addToArray(bd_addr_t address, bd_addr_t addressArray[addressArrayLength]) {
int i;
for (i = 0; i < addressArrayLength; i++) {
if (addressArray[i][0] == 0 && addressArray[i][1] == 0) {
// add the address piece by piece to the index, because C
Serial.print("Adding address at: ");
Serial.println(i, DEC);
for (int index = 0; index < 6; index++) {
addressArray[i][index] = address[index];
}
return i;
}
}
// we got to the end of the array without finding a new spot
addToEndOfArray(address, addressArray);
return 0;
}
boolean addToEndOfArray(bd_addr_t address, bd_addr_t addressArray[addressArrayLength]) {
// iterate through the array, setting n to the value of n+1, until n == 99
for (int i = 0; i < addressArrayLength; i++) {
if (i == addressArrayLength - 1) {
// set final spot
for (int index = 0; index < 6; index++) {
addressArray[i][index] = address[index];
}
} else {
for (int index = 0; index < 6; index++) {
addressArray[i][index] = addressArray[i+1][index];
}
}
}
Serial.println("Added to end of array");
return true;
}
boolean compareAddresses(bd_addr_t addressA, bd_addr_t addressB) {
for (int index = 0; index < 6; index++) {
if (addressA[index] != addressB[index]) {
return false;
}
}
return true;
}
boolean addressInArray(bd_addr_t address, bd_addr_t addressArray[addressArrayLength]) {
// loop over the whole array:
int i;
for (i = 0; i < addressArrayLength; i++) {
// loop through the piece os the array:
if (addressArray[i][0] == 0 && addressArray[i][1] == 0) {
addToArray(address, addressArray);
return false;
} else {
// we're not at the end yet
// check if address is equivalent:
if (compareAddresses(addressArray[i], address)) {
return true;
}
}
}
// reached the end of the foor loop, and there were no matching arrays
addToEndOfArray(address, addressArray);
return false;
}
/*
* Advertising Event Type:
* - (0x00)BLE_GAP_ADV_TYPE_ADV_IND : Connectable undirected.
* - (0x01)BLE_GAP_ADV_TYPE_ADV_DIRECT_IND : Connectable directed.
* - (0x02)BLE_GAP_ADV_TYPE_ADV_SCAN_IND : Scannable undirected.
* - (0x03)BLE_GAP_ADV_TYPE_ADV_NONCONN_IND : Non connectable undirected.
* - (0x04)BLE_GAP_ADV_TYPE_SCAN_RSP : Scan response.
*/
void reportCallback(advertisementReport_t *report) {
addressInArray(report->peerAddr, seenAddresses);
for (int index = 0; index < 6; index++) {
Serial.print(report->peerAddr[index], HEX);
Serial.print("-");
}
Serial.println(" ");
}
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(115200);
delay(5000);
Serial.println("BLE scan demo.");
ble.init();
ble.onScanReportCallback(reportCallback);
// Set scan parameters.
ble.setScanParams(BLE_SCAN_TYPE, BLE_SCAN_INTERVAL, BLE_SCAN_WINDOW);
ble.startScanning();
Serial.println("BLE scan start.");
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment