h2zero / esp-nimble-cpp

C++ library for the esp32 NimBLE stack based on and mostly compatible with @nkolban cpp_utils BLE library.
https://h2zero.github.io/esp-nimble-cpp/
Apache License 2.0
181 stars 62 forks source link

Connect while scanning #238

Closed thekurtovic closed 3 days ago

thekurtovic commented 4 days ago

There is a relatively new option called CONFIG_BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN. I was curious to test it out but so far it's not working as I would expect. It appears to be backported to 5.3.1. https://github.com/espressif/esp-idf/commit/92e4100df197e8991dcff99223034f571055bd9c https://github.com/espressif/esp-nimble/commit/50c2641a04e356fdac9c53fae56f5004f26605e0

It works partially, as calling NimBLEClient::connect does not implicitly stop the scan like it would previously. However once the esp32-s3 connects to the peripheral it will not receive any new advertisements, even though the scan seemingly never ended. I tried to disable scan duplicate filtering just in case that was conflicting, but it made no difference. The peripheral starts advertising every 15 seconds, but it won't show up until the scan duration is reached and restarted. Also some issues with stopping/starting the scan early, seems like I have to wait until the scan reaches the specified duration before it can be restarted.

Should this work out of the box once the option is enabled? I have a feeling I should report this upstream but wanted to double check here.

esp32-s3 output
I [ 10769][main.cpp:254] BLE adv (-59 db, phy 1 0) Name: , Address: e3:e4:5b:75:ad:36
D (10791) NimBLEClient: >> connect(e3:e4:5b:75:ad:36)
D (10792) NimBLEScan: erase device: e3:e4:5b:75:ad:36
D (12376) NimBLEClient: Got Client event BLE_GAP_EVENT_CONNECT
I [ 12378][main.cpp:162] Connected to e3e45b75ad36, RSSI 0 (bond 0)
D (12392) NimBLEClient: Got Client event BLE_GAP_EVENT_MTU
I (12392) NimBLEClient: mtu update: mtu=247
D [ 12402][main.cpp:238] On MTU change -> 247, e3e45b75ad36
D (12402) NimBLEClient: exchangeMTUCb: status=0, mtu=247
D (12473) NimBLEClient: Got Client event BLE_GAP_EVENT_CONN_UPDATE
I (12473) NimBLEClient: Connection parameters updated.
D (12483) NimBLEClient: Got Client event BLE_GAP_EVENT_LINK_ESTAB
D (12511) NimBLEClient: Got Client event BLE_GAP_EVENT_DATA_LEN_CHG
D (12974) NimBLEClient: Got Client event BLE_GAP_EVENT_PASSKEY_ACTION
D (12974) NimBLEClient: Enter the passkey
D [ 12984][main.cpp:245] Client e3e45b75ad36 requesting passkey...
D [ 12984][main.cpp:54] Generated sensor key
D (12996) NimBLEDevice: BLE_SM_IOACT_INPUT; ble_sm_inject_io result: 0
D (13734) NimBLEUtils: Unknown event type 27 0x1b
D (13734) NimBLEClient: Got Client event Unknown event type
D (13801) NimBLEClient: Got Client event BLE_GAP_EVENT_ENC_CHANGE
D [ 13802][main.cpp:220] Auth event e3e45b75ad36 | 1 1 1 16
D [ 13812][main.cpp:223] Secured connection
D (13827) NimBLEClient: >> getService: uuid: 3a060001-c5ae-404b-acae-fd594f568158
D (13839) NimBLEClient: Service Discovered >> status: 0 handle: 14
D (13854) NimBLEClient: Service Discovered >> status: 14 handle: -1
D (13854) NimBLEClient: << Service Discovered
D (13856) NimBLERemoteService: >> getCharacteristic: uuid: 3a060002-c5ae-404b-acae-fd594f568158
D (13865) NimBLERemoteService: >> retrieveCharacteristics()
D (13914) NimBLERemoteService: Characteristic Discovery >>
D (13937) NimBLERemoteService: Characteristic Discovery >>
D (13937) NimBLERemoteService: << Characteristic Discovery
D (13937) NimBLERemoteService: << retrieveCharacteristics()
D (13949) NimBLERemoteService: << Characteristic found
D (13961) NimBLERemoteService: >> getCharacteristic: uuid: 3a060003-c5ae-404b-acae-fd594f568158
D (13961) NimBLERemoteService: >> retrieveCharacteristics()
D (13989) NimBLERemoteService: Characteristic Discovery >>
D (14004) NimBLERemoteService: Characteristic Discovery >>
D (14004) NimBLERemoteService: << Characteristic Discovery
D (14004) NimBLERemoteService: << retrieveCharacteristics()
D (14015) NimBLERemoteService: << Characteristic found
D (14026) NimBLERemoteCharacteristic: >> setNotify()
D (14026) NimBLERemoteCharacteristic: >> getDescriptor: uuid: 0x2902
D (14037) NimBLERemoteCharacteristic: >> retrieveDescriptors() for characteristic: 3a060003-c5ae-404b-acae-fd594f568158
D (14064) NimBLERemoteCharacteristic: Descriptor Discovery >> status: 0 handle: 19
D (14064) NimBLERemoteCharacteristic: << Descriptor Discovery
D (14065) NimBLERemoteCharacteristic: << retrieveDescriptors(): found 1 descriptors.
D (14086) NimBLERemoteCharacteristic: << getDescriptor: found
D (14086) NimBLERemoteCharacteristic: << setNotify()
D (14096) NimBLERemoteValueAttribute: >> writeValue()
I (14109) NimBLERemoteValueAttribute: Write complete; status=0
D (14111) NimBLERemoteValueAttribute: << writeValue
D (14122) NimBLERemoteValueAttribute: >> writeValue()
I (14146) NimBLERemoteValueAttribute: Write complete; status=0
D (14147) NimBLERemoteValueAttribute: << writeValue
D (14147) NimBLEClient: Got Client event BLE_GAP_EVENT_NOTIFY_RX
D (14157) NimBLEClient: Notify Received for handle: 18
D (14167) NimBLEClient: checking service 3a060001-c5ae-404b-acae-fd594f568158 for handle: 18
D (14178) NimBLEClient: Got Notification for characteristic Characteristic: uuid: 3a060003-c5ae-404b-acae-fd594f568158, handle: 18 0x0012, props:  0x10
Descriptor: uuid: 0x2902, handle: 19
I [ 14189][main.cpp:419] Notif from e3e45b75ad36, 36 B
D [ 14209][main.cpp:420] data
 F4 D9 1A AD 00 00 1C 00 00 09 E9 83 01 09 19 00 03 09 00 00 07 07 B9 04 03 08 04 00 00 10 FF 00 ................................
 01 10 5F 00                                                                                     .._.

