Skip to content

Commit 40d3fb0

Browse files
committed
Added Eddystone-URL support: added setAdvertisedServiceData() to support BLE Service Data; changed Incomplete to Complete Service UUID list code in Advertising initialization, required by Eddystone-URL protocol; added (for debugging) getAdvertisingLength() and getAdvertising() to enable a Sketch viewing the Advertizing packet.
1 parent 1f3899a commit 40d3fb0

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

Diff for: libraries/CurieBLE/keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ written KEYWORD2
4949
subscribed KEYWORD2
5050

5151
begin KEYWORD2
52+
getAdvertisingLength KEYWORD2
53+
getAdvertising KEYWORD2
5254
setAdvertisedServiceUuid KEYWORD2
55+
setAdvertisedServiceData KEYWORD2
5356
setLocalName KEYWORD2
5457
setAppearance KEYWORD2
5558
setConnectionInterval KEYWORD2

Diff for: libraries/CurieBLE/src/BLEPeripheral.cpp

+52-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ BLEPeripheral::BLEPeripheral(void) :
4747
_state(BLE_PERIPH_STATE_NOT_READY),
4848
_advertise_service_uuid(NULL),
4949
_local_name(NULL),
50+
_service_data_uuid(NULL),
51+
_service_data(NULL),
52+
_service_data_length(0),
5053
_appearance(0),
5154
_min_conn_interval(DEFAULT_MIN_CONN_INTERVAL),
5255
_max_conn_interval(DEFAULT_MAX_CONN_INTERVAL),
@@ -136,6 +139,18 @@ BLEPeripheral::end()
136139
_stop();
137140
}
138141

142+
uint8_t
143+
BLEPeripheral::getAdvertisingLength()
144+
{
145+
return _adv_data_len;
146+
}
147+
148+
uint8_t*
149+
BLEPeripheral::getAdvertising()
150+
{
151+
return _adv_data;
152+
}
153+
139154
void
140155
BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
141156
{
@@ -148,6 +163,14 @@ BLEPeripheral::setLocalName(const char* localName)
148163
_local_name = localName;
149164
}
150165

166+
void
167+
BLEPeripheral::setAdvertisedServiceData(const char* serviceDataUuid, uint8_t* serviceData, uint8_t serviceDataLength)
168+
{
169+
_service_data_uuid = serviceDataUuid;
170+
_service_data = serviceData;
171+
_service_data_length = serviceDataLength;
172+
}
173+
151174
void
152175
BLEPeripheral::setDeviceName(const char deviceName[])
153176
{
@@ -294,7 +317,7 @@ BLEPeripheral::_advDataInit(void)
294317
if (BT_UUID16 == uuid.type) {
295318
uint8_t *adv_tmp = &_adv_data[_adv_data_len];
296319
*adv_tmp++ = (1 + sizeof(uint16_t)); /* Segment data length */
297-
*adv_tmp++ = BLE_ADV_TYPE_INC_16_UUID;
320+
*adv_tmp++ = BLE_ADV_TYPE_COMP_16_UUID; /* Needed for Eddystone */
298321
UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
299322
_adv_data_len += (2 + sizeof(uint16_t));
300323
} else if (BT_UUID128 == uuid.type) {
@@ -324,6 +347,34 @@ BLEPeripheral::_advDataInit(void)
324347
memcpy(adv_tmp, _local_name, calculated_len);
325348
_adv_data_len += calculated_len + 2;
326349
}
350+
351+
if (_service_data) {
352+
/* Add Service Data (if it will fit) */
353+
354+
BLEUuid bleUuid = BLEUuid(_service_data_uuid);
355+
struct bt_uuid uuid = bleUuid.uuid();
356+
357+
/* A 128-bit Service Data UUID won't fit in an Advertising packet */
358+
if (BT_UUID16 != uuid.type) {
359+
return; /* We support service data only for 16-bit service UUID */
360+
}
361+
362+
uint8_t block_len = 1 + sizeof(uint16_t) + _service_data_length;
363+
if (_adv_data_len + 1 + block_len > BLE_MAX_ADV_SIZE) {
364+
return; // Service data block is too large.
365+
}
366+
367+
adv_tmp = &_adv_data[_adv_data_len];
368+
369+
*adv_tmp++ = block_len;
370+
_adv_data_len++;
371+
372+
*adv_tmp++ = BLE_ADV_TYPE_SERVICE_DATA_16_UUID;
373+
UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
374+
memcpy(adv_tmp, _service_data, _service_data_length);
375+
376+
_adv_data_len += block_len;
377+
}
327378
}
328379

329380
BleStatus

Diff for: libraries/CurieBLE/src/BLEPeripheral.h

+44
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ class BLEPeripheral {
5555
*/
5656
virtual ~BLEPeripheral(void);
5757

58+
/**
59+
* Return the number of bytes in the advertising block.
60+
* Useful for debugging advertising problems.
61+
*
62+
* @note Call only after calling begin().
63+
*/
64+
uint8_t getAdvertisingLength();
65+
66+
/**
67+
* Returns a pointer to the advertising block
68+
* of length getAdvertisingLength().
69+
* Useful for debugging advertising problems.
70+
*
71+
* @note Call only after calling begin().
72+
*/
73+
uint8_t* getAdvertising();
74+
5875
/**
5976
* Set the service UUID that the BLE Peripheral Device advertises
6077
*
@@ -74,6 +91,30 @@ class BLEPeripheral {
7491
*/
7592
void setLocalName(const char* localName);
7693

94+
/**
95+
* Set the Service Data that the BLE Peripheral Device advertises
96+
*
97+
* @param serviceDataUuid 16-bit Service UUID for this Service Data
98+
* (in string form). Must match the UUID parameter
99+
* of setAdvertisedServiceUuid(). To fit into BLE_MAX_ADV_SIZE,
100+
* the UUID must be a 16-bit UUID.
101+
*
102+
* @param serviceData binary array of Service Data.
103+
*
104+
* @param serviceDataLength length (bytes) of serviceData[]
105+
*
106+
* @note the entire advertising packet must be no more than
107+
* BLE_MAX_ADV_SIZE bytes, which is currently 31.
108+
* This likely means that if you use Service Data
109+
* there will not be room for a Local Name.
110+
*
111+
* @note if serviceDataUuid isn't 16-bits long, or if
112+
* serviceDataLength won't fit in the advertising block,
113+
* the service data will silently not be copied
114+
* into the advertising block.
115+
*/
116+
void setAdvertisedServiceData(const char* serviceDataUuid, uint8_t* serviceData, uint8_t serviceDataLength);
117+
77118
/**
78119
* Set the device name for the BLE Peripheral Device
79120
*
@@ -194,6 +235,9 @@ class BLEPeripheral {
194235

195236
const char* _advertise_service_uuid;
196237
const char* _local_name;
238+
const char* _service_data_uuid;
239+
uint8_t* _service_data;
240+
uint8_t _service_data_length;
197241
char _device_name[BLE_MAX_DEVICE_NAME+1];
198242
uint16_t _appearance;
199243
uint16_t _min_conn_interval;

0 commit comments

Comments
 (0)