Skip to content

Commit e943529

Browse files
committed
nRF52: Ensure that if enabled the HID service gets added to the main advertising packet (#2631)
Also improve docs to deprecate setServices(...advertise) and provide alternative
1 parent 845d1c1 commit e943529

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
nRF52840: Allow 1mbps,coded/coded,1mbps and up to double-length advertising in 'extended' advertising mode
4747
BLE: Ensure all Espruino devices advertise the Espruino 0x0590 manufacturer ID by default
4848
- The default Nordic UART scan response will be removed in future to reduce power usage
49+
nRF52: Ensure that if enabled the HID service gets added to the main advertising packet (#2631)
4950

5051
2v25 : ESP32C3: Get analogRead working correctly
5152
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)

libs/bluetooth/bluetooth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ typedef struct {
108108
#define BLUETOOTH_ADVERTISING_INTERVAL 375
109109
#endif
110110

111+
#define ADVERTISE_MAX_UUIDS 4 ///< maximum custom UUIDs to advertise
112+
111113
typedef enum {
112114
BLE_NONE = 0,
113115
BLE_IS_SENDING = 1, //< sending data with jswrap_ble_transmit_string?

libs/bluetooth/jswrap_bluetooth.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,21 @@ JsVar *_jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options, bool isForSet
859859
jsble_setup_advdata(&advdata);
860860
ble_advdata_manuf_data_t manuf_specific_data;
861861
memset(&manuf_specific_data, 0, sizeof(ble_advdata_manuf_data_t));
862+
ble_uuid_t adv_uuids[ADVERTISE_MAX_UUIDS];
863+
advdata.uuids_complete.uuid_cnt = 0;
864+
advdata.uuids_complete.p_uuids = &adv_uuids[0];
865+
862866
if (isForSetAdvertising) { // if for setAdvertising add manufacturerData as Espruino
863867
advdata.p_manuf_specific_data = &manuf_specific_data;
864868
advdata.p_manuf_specific_data->company_identifier = 0x0590;
869+
870+
if (bleStatus & BLE_HID_INITED) {
871+
advdata.uuids_complete.p_uuids[advdata.uuids_complete.uuid_cnt].uuid = BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE;
872+
advdata.uuids_complete.p_uuids[advdata.uuids_complete.uuid_cnt].type = BLE_UUID_TYPE_BLE;
873+
advdata.uuids_complete.uuid_cnt++;
874+
}
865875
}
876+
866877
#endif
867878

868879
if (jsvIsObject(options)) {
@@ -916,9 +927,7 @@ JsVar *_jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options, bool isForSet
916927
int maxServices = jsvGetChildren(data);
917928
ble_advdata_service_data_t *service_data = (ble_advdata_service_data_t*)alloca(maxServices*sizeof(ble_advdata_service_data_t));
918929
int service_data_cnt = 0;
919-
ble_uuid_t *adv_uuid = (ble_uuid_t*)alloca(maxServices*sizeof(ble_uuid_t));
920-
int adv_uuid_cnt = 0;
921-
if (maxServices && (!service_data || !adv_uuid))
930+
if (maxServices && !service_data)
922931
return 0; // allocation error
923932
#endif
924933
JsvObjectIterator it;
@@ -934,8 +943,10 @@ JsVar *_jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options, bool isForSet
934943
}
935944
#ifdef NRF5X
936945
if (jsvIsUndefined(v)) {
937-
adv_uuid[adv_uuid_cnt] = ble_uuid;
938-
adv_uuid_cnt++;
946+
if (advdata.uuids_complete.uuid_cnt < ADVERTISE_MAX_UUIDS)
947+
advdata.uuids_complete.p_uuids[advdata.uuids_complete.uuid_cnt++] = ble_uuid;
948+
else
949+
jsWarn("Too many UUIDs\n");
939950
} else {
940951
service_data[service_data_cnt].service_uuid = ble_uuid.uuid;
941952
service_data[service_data_cnt].data.size = dLen;
@@ -950,8 +961,6 @@ JsVar *_jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options, bool isForSet
950961
#ifdef NRF5X
951962
advdata.service_data_count = service_data_cnt;
952963
advdata.p_service_data_array = service_data;
953-
advdata.uuids_complete.uuid_cnt = adv_uuid_cnt;
954-
advdata.uuids_complete.p_uuids = adv_uuid;
955964
#endif
956965
} else if (!jsvIsUndefined(data)) {
957966
jsExceptionHere(JSET_TYPEERROR, "Expecting Object, Array or undefined, got %t", data);
@@ -1088,8 +1097,8 @@ NRF.setAdvertising([
10881097
interval: 600 // Advertising interval in msec, between 20 and 10000 (default is 375ms)
10891098
manufacturer: 0x0590 // This is the manufacturer ID. Set to `0/false` to disable manufacturer data (2v26+ advertises Espruino's 0x0590 by default)
10901099
manufacturerData: [...] // If sending manufacturer data, this is an array of data to send
1091-
phy: "1mbps/2mbps/coded/coded,1mbps/1mbps,coded" // (NRF52833/NRF52840 only) use the long-range coded phy for transmission (1mbps default)
1092-
extended : true // (NRF52833/NRF52840 only) force use of extended (>31 byte) advertising packets - usually only done if phy isn't set to "1mbps"
1100+
phy: "1mbps/2mbps/coded/coded,1mbps/1mbps,coded" // ((2v26+, NRF52833/NRF52840 only) use the long-range coded phy for transmission (1mbps default)
1101+
extended : true // (2v26+, NRF52833/NRF52840 only) force use of extended (>31 byte) advertising packets - usually only done if phy isn't set to "1mbps"
10931102
}
10941103
```
10951104
@@ -1324,7 +1333,6 @@ In addition, `options` can contain:
13241333
are left out (usually `[2,1,6]`). It can be very useful to do this
13251334
if you're using `NRF.getAdvertisingData(...)` to set a scan response packet:
13261335
1327-
13281336
```
13291337
NRF.setScanResponse(NRF.getAdvertisingData({
13301338
0x1809 : [Math.round(E.getTemperature())] // temperature service data in scan response
@@ -1358,8 +1366,19 @@ NRF.setScanResponse([0x07, // Length of Data
13581366
'S', 'a', 'm', 'p', 'l', 'e']);
13591367
```
13601368
1361-
**Note:** `NRF.setServices(..., {advertise:[ ... ]})` writes advertised services
1362-
into the scan response - so you can't use both `advertise` and `NRF.setServices`
1369+
Or you can use `NRF.getAdvertisingData` to correctly format the advertising data
1370+
for you. For example to advertise the HRM service and temperature in the Scan
1371+
Response you can do:
1372+
1373+
```
1374+
NRF.setScanResponse(NRF.getAdvertisingData({
1375+
0x180D: undefined, // HRM service
1376+
0x1809: [Math.round(E.getTemperature())] // temperature
1377+
},{ flags:false, showName:false }))
1378+
```
1379+
1380+
**Note:** The deprecated `NRF.setServices(..., {advertise:[ ... ]})` writes advertised services
1381+
into the scan response - so you can't use both `NRF.setScanResponse` and `NRF.setServices(..., {advertise:[...]})`
13631382
or one will overwrite the other.
13641383
*/
13651384
void jswrap_ble_setScanResponse(JsVar *data) {
@@ -1483,7 +1502,8 @@ NRF.setServices({
14831502
NRF.setServices(undefined, {
14841503
hid : new Uint8Array(...), // optional, default is undefined. Enable BLE HID support
14851504
uart : true, // optional, default is true. Enable BLE UART support
1486-
advertise: [ '180D' ] // optional, list of service UUIDs to advertise
1505+
advertise: [ '180D' ] // optional, list of service UUIDs to advertise in the scan response
1506+
// (deprecated - use `NRF.setScanResponse(NRF.getAdvertisingData({'180D':undefined},{flags:false, showName:false}))`)
14871507
ancs : true, // optional, Bangle.js-only, enable Apple ANCS support for notifications (see `NRF.ancs*`)
14881508
ams : true // optional, Bangle.js-only, enable Apple AMS support for media control (see `NRF.ams*`)
14891509
cts : true // optional, Bangle.js-only, enable Apple Current Time Service support (see `NRF.ctsGetTime`)

targets/nrf5x/bluetooth.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ __ALIGN(4) static ble_gap_lesc_dhkey_t m_lesc_dhkey; /**< LESC ECC DH Key*/
154154
// -----------------------------------------------------------------------------------
155155
// -----------------------------------------------------------------------------------
156156

157-
#define ADVERTISE_MAX_UUIDS 4 ///< maximum custom UUIDs to advertise
158-
159157
#if NRF_SD_BLE_API_VERSION < 5
160158
static ble_nus_t m_nus; /**< Structure to identify the Nordic UART Service. */
161159
#elif NRF_SD_BLE_API_VERSION < 6

0 commit comments

Comments
 (0)