-
Notifications
You must be signed in to change notification settings - Fork 170
Expose stop service advertisment on Windows #281
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
base: dev
Are you sure you want to change the base?
Conversation
return err | ||
} | ||
|
||
a.gattServiceProvider = serviceProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understood correctly each GATT service requires its own GattServiceProvider
. So if someone adds two services this would only store the latest created.
Users should be able to add and stop the specific service they want.
I was thinking that a map with the UUID and the service provider could work, but the BLE spec allows UUIDs to be repeated (a problem already found on other ble libraries hbldh/bleak#362).
So maybe AddService()
should return some handle to the created service to stop it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe AddService() should return some handle to the created service to stop it?
That sounds like a very reasonable suggestion!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my review. The API as proposed would mean there's a significant difference between Windows and other systems. Working around that is going to be annoying, but I proposed a solution in the comment below.
func (a *Adapter) StopServiceAdvertisement() error { | ||
return a.gattServiceProvider.StopAdvertising() | ||
} |
There was a problem hiding this comment.
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 callStartAdvertisingWithParameters
if it's in there.
(We don't currently have a way to stop services, that would probably also be useful to add).
It seems like you need to call
StopAdvertising
on theGattServiceProvider
when you want to change the service being advertised or else Windows will just advertise both services. This just exposes that function on the adapter but only for Windows. I added a dummy function on the Linux implementation so apps that use it still build without having to make Linux and Windows specific files.I'm not sure if a Linux implementation is needed or if BlueZ handles this differently.