Expose stop service advertisment on Windows by johnb8 · Pull Request #281 · tinygo-org/bluetooth

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait - so normally after you call adapter.AddService the service is also included in the advertisement packet?
That's not how BLE normally works. The advertisement packet and the list of services that are actually available after connection may overlap, but this is normally the responsibility of the application developer to explicitly include.

Apparently Windows works differently. This is quite annoying.
To keep the API consistent (so the user doesn't need to know too much about platform differences), I would recommend the following:

  • Keep track of which service UUIDs are currently listed in the configured advertisement packet.
  • Also keep track of which services are currently enabled.
  • On every update to the advertisement packet, start/stop advertising the services according to the UUIDs in the advertisement packet.

So how this could work in practice:

  • When advertisement is started, go through the enabled services and start advertisements for the services with an UUID listed in AdvertisementOptions.ServiceUUIDs.
  • When advertisement is stopped, go through the enabled services and stop advertisement for all of them.
  • When a new service is added, check whether the UUID is part of AdvertisementOptions.ServiceUUIDs of the current advertisement and only call StartAdvertisingWithParameters if it's in there.

(We don't currently have a way to stop services, that would probably also be useful to add).