Skip to content

Added Eddystone-URL support #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/CurieBLE/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ written KEYWORD2
subscribed KEYWORD2

begin KEYWORD2
getAdvertisingLength KEYWORD2
getAdvertising KEYWORD2
setAdvertisedServiceUuid KEYWORD2
setAdvertisedServiceData KEYWORD2
setLocalName KEYWORD2
setAppearance KEYWORD2
setConnectionInterval KEYWORD2
Expand Down
53 changes: 52 additions & 1 deletion libraries/CurieBLE/src/BLEPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ BLEPeripheral::BLEPeripheral(void) :
_state(BLE_PERIPH_STATE_NOT_READY),
_advertise_service_uuid(NULL),
_local_name(NULL),
_service_data_uuid(NULL),
_service_data(NULL),
_service_data_length(0),
_appearance(0),
_min_conn_interval(DEFAULT_MIN_CONN_INTERVAL),
_max_conn_interval(DEFAULT_MAX_CONN_INTERVAL),
Expand Down Expand Up @@ -136,6 +139,18 @@ BLEPeripheral::end()
_stop();
}

uint8_t
BLEPeripheral::getAdvertisingLength()
{
return _adv_data_len;
}

uint8_t*
BLEPeripheral::getAdvertising()
{
return _adv_data;
}

void
BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
{
Expand All @@ -148,6 +163,14 @@ BLEPeripheral::setLocalName(const char* localName)
_local_name = localName;
}

void
BLEPeripheral::setAdvertisedServiceData(const char* serviceDataUuid, uint8_t* serviceData, uint8_t serviceDataLength)
{
_service_data_uuid = serviceDataUuid;
_service_data = serviceData;
_service_data_length = serviceDataLength;
}

void
BLEPeripheral::setDeviceName(const char deviceName[])
{
Expand Down Expand Up @@ -294,7 +317,7 @@ BLEPeripheral::_advDataInit(void)
if (BT_UUID16 == uuid.type) {
uint8_t *adv_tmp = &_adv_data[_adv_data_len];
*adv_tmp++ = (1 + sizeof(uint16_t)); /* Segment data length */
*adv_tmp++ = BLE_ADV_TYPE_INC_16_UUID;
*adv_tmp++ = BLE_ADV_TYPE_COMP_16_UUID; /* Needed for Eddystone */
UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
_adv_data_len += (2 + sizeof(uint16_t));
} else if (BT_UUID128 == uuid.type) {
Expand Down Expand Up @@ -324,6 +347,34 @@ BLEPeripheral::_advDataInit(void)
memcpy(adv_tmp, _local_name, calculated_len);
_adv_data_len += calculated_len + 2;
}

if (_service_data) {
/* Add Service Data (if it will fit) */

BLEUuid bleUuid = BLEUuid(_service_data_uuid);
struct bt_uuid uuid = bleUuid.uuid();

/* A 128-bit Service Data UUID won't fit in an Advertising packet */
if (BT_UUID16 != uuid.type) {
return; /* We support service data only for 16-bit service UUID */
}

uint8_t block_len = 1 + sizeof(uint16_t) + _service_data_length;
if (_adv_data_len + 1 + block_len > BLE_MAX_ADV_SIZE) {
return; // Service data block is too large.
}

adv_tmp = &_adv_data[_adv_data_len];

*adv_tmp++ = block_len;
_adv_data_len++;

*adv_tmp++ = BLE_ADV_TYPE_SERVICE_DATA_16_UUID;
UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
memcpy(adv_tmp, _service_data, _service_data_length);

_adv_data_len += block_len;
}
}

BleStatus
Expand Down
44 changes: 44 additions & 0 deletions libraries/CurieBLE/src/BLEPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ class BLEPeripheral {
*/
virtual ~BLEPeripheral(void);

/**
* Return the number of bytes in the advertising block.
* Useful for debugging advertising problems.
*
* @note Call only after calling begin().
*/
uint8_t getAdvertisingLength();

/**
* Returns a pointer to the advertising block
* of length getAdvertisingLength().
* Useful for debugging advertising problems.
*
* @note Call only after calling begin().
*/
uint8_t* getAdvertising();

/**
* Set the service UUID that the BLE Peripheral Device advertises
*
Expand All @@ -74,6 +91,30 @@ class BLEPeripheral {
*/
void setLocalName(const char* localName);

/**
* Set the Service Data that the BLE Peripheral Device advertises
*
* @param serviceDataUuid 16-bit Service UUID for this Service Data
* (in string form). Must match the UUID parameter
* of setAdvertisedServiceUuid(). To fit into BLE_MAX_ADV_SIZE,
* the UUID must be a 16-bit UUID.
*
* @param serviceData binary array of Service Data.
*
* @param serviceDataLength length (bytes) of serviceData[]
*
* @note the entire advertising packet must be no more than
* BLE_MAX_ADV_SIZE bytes, which is currently 31.
* This likely means that if you use Service Data
* there will not be room for a Local Name.
*
* @note if serviceDataUuid isn't 16-bits long, or if
* serviceDataLength won't fit in the advertising block,
* the service data will silently not be copied
* into the advertising block.
*/
void setAdvertisedServiceData(const char* serviceDataUuid, uint8_t* serviceData, uint8_t serviceDataLength);

/**
* Set the device name for the BLE Peripheral Device
*
Expand Down Expand Up @@ -194,6 +235,9 @@ class BLEPeripheral {

const char* _advertise_service_uuid;
const char* _local_name;
const char* _service_data_uuid;
uint8_t* _service_data;
uint8_t _service_data_length;
char _device_name[BLE_MAX_DEVICE_NAME+1];
uint16_t _appearance;
uint16_t _min_conn_interval;
Expand Down