Need faster scans in BLE central mode (BLEScanner sketch)


#22

For grins, try taking out all the Serial.print statements except the ones that print the miliis() elapsed time.


#23

Yes, I agree on the short times.


#24

Sigh…I’m thinking about trying a different module. Maybe something by Adafruit. They seem to have strong documentation and support. But so far, I am not sure if their BLE modules will work in Central mode.

Also looking into some of those cheap cheap modules from China on AliExpress or eBay. They use the same Nordic chip, nRF51822, but I am also not sure if they can run in Central mode. Everything out there seems focused on having iPhone in Central mode and module as peripheral. Unfortunately, my project is a standalone one that needs module as Central to collect ads from iBeacon peripheral.


#25

This looks interesting. They have some details about “ble.startScanning();” that I might try. So instead of starting scanning in void setup, maybe I will call it in a loop, i.e. ble.startScanning();…get scan results…ble.stopScanning();…ble.startScanning();…get scan results…ble.stopScanning();…

Could be a worthless amateur hack, but you never know until you try, right?! :wink:

Thanks for your input!!!


#26

Don’t give up on Duo yet. RedBear support has been awesome for me, so far. I think they are on holiday now aren’t they?

Try checking out the Particle forum, too, if you have not already do so. Somewhere on there, I read about Serial.print causing problems with the wifi/ble chipset. Sorry I do not have link handy.

Test results setting Serial.print = 115200.

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: C3 AE D5 9E F2 E
The rssi: -61The ADV data: 2 1 6 3 3 ED FE 5 9 54 69 6C 65 Milliseconds elapsed since last scan result: 2986

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: C3 AE D5 9E F2 E
The rssi: -61The ADV data: Milliseconds elapsed since last scan result: 2

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -72The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 588

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 4 2E BF 8F 9
The rssi: -66The ADV data: 1B FF 75 0 42 4 1 80 20 F8 4 2E BF 8F 9 FA 4 2E BF 8F 8 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 306

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -64The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 294

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -64The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 4

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 4 2E BF 8F 9
The rssi: -65The ADV data: 1B FF 75 0 42 4 1 80 20 F8 4 2E BF 8F 9 FA 4 2E BF 8F 8 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 8

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 4 2E BF 8F 9
The rssi: -70The ADV data: 1B FF 75 0 42 4 1 80 20 F8 4 2E BF 8F 9 FA 4 2E BF 8F 8 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 306

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 4 2E BF 8F 9
The rssi: -66The ADV data: 1B FF 75 0 42 4 1 80 20 F8 4 2E BF 8F 9 FA 4 2E BF 8F 8 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 304

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -61The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 886

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -61The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 77 B8 8C 2D 7
The rssi: -87The ADV data: 1B FF 75 0 42 4 1 80 60 F8 77 B8 8C 2D 7 FA 77 B8 8C 2D 6 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 600

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -67The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 2

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -68The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: C3 AE D5 9E F2 E
The rssi: -57The ADV data: 2 1 6 3 3 ED FE 5 9 54 69 6C 65 Milliseconds elapsed since last scan result: 600

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: C3 AE D5 9E F2 E
The rssi: -57The ADV data: Milliseconds elapsed since last scan result: 4

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D7 94 72 DE 13 66
The rssi: -64The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 288

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: D7 94 72 DE 13 66
The rssi: -65The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 3
The peerAddrType: 0
The peerAddr: F8 77 B8 8C 2D 7
The rssi: -87The ADV data: 1B FF 75 0 42 4 1 80 60 F8 77 B8 8C 2D 7 FA 77 B8 8C 2D 6 1 0 0 0 0 0 0 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -62The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 592

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -64The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -67The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 598

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -69The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 4

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: FF 34 C4 CC DC 4C
The rssi: -62The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 3872

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: FF 34 C4 CC DC 4C
The rssi: -63The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -61The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 594

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -61The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -67The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 594

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: F2 6D EF 6E 4F E
The rssi: -68The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 4

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: FF 34 C4 CC DC 4C
The rssi: -68The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 906

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: FF 34 C4 CC DC 4C
The rssi: -69The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 6

reportCallback:
The advEventType: 0
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -62The ADV data: 2 1 6 5 2 D 18 A 18 Milliseconds elapsed since last scan result: 594

reportCallback:
The advEventType: 4
The peerAddrType: 1
The peerAddr: D2 B2 E1 48 67 2D
The rssi: -63The ADV data: 10 9 4D 49 4F 20 47 4C 4F 42 41 4C 2D 46 55 53 45 Milliseconds elapsed since last scan result: 4


#27

start/stop - yes, I have to use it to connect. Sounds like a good next step. Good luck. Please keep posting results. :slight_smile:


#28

Hey Bill, do you have:

SYSTEM_MODE(MANUAL);

set to MANUAL like I do? Or set to something else?


#29

