Skip to content

Commit 4def2f2

Browse files
authored
Implement the BD_ADDR(char * address_string) constructor. (earlephilhower#1440)
* Implement the BD_ADDR(char * address_string) constructor. * Updating implementation to use sscanf. There is an extra step after the sscanf that checks that we got six bytes back and if we did not, it will set all bytes in the address to zero. * Example using BD_ADDR(const char * address_string) This example shows how BD_ADDR(const char * address_string) can be used to create BD_ADDR objects to use for comparisons etc. * Update LEDeviceScanner.ino formatting
1 parent b2b4b2d commit 4def2f2

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <BTstackLib.h>
2+
#include <SPI.h>
3+
4+
/*
5+
EXAMPLE_START(LEDeviceScanner): LE Device Scanner
6+
7+
@text The LE Device Scanner monitors BLE device advertisements,
8+
keeping track of one or more known devices as they appear.
9+
*/
10+
11+
// Application state
12+
int counter[2] = {0, 0};
13+
BD_ADDR known_devices[2] = {BD_ADDR("DB:88:B6:70:9E:EB"),
14+
BD_ADDR("C9:60:BD:F2:B4:9D")
15+
};
16+
17+
/*
18+
@section Setup
19+
20+
@text After BTstack.setup(), BTstack is configured to call
21+
advertisementCallback whenever an Advertisement was received.
22+
*/
23+
/* LISTING_START(LEDeviceScannerSetup): LE Device Scanner Setup */
24+
void setup(void) {
25+
Serial.begin(9600);
26+
BTstack.setBLEAdvertisementCallback(advertisementCallback);
27+
BTstack.setup();
28+
BTstack.bleStartScanning();
29+
}
30+
/* LISTING_END(LEDeviceScannerSetup): LE Device Scanner Setup */
31+
32+
/*
33+
@section Loop
34+
35+
@text In the standard Arduino loop() function, BTstack's loop() is called.
36+
*/
37+
/* LISTING_START(LEDeviceScannerLoop): Loop */
38+
void loop(void) {
39+
BTstack.loop();
40+
}
41+
/* LISTING_END(LEDeviceScannerLoop): Loop */
42+
43+
/*
44+
@section Advertisement Callback
45+
46+
@text Whenever an Advertisement is received, isIBeacon() checks if
47+
it contains an iBeacon.
48+
49+
If it's not an iBeacon, the BD_ADDR is compared to the address we are
50+
looking for and the counter is incremented.
51+
*/
52+
/* LISTING_START(LEDeviceScannerAdvertisementCallback): Advertisement Callback
53+
*/
54+
void advertisementCallback(BLEAdvertisement *bleAdvertisement) {
55+
if (!(bleAdvertisement->isIBeacon())) {
56+
Serial.print("Device discovered: ");
57+
Serial.println(bleAdvertisement->getBdAddr()->getAddressString());
58+
for (size_t i = 0; i < sizeof(counter) / sizeof(int); i++) {
59+
if (memcmp(bleAdvertisement->getBdAddr()->getAddress(),
60+
known_devices[i].getAddress(), sizeof(known_devices[i])) == 0) {
61+
counter[i]++;
62+
Serial.printf("Known device: %s, has been discovered %d times.\n",
63+
known_devices[i].getAddressString(), counter[i]);
64+
}
65+
}
66+
}
67+
}
68+
/* LISTING_END(LEDeviceScannerAdvertisementCallback): Advertisement Callback */

libraries/BTstackLib/src/BTstackLib.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,13 @@ BD_ADDR::BD_ADDR(void) {
396396
}
397397

398398
BD_ADDR::BD_ADDR(const char * address_string, BD_ADDR_TYPE address_type) : address_type(address_type) {
399-
(void) address_string;
400-
// TODO: implement
401-
// log_error("BD_ADDR::BD_ADDR(const char *, BD_ADDR_TYPE) not implemented yet!");
399+
int processed = sscanf(address_string, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", address, address + 1,
400+
address + 2, address + 3, address + 4, address + 5);
401+
if (processed != 6) { // Set address to zeroes if we did not get six bytes back.
402+
for (int i = 0; i < 6; i++) {
403+
address[i] = 0;
404+
}
405+
}
402406
}
403407

404408
BD_ADDR::BD_ADDR(const uint8_t address[6], BD_ADDR_TYPE address_type) : address_type(address_type) {

0 commit comments

Comments
 (0)