Skip to content

Commit 84ab587

Browse files
committed
Merge branch 'pr-53'
2 parents c59b431 + c975869 commit 84ab587

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <rduinoBLE.h>
2+
3+
void setup() {
4+
Serial.begin(9600);
5+
while (!Serial);
6+
7+
// begin initialization
8+
if (!BLE.begin()) {
9+
Serial.println("starting BLE failed!");
10+
11+
while (1);
12+
}
13+
14+
Serial.println("BLE Central scan");
15+
16+
// start scanning for peripheral
17+
BLE.scan();
18+
}
19+
20+
void loop() {
21+
// check if a peripheral has been discovered
22+
BLEDevice peripheral = BLE.available();
23+
24+
if (!peripheral) {
25+
return;
26+
}
27+
if (!peripheral.hasManufacturerData()) {
28+
return;
29+
}
30+
31+
String manufacturerData = peripheral.manufacturerData();
32+
if (manufacturerData.length() < 25 * 2) {
33+
return;
34+
}
35+
if (manufacturerData.substring(0, 8) != "4c000215") {
36+
return;
37+
}
38+
39+
// Discovered an iBeacon
40+
Serial.println("Discovered an iBeacon");
41+
Serial.println("-----------------------");
42+
43+
// UUID
44+
String id = manufacturerData.substring(8, 16) + "-";
45+
id += manufacturerData.substring(16, 20) + "-";
46+
id += manufacturerData.substring(20, 24) + "-";
47+
id += manufacturerData.substring(24, 28) + "-";
48+
id += manufacturerData.substring(28, 40);
49+
Serial.print("UUID: ");
50+
Serial.println(id);
51+
52+
// Major ID
53+
char buf[5];
54+
manufacturerData.substring(40, 44).toCharArray(buf, 5);
55+
int major = (int)strtol(buf, NULL, 16);
56+
Serial.print("Major ID: ");
57+
Serial.println(major);
58+
59+
// Minor ID
60+
manufacturerData.substring(44, 48).toCharArray(buf, 5);
61+
int minor = (int)strtol(buf, NULL, 16);
62+
Serial.print("Minor ID: ");
63+
Serial.println(minor);
64+
65+
// RSSI
66+
Serial.print("RSSI: ");
67+
Serial.println(peripheral.rssi());
68+
69+
Serial.println();
70+
}

src/BLEDevice.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "utility/ATT.h"
2121
#include "utility/BLEUuid.h"
22+
#include "utility/BLEManufacturerData.h"
2223
#include "utility/HCI.h"
2324

2425
#include "remote/BLERemoteDevice.h"
@@ -85,6 +86,11 @@ bool BLEDevice::hasLocalName() const
8586
return (localName().length() > 0);
8687
}
8788

89+
bool BLEDevice::hasManufacturerData() const
90+
{
91+
return (manufacturerData().length() > 0);
92+
}
93+
8894
bool BLEDevice::hasAdvertisedServiceUuid() const
8995
{
9096
return hasAdvertisedServiceUuid(0);
@@ -146,6 +152,25 @@ String BLEDevice::localName() const
146152
return localName;
147153
}
148154

155+
String BLEDevice::manufacturerData() const
156+
{
157+
String manufacturerData = "";
158+
159+
for (int i = 0; i < _eirDataLength;) {
160+
int eirLength = _eirData[i++];
161+
int eirType = _eirData[i++];
162+
163+
if (eirType == 0xFF) {
164+
manufacturerData = BLEManufacturerData::manufacturerDataToString(&_eirData[i], eirLength);
165+
break;
166+
}
167+
168+
i += (eirLength - 1);
169+
}
170+
171+
return manufacturerData;
172+
}
173+
149174
String BLEDevice::advertisedServiceUuid() const
150175
{
151176
return advertisedServiceUuid(0);

src/BLEDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ class BLEDevice {
5050
virtual String address() const;
5151

5252
bool hasLocalName() const;
53-
53+
bool hasManufacturerData() const;
54+
5455
bool hasAdvertisedServiceUuid() const;
5556
bool hasAdvertisedServiceUuid(int index) const;
5657
int advertisedServiceUuidCount() const;
5758

5859
String localName() const;
60+
String manufacturerData() const;
5961
String advertisedServiceUuid() const;
6062
String advertisedServiceUuid(int index) const;
6163

src/utility/BLEManufacturerData.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <stdlib.h>
21+
#include <string.h>
22+
23+
#include "BLEManufacturerData.h"
24+
25+
const char* BLEManufacturerData::manufacturerDataToString(const uint8_t* data, uint8_t length)
26+
{
27+
static char manufacturerData[32 * 2 + 1];
28+
char* c = manufacturerData;
29+
30+
for (int i = 0; i < length - 1; i++) {
31+
uint8_t b = data[i];
32+
33+
utoa(b >> 4, c++, 16);
34+
utoa(b & 0x0f, c++, 16);
35+
}
36+
37+
*c = '\0';
38+
39+
return manufacturerData;
40+
}

src/utility/BLEManufacturerData.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _BLE_MANUFACTURER_DATA_H_
21+
#define _BLE_MANUFACTURER_DATA_H_
22+
23+
#include <Arduino.h>
24+
25+
class BLEManufacturerData
26+
{
27+
public:
28+
static const char* manufacturerDataToString(const uint8_t* data, uint8_t length);
29+
};
30+
31+
#endif

0 commit comments

Comments
 (0)