Skip to content
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

allow advertising updates at runtime #198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class BLEDevice
BLERemoteAttribute** /*remoteAttributes*/,
unsigned char /*numRemoteAttributes*/) { }

virtual void updateAdvertisementData(unsigned char /*advertisementDataSize*/,
BLEEirData * /*advertisementData*/,
unsigned char /*scanDataSize*/,
BLEEirData * /*scanData*/) { }

virtual void poll() { }

virtual void end() { }
Expand Down
19 changes: 15 additions & 4 deletions src/BLEPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ BLEPeripheral::~BLEPeripheral() {
}
}

void BLEPeripheral::begin() {
unsigned char BLEPeripheral::updateAdvertismentData() {
unsigned char advertisementDataSize = 0;

BLEEirData advertisementData[3];
BLEEirData scanData;

scanData.length = 0;

unsigned char remainingAdvertisementDataLength = BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH + 2;
Expand Down Expand Up @@ -131,6 +128,11 @@ void BLEPeripheral::begin() {
memcpy(scanData.data, this->_localName, scanData.length);
}

return advertisementDataSize;
}

void BLEPeripheral::begin() {

if (this->_localAttributes == NULL) {
this->initLocalAttributes();
}
Expand Down Expand Up @@ -158,6 +160,7 @@ void BLEPeripheral::begin() {
this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic);
}

int advertisementDataSize = updateAdvertismentData();
this->_device->begin(advertisementDataSize, advertisementData,
scanData.length > 0 ? 1 : 0, &scanData,
this->_localAttributes, this->_numLocalAttributes,
Expand Down Expand Up @@ -203,6 +206,14 @@ void BLEPeripheral::setBondStore(BLEBondStore& bondStore) {
this->_device->setBondStore(bondStore);
}

void BLEPeripheral::startAdvertising() {
int advertisementDataSize = updateAdvertismentData();
this->_device->updateAdvertisementData(
advertisementDataSize, advertisementData,
scanData.length > 0 ? 1 : 0, &scanData);
this->_device->startAdvertising();
}

void BLEPeripheral::setDeviceName(const char* deviceName) {
this->_deviceNameCharacteristic.setValue(deviceName);
}
Expand Down
5 changes: 5 additions & 0 deletions src/BLEPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);

void startAdvertising();

void setDeviceName(const char* deviceName);
void setAppearance(unsigned short appearance);
Expand Down Expand Up @@ -126,6 +127,7 @@ class BLEPeripheral : public BLEDeviceEventListener,

private:
void initLocalAttributes();
unsigned char updateAdvertismentData();

private:
BLEDevice* _device;
Expand Down Expand Up @@ -158,6 +160,9 @@ class BLEPeripheral : public BLEDeviceEventListener,

BLECentral _central;
BLEPeripheralEventHandler _eventHandlers[4];

BLEEirData advertisementData[3];
BLEEirData scanData;
};

#endif
85 changes: 47 additions & 38 deletions src/nRF51822.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,52 @@ nRF51822::~nRF51822() {
this->end();
}

void nRF51822::updateAdvertisementData(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData)
{
unsigned char srData[31];
unsigned char srDataLen = 0;

this->_advDataLen = 0;

// flags
this->_advData[this->_advDataLen + 0] = 2;
this->_advData[this->_advDataLen + 1] = 0x01;
this->_advData[this->_advDataLen + 2] = 0x06;

this->_advDataLen += 3;

if (advertisementDataSize && advertisementData) {
for (int i = 0; i < advertisementDataSize; i++) {
this->_advData[this->_advDataLen + 0] = advertisementData[i].length + 1;
this->_advData[this->_advDataLen + 1] = advertisementData[i].type;
this->_advDataLen += 2;

memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length);

this->_advDataLen += advertisementData[i].length;
}
}

if (scanDataSize && scanData) {
for (int i = 0; i < scanDataSize; i++) {
srData[srDataLen + 0] = scanData[i].length + 1;
srData[srDataLen + 1] = scanData[i].type;
srDataLen += 2;

memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length);

srDataLen += scanData[i].length;
_hasScanData = true;
}
}

sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen);
}


void nRF51822::begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
Expand Down Expand Up @@ -187,44 +233,7 @@ void nRF51822::begin(unsigned char advertisementDataSize,
sd_ble_gap_ppcp_set(&gap_conn_params);
sd_ble_gap_tx_power_set(0);

unsigned char srData[31];
unsigned char srDataLen = 0;

this->_advDataLen = 0;

// flags
this->_advData[this->_advDataLen + 0] = 2;
this->_advData[this->_advDataLen + 1] = 0x01;
this->_advData[this->_advDataLen + 2] = 0x06;

this->_advDataLen += 3;

if (advertisementDataSize && advertisementData) {
for (int i = 0; i < advertisementDataSize; i++) {
this->_advData[this->_advDataLen + 0] = advertisementData[i].length + 1;
this->_advData[this->_advDataLen + 1] = advertisementData[i].type;
this->_advDataLen += 2;

memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length);

this->_advDataLen += advertisementData[i].length;
}
}

if (scanDataSize && scanData) {
for (int i = 0; i < scanDataSize; i++) {
srData[srDataLen + 0] = scanData[i].length + 1;
srData[srDataLen + 1] = scanData[i].type;
srDataLen += 2;

memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length);

srDataLen += scanData[i].length;
_hasScanData = true;
}
}

sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen);
updateAdvertisementData(advertisementDataSize, advertisementData, scanDataSize, scanData);
sd_ble_gap_appearance_set(0);

for (int i = 0; i < numLocalAttributes; i++) {
Expand Down
5 changes: 5 additions & 0 deletions src/nRF51822.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class nRF51822 : public BLEDevice
BLERemoteAttribute** remoteAttributes,
unsigned char numRemoteAttributes);

virtual void updateAdvertisementData(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData);

virtual void poll();

virtual void end();
Expand Down