Didn’t work. :weary:


#30

Manual, like you do. Sorry to hear it did not work for you. Did you try the high baud rate on Serial and reducing the number of print statements?


#31

Hi there,

Sorry for the delay respond because of Chinese traditional holiday.

This is not the setting of 1 second for scan interval, it is 3.52s = 0x1600 (5632) * 0.625. If you want to set the scan interval to be 1s, then the value should be 0x640 in hexadecimal.

I suggest you that set the scan parameter to be:

ble.setScanParams(0x00, 0x0650, 0x0650);

Hope this can print your iBeaon per second.

Regards,
Guohui


#32

Tried that but no improvement.


#33

Tried your suggestion and used 0x00, 0x0650, 0x0650 but no improvement. Nearly the exact same behavior: around 3 seconds sometimes, around 6.9 seconds sometimes, then 20-30 seconds sometimes.

BLE scan start.
The rssi: -76
Milliseconds elapsed since last scan result: 9016
The rssi: -72
Milliseconds elapsed since last scan result: 6920
The rssi: -73
Milliseconds elapsed since last scan result: 3000
The rssi: -70
Milliseconds elapsed since last scan result: 22782
The rssi: -71
Milliseconds elapsed since last scan result: 2998
The rssi: -71
Milliseconds elapsed since last scan result: 6910
The rssi: -71
Milliseconds elapsed since last scan result: 3002
The rssi: -72
Milliseconds elapsed since last scan result: 6906
The rssi: -71
Milliseconds elapsed since last scan result: 3000
The rssi: -74
Milliseconds elapsed since last scan result: 22786
The rssi: -73
Milliseconds elapsed since last scan result: 2998
The rssi: -72
Milliseconds elapsed since last scan result: 6908
The rssi: -71
Milliseconds elapsed since last scan result: 3008
The rssi: -70
Milliseconds elapsed since last scan result: 32690
The rssi: -73
Milliseconds elapsed since last scan result: 3002
The rssi: -74
Milliseconds elapsed since last scan result: 9912
The rssi: -70
Milliseconds elapsed since last scan result: 22782
The rssi: -71
Milliseconds elapsed since last scan result: 2996
The rssi: -71
Milliseconds elapsed since last scan result: 6916
The rssi: -72
Milliseconds elapsed since last scan result: 25786
The rssi: -71
Milliseconds elapsed since last scan result: 2998
The rssi: -72
Milliseconds elapsed since last scan result: 6916
The rssi: -70
Milliseconds elapsed since last scan result: 2996
The rssi: -71
Milliseconds elapsed since last scan result: 6908
The rssi: -71
Milliseconds elapsed since last scan result: 2994
The rssi: -73
Milliseconds elapsed since last scan result: 6906
The rssi: -70
Milliseconds elapsed since last scan result: 2996
The rssi: -73
Milliseconds elapsed since last scan result: 6908
The rssi: -72
Milliseconds elapsed since last scan result: 28790
The rssi: -70
Milliseconds elapsed since last scan result: 6916
The rssi: -72
Milliseconds elapsed since last scan result: 2996

Could it be that my duo is defective?


#35

There may be timing issue on the BLE. Since when I was trying to reproduce your situation, I wrote a simple sketch to make the Duo broadcast as an beacon, but the advertising interval is far away from what I set. :thinking:

We will look into this, but it might take a long time to figure it out.


#36

Hi @guohui any update on this?


#37

We are managing to update the system firmware. We will try our best to figure it out.


#38

Is there an estimation on when will this update be released?


#39

Is there any hope of an update to address this timing issue?


#40

I think discovered what is behind this issue and how to fix. Yes it is timing, but I can elaborate further. It seems the HCI commands to the Bluetooth radio are getting executed out of order and I can see this by turning on the packet logger:

ble.enablePacketLogger();

The initialization of the of the stack had to be tweaked. Here is what I did. The actual timings I used are not exact, but the idea is to add some time for the stack to initialize so the scan parameters can be set and can start scanning. I might look into trapping the HCI command responses to ensure they complete before another command is sent.

     Timer scanStartTimer(3000, startScan, true);
     Timer scanParamTimer(3000, setScanParams, true);

     void startScan() {
         Serial.print("Start Scanning");
         ble.startScanning();
     }

     void setScanParams() {
        Serial.print("Set Scan Params");
        ble.setScanParams(0x01, 0x0010, 0x0010);
        scanStartTimer.start();
    }

    void setup() {
       ble.init();
       ble.onScanReportCallback(scanCallback);
       scanParamTimer.start();
    }

This also resolved the issue with not getting Scan Response packets when in active scanning mode.


DUO 0.3.1 Missing Scan Response Packets
#41

The timing issue could be related to a problem I found when playing with the RedBear Duo. The underlying BTstack APIs are used in a way that often makes them silently fail. I opened a ticket with the details here.