Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not receiving response from discoverServices #66

Open
andr3a88-amk opened this issue Dec 10, 2024 · 5 comments
Open

Not receiving response from discoverServices #66

andr3a88-amk opened this issue Dec 10, 2024 · 5 comments

Comments

@andr3a88-amk
Copy link

andr3a88-amk commented Dec 10, 2024

Hi, I am experiencing an issue with my connection flow when using the library. Occasionally, the code gets stuck after calling discoverServices, and the process ends with a peripheral disconnection error (the peripheral has a timeout of 5 minutes)

Below is the relevant part of my code:

if peripheral.state != .connected {
    logger.debug("📶 Peripheral not connected. Connecting...", subsystem: Self.tag)
    try await centralManager.connect(peripheral, options: options)
}
logger.debug("📶 Connected to peripheral \(peripheral.cbPeripheral.description)", subsystem: Self.tag)

try await peripheral.discoverServices(sensorServiceIds.map { $0.cbUUID })
logger.debug("💡 Discover services for peripheral \(peripheral.identifier)", subsystem: Self.tag)

The logs indicate that discoverServices does not return the expected services. The connection seems successfully initiated (state = connected), but the discoverServices call appears to hang and does not return the services. This mainly occurs when peripheral.state is connected

17:14:09 📶 Connected to peripheral <CBPeripheral: 0x300d880d0, identifier = 24CF3D10-C283-018D-FB02-5BC4B0A024DE, name = Respiro, mtu = 247, state = connected>
17:19:42 didDisconnectPeripheral <CBPeripheral: 0x300d880d0, identifier = 24CF3D10-C283-018D-FB02-5BC4B0A024DE, name = Respiro, mtu = 23, state = connecting> | isReconnecting false | The connection has timed out unexpectedly

Could this be a bug in the library, or is there something I might be missing in the connection flow?
Are there any known issues with discoverServices in similar scenarios?

@manolofdez
Copy link
Owner

Hi,
I am not aware of any issues with discoverServices. If you set a breakpoint in PeripheralDelegate's line 34, does it get hit?

@andr3a88-amk
Copy link
Author

Hi @manolofdez, sorry for the delay. At the moment i can't figure out the issue.

The only time when it happens is after a peripheral restore or after a consequent peripheral disconnection (caused by The connection has timed out unexpectedly) and reconnection. Then if I use the peripheral restored/reconnected sometimes i got Service not found and sometimes no responses from the peripheral.

@nonameplum
Copy link

@andr3a88-amk could it be connected to this issue https://forums.developer.apple.com/forums/thread/742497 ?

@andr3a88-amk
Copy link
Author

@nonameplum yes is possible, but from my logs I cannot see a peripheral with same UUID but different memory address.

When a peripheral is restored i use the method retrievePeripherals, to get a fresh instance. but right after the restoration i have a disconnection:

let peripheralsRetrieved = centralManager.retrievePeripherals(withIdentifiers: [cbPeripheralRestored.identifier])

08:37:38  Restored peripheral received: <CBPeripheral: 0x302fb6970, identifier = B19706B8-D5DE-BE36-560C-E909590D20E9, name = "", mtu = 247, state = connected>

08:37:39 didDisconnectPeripheral <CBPeripheral: 0x302fb6970, identifier = B19706B8-D5DE-BE36-560C-E909590D20E9, name = "" , mtu = 23, state = connecting> | isReconnecting false | The connection has timed out unexpectedly.

@andr3a88-amk
Copy link
Author

Hi @manolofdez, I have a question about the behavior of connect method.

Does this method return immediately after initiating the connection request (after the call), or does it stay in an await state until the connection is fully established?

try await centralManager.connect(peripheral, options: nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants