I have two methods, each which adds a service to the peripheral manager. When I get the didAddService
callback I use the [manager startAdvertisingServices:]
method with the services that have been added (so the array used as the value for the CBAdvertisementDataServiceUUIDsKey
key has either 1 or 2 service UUIDs in it.)
When I add the second service and try to advertise them both, I always get the error Advertising has already started.
I've tried to use [manager stopAdvertising]
and then adding the new services, but the stopAdvertising method is asynchronous, so sometimes it still fails (it hasn't stopped advertising by the time I call startAdvertisingServices
again.)
I have no problem advertising/reading/writing them individually, and if I wait after calling stopAdvertising I can start advertising them together, but that seems like a hack.
The way I can think of to do this safely is to KVO on the isAdvertising
property of the peripheral manager and hold flags for whether or not each service should be advertising, so that I know exactly when the advertising stops and I can safely start again. I'm also not sure if this will have an affect on Centrals in the field if I start and stop advertising just to add new services.
What's the proper way to advertise multiple services using the peripheral manager?