didDiscoverPeripheral: not working sometimes on background
Asked Answered
I

1

2

Happens both on iOS 12 & iOS 13.

I have Bluetooth-central correctly added on my Info.plist

I'm specifying a list of service UUIDs

What I am seeing:

  • If I interact with our BLE device on the foreground, I can still scan for it on background, even hours and days later, as long as I don't turn off Bluetooth or restart the iOS device.
  • If I have never interacted with it while the app is open, or If I restart the device, or turn off Bluetooth on the device, then when scanning in the background, didDiscoverPeripheral: never gets called, until I open the app, which triggers didDiscoverPeripheral: instantly.
  • If I use retrievePeripheralsWithIdentifiers: and iOs can return the object "cached" then I can skip the scan and connect directly, which works perfectly. But sometimes retrievePeripheralsWithIdentifiers: doesn't return the object so I can't always relay on that (And forces the user to interact with our device on the foreground first)
Involucel answered 4/10, 2019 at 11:54 Comment(1)
Not exactly sure what the actual question is but if you already have a reference to the Peripheral when it disconnects, you don't need to scan again for it. Just tell your central manager to connect to it.Metaphor
I
2

So, after quite a bit of back and forth between me and our firmware team, we found the reason behind our problems.

Turns out, apps on background when scanning won't read the advertisement extended packet. The problem was, the service UUID we were using to filter the scan for our devices was only being sent on that advertisement extended packet.

For this reason, when the app was scanning a new device, it would not find anything with that service UUID, but once it had found it with the app open, it would cache it internally with its services, and from that point it would work even while on background (because it already knows what services that device has).

The fix was to include that service UUID on the first advertisement packet.

Involucel answered 1/11, 2019 at 18:5 Comment(1)
I have exact same issue as you. I just have one question, what do you mean by: "apps on background when scanning won't read the advertisement extended packet" ? What "extended packet" here means? is it advertisementData ?Bartlet

© 2022 - 2024 — McMap. All rights reserved.