Skip to content

Commit 53c8add

Browse files
committed
Add ability to update connection params after connect
1 parent d44ea0a commit 53c8add

File tree

7 files changed

+53
-10
lines changed

7 files changed

+53
-10
lines changed

src/BLEDevice.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BLEDeviceEventListener
2727
virtual void BLEDeviceDisconnected(BLEDevice& /*device*/) { }
2828
virtual void BLEDeviceBonded(BLEDevice& /*device*/) { }
2929
virtual void BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) { }
30+
virtual void BLEDeviceConnectionParamsUpdated(BLEDevice& /*device*/) { }
3031

3132
virtual void BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& /*characteristic*/, const unsigned char* /*value*/, unsigned char /*valueLength*/) { }
3233
virtual void BLEDeviceCharacteristicSubscribedChanged(BLEDevice& /*device*/, BLECharacteristic& /*characteristic*/, bool /*subscribed*/) { }
@@ -53,6 +54,7 @@ class BLEDevice
5354

5455
void setAdvertisingInterval(unsigned short advertisingInterval);
5556
void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
57+
virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) { }
5658
void setConnectable(bool connectable);
5759
void setBondStore(BLEBondStore& bondStore);
5860

src/BLEPeripheral.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ void BLEPeripheral::setConnectionInterval(unsigned short minimumConnectionInterv
241241
this->_device->setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
242242
}
243243

244+
void BLEPeripheral::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
245+
this->_device->updateConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
246+
}
247+
244248
void BLEPeripheral::disconnect() {
245249
this->_device->disconnect();
246250
}
@@ -363,6 +367,18 @@ void BLEPeripheral::BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) {
363367
}
364368
}
365369

370+
void BLEPeripheral::BLEDeviceConnectionParamsUpdated(BLEDevice& /*device*/) {
371+
#ifdef BLE_PERIPHERAL_DEBUG
372+
Serial.print(F("Updated connection parameters from central: "));
373+
Serial.println(this->_central.address());
374+
#endif
375+
376+
BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnectionParamsUpdated];
377+
if (eventHandler) {
378+
eventHandler(this->_central);
379+
}
380+
}
381+
366382
void BLEPeripheral::BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) {
367383
characteristic.setValue(this->_central, value, valueLength);
368384
}

src/BLEPeripheral.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ enum BLEPeripheralEvent {
4444
BLEConnected = 0,
4545
BLEDisconnected = 1,
4646
BLEBonded = 2,
47-
BLERemoteServicesDiscovered = 3
47+
BLERemoteServicesDiscovered = 3,
48+
BLEConnectionParamsUpdated = 4
4849
};
4950

5051
typedef void (*BLEPeripheralEventHandler)(BLECentral& central);
@@ -71,6 +72,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
7172
// connection intervals in 1.25 ms increments,
7273
// must be between 0x0006 (7.5 ms) and 0x0c80 (4 s), values outside of this range will be ignored
7374
void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
75+
void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
7476
bool setTxPower(int txPower);
7577
void setConnectable(bool connectable);
7678
void setBondStore(BLEBondStore& bondStore);
@@ -109,6 +111,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
109111
virtual void BLEDeviceDisconnected(BLEDevice& device);
110112
virtual void BLEDeviceBonded(BLEDevice& device);
111113
virtual void BLEDeviceRemoteServicesDiscovered(BLEDevice& device);
114+
virtual void BLEDeviceConnectionParamsUpdated(BLEDevice& device);
112115

113116
virtual void BLEDeviceCharacteristicValueChanged(BLEDevice& device, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength);
114117
virtual void BLEDeviceCharacteristicSubscribedChanged(BLEDevice& device, BLECharacteristic& characteristic, bool subscribed);
@@ -152,7 +155,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
152155
BLERemoteCharacteristic _remoteServicesChangedCharacteristic;
153156

154157
BLECentral _central;
155-
BLEPeripheralEventHandler _eventHandlers[4];
158+
BLEPeripheralEventHandler _eventHandlers[5];
156159
};
157160

