Added active option to NRF.setScan/findDevices/requestDevices to allow scan response packets to be requested

This commit is contained in:
Gordon Williams 2019-02-11 09:35:54 +00:00
parent f34b4ad264
commit e56d91b044
6 changed files with 29 additions and 17 deletions

View File

@ -14,6 +14,7 @@
nRF52: Disconnect RX pin after UART test at boot (saves power when in deep sleep)
Serial.unsetup now sets pin state to STATE_UNDEFINED, which disconnects the pins internally
Removed modulo on `new Date` h/m/s/ms arguments as it seems desktop JS is fine with out of range values
Added `active` option to NRF.setScan/findDevices/requestDevices to allow scan response packets to be requested
2v01 : ESP32: update to esp-idf V3.1
Fix issues with Class Extends

View File

@ -180,7 +180,7 @@ bool jsble_check_error(uint32_t err_code);
uint32_t jsble_set_periph_connection_interval(JsVarFloat min, JsVarFloat max);
/// Scanning for advertising packets
uint32_t jsble_set_scanning(bool enabled);
uint32_t jsble_set_scanning(bool enabled, bool activeScan);
/// returning RSSI values for current connection
uint32_t jsble_set_rssi_scan(bool enabled);

View File

@ -179,7 +179,7 @@ void jswrap_ble_reconfigure_softdevice() {
// restart various
JsVar *v,*o;
v = jsvObjectGetChild(execInfo.root, BLE_SCAN_EVENT,0);
if (v) jsble_set_scanning(true);
if (v) jsble_set_scanning(true, false);
jsvUnLock(v);
v = jsvObjectGetChild(execInfo.root, BLE_RSSI_EVENT,0);
if (v) jsble_set_rssi_scan(true);
@ -223,7 +223,7 @@ void jswrap_ble_kill() {
if (bleTaskInfo) jsvUnLock(bleTaskInfo);
bleTaskInfo = 0;
// if we were scanning, make sure we stop
jsble_set_scanning(false);
jsble_set_scanning(false, false);
jsble_set_rssi_scan(false);
#if CENTRAL_LINK_COUNT>0
@ -1527,6 +1527,10 @@ NRF.setScan(function(d) {
}, { filters: [{ manufacturerData:{0x0590:{}} }] });
```
You can also specify `active:true` in the second argument to perform
active scanning (this requests scan response packets) from any
devices it finds.
**Note:** BLE advertising packets can arrive quickly - faster than you'll
be able to print them to the console. It's best only to print a few, or
to use a function like `NRF.findDevices(..)` which will collate a list
@ -1605,7 +1609,9 @@ void jswrap_ble_setScan_cb(JsVar *callback, JsVar *filters, JsVar *adv) {
void jswrap_ble_setScan(JsVar *callback, JsVar *options) {
JsVar *filters = 0;
bool activeScan = false;
if (jsvIsObject(options)) {
activeScan = jsvGetBoolAndUnLock(jsvObjectGetChild(options, "active", 0));
filters = jsvObjectGetChild(options, "filters", 0);
if (filters && !jsvIsArray(filters)) {
jsvUnLock(filters);
@ -1628,7 +1634,7 @@ void jswrap_ble_setScan(JsVar *callback, JsVar *options) {
jsvObjectRemoveChild(execInfo.root, BLE_SCAN_EVENT);
}
// either start or stop scanning
uint32_t err_code = jsble_set_scanning(callback != 0);
uint32_t err_code = jsble_set_scanning(callback != 0, activeScan);
jsble_check_error(err_code);
jsvUnLock(filters);
}
@ -1641,7 +1647,7 @@ void jswrap_ble_setScan(JsVar *callback, JsVar *options) {
"generate" : "jswrap_ble_findDevices",
"params" : [
["callback","JsVar","The callback to call with received advertising packets, or undefined to stop"],
["options","JsVar","A time in milliseconds to scan for (defaults to 2000), Or an optional object `{filters: ..., timeout : ...}` (as would be passed to `NRF.requestDevice`) to filter devices by"]
["options","JsVar","A time in milliseconds to scan for (defaults to 2000), Or an optional object `{filters: ..., timeout : ..., active: bool}` (as would be passed to `NRF.requestDevice`) to filter devices by"]
]
}
Utility function to return a list of BLE devices detected in range. Behind the scenes,
@ -2204,6 +2210,16 @@ void jswrap_ble_sendHIDReport(JsVar *data, JsVar *callback) {
Search for available devices matching the given filters. Since we have no UI here,
Espruino will pick the FIRST device it finds, or it'll call `catch`.
`options` can have the following fields:
* `filters` - a list of filters that a device must match before it is returned (see below)
* `timeout` - the maximum time to scan for in milliseconds (scanning stops when a match
is found. eg. `NRF.requestDevice({ timeout:2000, filters: [ ... ] })`
* `active` - whether to perform active scanning (requesting 'scan response' packets from any
devices that are found). eg. `NRF.requestDevice({ active:true, filters: [ ... ] })`
**NOTE:** `timeout` and `active` are not part of the Web Bluetooth standard.
The following filter types are implemented:
* `services` - list of services as strings (all of which must match). 128 bit services must be in the form '01230123-0123-0123-0123-012301230123'
@ -2221,12 +2237,6 @@ NRF.requestDevice({ filters: [{ services: ['1823'] }] }).then(function(device) {
NRF.requestDevice({ filters: [{ manufacturerData:{0x0590:{}} }] }).then(function(device) { ... });
```
You can also specify a timeout to wait for devices in milliseconds. The default is 2 seconds (2000):
```
NRF.requestDevice({ timeout:2000, filters: [ ... ] })
```
As a full example, to send data to another Puck.js to turn an LED on:
```
@ -2258,7 +2268,7 @@ NRF.requestDevice({ filters: [{ namePrefix: 'Puck.js' }]}).then(
() => { gatt.disconnect(); console.log("Done!"); } );
```
Note that you'll have to keep track of the `gatt` variable so that you can
Note that you have to keep track of the `gatt` variable so that you can
disconnect the Bluetooth connection when you're done.
*/
#if CENTRAL_LINK_COUNT>0

View File

@ -440,13 +440,13 @@ static bool pixl_selfTest() {
jsiConsolePrintf("Have events - no BLE test\n");
} else {
uint32_t err_code;
err_code = jsble_set_scanning(true);
err_code = jsble_set_scanning(true, false);
jsble_check_error(err_code);
int timeout = 20;
while (timeout-- && !jshHasEvents()) {
nrf_delay_ms(100);
}
err_code = jsble_set_scanning(false);
err_code = jsble_set_scanning(false, false);
jsble_check_error(err_code);
if (!jshHasEvents()) {
jsiConsolePrintf("No BLE adverts found in 2s\n");

View File

@ -129,7 +129,8 @@ bool jsble_check_error(uint32_t err_code){
return false;
}
/// Scanning for advertisign packets
uint32_t jsble_set_scanning(bool enabled){
uint32_t jsble_set_scanning(bool enabled, bool activeScan){
if (activeScan) jsWarn("active scan not implemented\n");
bluetooth_gap_setScan(enabled);
return 0;
}

View File

@ -2359,7 +2359,7 @@ void jsble_restart_softdevice() {
jswrap_ble_reconfigure_softdevice();
}
uint32_t jsble_set_scanning(bool enabled) {
uint32_t jsble_set_scanning(bool enabled, bool activeScan) {
uint32_t err_code = 0;
if (enabled) {
if (bleStatus & BLE_IS_SCANNING) return 0;
@ -2373,7 +2373,7 @@ uint32_t jsble_set_scanning(bool enabled) {
};
#endif
// non-selective scan
m_scan_param.active = 0; // Active scanning set.
m_scan_param.active = activeScan; // Active scanning set.
m_scan_param.interval = SCAN_INTERVAL;// Scan interval.
m_scan_param.window = SCAN_WINDOW; // Scan window.
m_scan_param.timeout = 0x0000; // No timeout.