D (14251) NimBLEClient: Got Client event BLE_GAP_EVENT_DISCONNECT
D (14261) NimBLEClient: disconnect; reason=531, Remote User Terminated Connection
I [ 14262][main.cpp:184] BLE e3e45b75ad36 disconnected (531)
I [ 14272][main.cpp:54] Delete bond for e3e45b75ad36
D [ 14354][main.cpp:190] Try restarting scan...
D (14354) NimBLEScan: >> stop()
D (14354) NimBLEScan: << stop()
D (14365) NimBLEScan: >> start: duration=60000
E (14365) NimBLEScan: Error initiating GAP discovery procedure; rc=524, Command Disallowed
D (14375) NimBLEScan: << start()
D (14376) NimBLERemoteCharacteristic: >> deleteDescriptors
D (14386) NimBLERemoteCharacteristic: << deleteDescriptors
D (14396) NimBLERemoteCharacteristic: >> deleteDescriptors
D (14406) NimBLERemoteCharacteristic: << deleteDescriptors
h2zero commented 4 days ago

I saw this as well, they seem to be doing a lot of things lately that are not supported in other stacks or mynewt/nimble,like the connection re-attempt etc.. Not sure how well this one will work, I'd have to test it.

thekurtovic commented 3 days ago

I see. Can't find any other references to MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN now that you mention it. That is unfortunate, I was really looking forward to implementing this for my application. Let me know if you do 👍