158161
#endif

src/nRF51822.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,7 @@ void nRF51822::poll() {
558558

559559
if (this->_minimumConnectionInterval >= BLE_GAP_CP_MIN_CONN_INTVL_MIN &&
560560
this->_maximumConnectionInterval <= BLE_GAP_CP_MAX_CONN_INTVL_MAX) {
561-
ble_gap_conn_params_t gap_conn_params;
562-
563-
gap_conn_params.min_conn_interval = this->_minimumConnectionInterval; // in 1.25ms units
564-
gap_conn_params.max_conn_interval = this->_maximumConnectionInterval; // in 1.25ms unit
565-
gap_conn_params.slave_latency = 0;
566-
gap_conn_params.conn_sup_timeout = 4000 / 10; // in 10ms unit
567-
568-
sd_ble_gap_conn_param_update(this->_connectionHandle, &gap_conn_params);
561+
updateConnectionInterval(this->_minimumConnectionInterval, this->_maximumConnectionInterval);
569562
}
570563

571564
if (this->_numRemoteServices > 0) {
@@ -624,6 +617,9 @@ void nRF51822::poll() {
624617
Serial.print(bleEvt->evt.gap_evt.params.conn_param_update.conn_params.conn_sup_timeout, HEX);
625618
Serial.println();
626619
#endif
620+
if (this->_eventListener) {
621+
this->_eventListener->BLEDeviceConnectionParamsUpdated(*this);
622+
}
627623
break;
628624

629625
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
@@ -1035,6 +1031,19 @@ void nRF51822::end() {
10351031
this->_numRemoteCharacteristics = 0;
10361032
}
10371033

1034+
void nRF51822::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
1035+
setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
1036+
1037+
ble_gap_conn_params_t gap_conn_params;
1038+
1039+
gap_conn_params.min_conn_interval = this->_minimumConnectionInterval; // in 1.25ms units
1040+
gap_conn_params.max_conn_interval = this->_maximumConnectionInterval; // in 1.25ms unit
1041+
gap_conn_params.slave_latency = 0;
1042+
gap_conn_params.conn_sup_timeout = 4000 / 10; // in 10ms unit
1043+
1044+
sd_ble_gap_conn_param_update(this->_connectionHandle, &gap_conn_params);
1045+
}
1046+
10381047
bool nRF51822::updateCharacteristicValue(BLECharacteristic& characteristic) {
10391048
bool success = true;
10401049

src/nRF51822.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class nRF51822 : public BLEDevice
5252

5353
virtual ~nRF51822();
5454

55+
virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
56+
5557
virtual void begin(unsigned char advertisementDataSize,
5658
BLEEirData *advertisementData,
5759
unsigned char scanDataSize,

src/nRF8001.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,9 @@ void nRF8001::poll() {
10571057
Serial.print(F("Timing change received conn Interval: 0x"));
10581058
Serial.println(aciEvt->params.timing.conn_rf_interval, HEX);
10591059
#endif
1060+
if (this->_eventListener) {
1061+
this->_eventListener->BLEDeviceConnectionParamsUpdated(*this);
1062+
}
10601063
break;
10611064

10621065
case ACI_EVT_DISCONNECTED:
@@ -1208,6 +1211,12 @@ void nRF8001::end() {
12081211
this->_numRemotePipeInfo = 0;
12091212
}
12101213

1214+
void nRF8001::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
1215+
setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
1216+
1217+
lib_aci_change_timing(this->_minimumConnectionInterval, this->_maximumConnectionInterval, 0, 4000 / 10);
1218+
}
1219+
12111220
bool nRF8001::updateCharacteristicValue(BLECharacteristic& characteristic) {
12121221
bool success = true;
12131222

src/nRF8001.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class nRF8001 : protected BLEDevice
4646

4747
virtual ~nRF8001();
4848

49+
virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
50+
4951
virtual void begin(unsigned char advertisementDataSize,
5052
BLEEirData *advertisementData,
5153
unsigned char scanDataSize,

0 commit comments

Comments
 (0)