arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
303 stars 200 forks source link

Problem with BLEDevice.discoverAttributes in 1.2.0 version #175

Open akumar36 opened 3 years ago

akumar36 commented 3 years ago

I'm working on Arduino Nano 33 IoT and trying to discover attributes of nearby BLE devices. With BLE library version 1.1.3, everything works fine. But, with version 1.2.0, the Arduino device goes to a bad state when I execute discover attributes for some BLE peripherals(Mi band 4 in this case).

I'm attaching my sketch which scans nearby devices and discovers their attributes. BLE_Explorer.zip

Update to reproduction steps: The issue can also be reproduced by using an Android phone acting as a BLE peripheral using NRF connect app. Just configure the GATT server with "Sample Configuration" and try to discover attributes.

baconcheese113 commented 3 years ago

Running into the same issue, have you found a workaround yet?

My logs from nRF Connect:

22:10:39>Connecting to <MAC_ADDRESS>
22:10:39>gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, preferred PHY = LE 1M)
22:10:39>[Callback] Connection state changed with status: 0 and new state: CONNECTED (2)
22:10:39>Connected to <MAC_ADDRESS>
22:10:39>Discovering services...
22:10:39>gatt.discoverServices()
22:10:39>[Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED
22:10:40>Connection parameters updated (interval: 7.5ms, latency: 0, timeout: 5000ms)
22:10:54>[Callback] Connection state changed with status: 22 and new state: DISCONNECTED (0)
22:10:54>Error 22 (0x16): GATT CONN TERMINATE LOCAL HOST
22:10:54>Disconnected
22:10:54>[Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED

It's also worth mentioning that when it stops advertising, both the OnConnect and OnDisconnect handlers are called

baconcheese113 commented 3 years ago

I isolated my issue down to BLE.advertise() not also allowing service discovery by peripherals

Not sure why, but calling BLE.available() every loop cycle allowed services to be discovered

akumar36 commented 3 years ago

The issue seems to be due to this submission: https://github.com/arduino-libraries/ArduinoBLE/commit/3074107

Reverting the above change back resolves the issue.

akumar36 commented 3 years ago

I isolated my issue down to BLE.advertise() not also allowing service discovery by peripherals

Not sure why, but calling BLE.available() every loop cycle allowed services to be discovered

calling BLE.available() function did not help.

ElreboCM commented 3 years ago

I have the same issue on Arduino BLE Nano 33 with a Cycling Power Meter.

For Reference I have attached debug dumps for hcidump-bsp1.3.1-library1.1.3.txt hcidump-bsp2.4.1-library1.2.1.txt

@polldo: Can you look into this?

ElreboCM commented 3 years ago

I can confirm that reverting the change of this commit resolves the issue.

TheMadMoose commented 1 year ago

Small update just in case someone else was experiencing this issue as of March 2023: Reverting the change of the aforementioned commit resolved it for me as well (Hardware: Portenta H7 connecting over BLE to two Nicla Sense ME boards).

Elaboration on our setup: Both Nicla boards have a unique BLE device name, but identical service and characteristic UUIDs. We cycle through each peripheral (nicla) on the Central device. The first Nicla was discovered as expected, however the second one kept failing the characteristic discovery step.

Changing "J+1" to "J" seemed to immediately resolve this.

If I broke any rules by commenting / updating - My sincere apologies :)

ForrestThump commented 6 months ago

@TheMadMoose Can you elaborate on where I should change J+1 to J? My peripheral discoverAttributes() call always returns false, and I'm at a loss. I'm using two esp32s...