Skip to content

Commit c293192

Browse files
authored
Merge pull request #225 from paul-szczepanek-arm/master
fix using mixed APIs in Gattserver example
2 parents 5f4cead + 73bb488 commit c293192

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

BLE_GattServer/source/BLEProcess.h

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,22 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
128128
printf("Ble instance initialized\r\n");
129129

130130
Gap &gap = _ble_interface.gap();
131-
ble_error_t error = gap.setAdvertisingPayload(make_advertising_data());
132-
if (error) {
133-
printf("Error %u during gap.setAdvertisingPayload\r\n", error);
131+
gap.onConnection(this, &BLEProcess::when_connection);
132+
gap.onDisconnection(this, &BLEProcess::when_disconnection);
133+
134+
if (!set_advertising_parameters()) {
134135
return;
135136
}
136137

137-
gap.setAdvertisingParams(make_advertising_params());
138-
139-
gap.onConnection(this, &BLEProcess::when_connection);
140-
gap.onDisconnection(this, &BLEProcess::when_disconnection);
138+
if (!set_advertising_data()) {
139+
return;
140+
}
141141

142-
start_advertising();
142+
if (!start_advertising()) {
143+
return;
144+
}
143145

144-
if (_post_init_cb) {
146+
if (_post_init_cb) {
145147
_post_init_cb(_ble_interface, _event_queue);
146148
}
147149
}
@@ -157,45 +159,59 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
157159
start_advertising();
158160
}
159161

160-
void start_advertising(void)
162+
bool start_advertising(void)
161163
{
162-
ble_error_t error = _ble_interface.gap().startAdvertising();
164+
Gap &gap = _ble_interface.gap();
165+
166+
/* Start advertising the set */
167+
ble_error_t error = gap.startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
168+
163169
if (error) {
164170
printf("Error %u during gap.startAdvertising.\r\n", error);
165-
return;
171+
return false;
166172
} else {
167173
printf("Advertising started.\r\n");
174+
return true;
168175
}
169176
}
170177

171-
static GapAdvertisingData make_advertising_data(void)
178+
bool set_advertising_parameters()
172179
{
173-
static const uint8_t device_name[] = "GattServer";
174-
GapAdvertisingData advertising_data;
180+
Gap &gap = _ble_interface.gap();
175181

176-
// add advertising flags
177-
advertising_data.addFlags(
178-
GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
179-
GapAdvertisingData::BREDR_NOT_SUPPORTED
182+
ble_error_t error = gap.setAdvertisingParameters(
183+
ble::LEGACY_ADVERTISING_HANDLE,
184+
ble::AdvertisingParameters()
180185
);
181186

182-
// add device name
183-
advertising_data.addData(
184-
GapAdvertisingData::COMPLETE_LOCAL_NAME,
185-
device_name,
186-
sizeof(device_name)
187-
);
187+
if (error) {
188+
printf("Gap::setAdvertisingParameters() failed with error %d", error);
189+
return false;
190+
}
188191

189-
return advertising_data;
192+
return true;
190193
}
191194

192-
static GapAdvertisingParams make_advertising_params(void)
195+
bool set_advertising_data()
193196
{
194-
return GapAdvertisingParams(
195-
/* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
196-
/* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500),
197-
/* timeout */ 0
197+
Gap &gap = _ble_interface.gap();
198+
199+
/* Use the simple builder to construct the payload; it fails at runtime
200+
* if there is not enough space left in the buffer */
201+
ble_error_t error = gap.setAdvertisingPayload(
202+
ble::LEGACY_ADVERTISING_HANDLE,
203+
ble::AdvertisingDataSimpleBuilder<ble::LEGACY_ADVERTISING_MAX_SIZE>()
204+
.setFlags()
205+
.setName("GattServer")
206+
.getAdvertisingData()
198207
);
208+
209+
if (error) {
210+
printf("Gap::setAdvertisingPayload() failed with error %d", error);
211+
return false;
212+
}
213+
214+
return true;
199215
}
200216

201217
events::EventQueue &_event_queue;

0 commit comments

Comments
 (0)