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

Added ability to set advertising interval #419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion bluezero/advertisement.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def __init__(self, advert_id, ad_type):
'ServiceData': None,
'Includes': set(),
'Appearance': None,
'LocalName': None
'LocalName': None,
'MinInterval': None,
'MaxInterval': None,
}
}

Expand Down Expand Up @@ -182,6 +184,24 @@ def appearance(self):
def appearance(self, appearance):
self.Set(constants.LE_ADVERTISEMENT_IFACE, 'Appearance', appearance)

@property
def min_interval(self):
"""List of UUIDs that represent available services."""
return self.Get(constants.LE_ADVERTISEMENT_IFACE, 'MinInterval')

@min_interval.setter
def min_interval(self, value):
self.Set(constants.LE_ADVERTISEMENT_IFACE, 'MinInterval', value)

@property
def max_interval(self):
"""List of UUIDs that represent available services."""
return self.Get(constants.LE_ADVERTISEMENT_IFACE, 'MaxInterval')

@max_interval.setter
def max_interval(self, value):
self.Set(constants.LE_ADVERTISEMENT_IFACE, 'MaxInterval', value)

@dbus.service.method(constants.DBUS_PROP_IFACE,
in_signature='s',
out_signature='a{sv}')
Expand Down Expand Up @@ -223,6 +243,12 @@ def GetAll(self, interface_name): # pylint: disable=invalid-name
if self.props[interface_name]['Appearance'] is not None:
response['Appearance'] = dbus.UInt16(
self.props[interface_name]['Appearance'])
if self.props[interface_name]['MinInterval'] is not None:
response['MinInterval'] = dbus.UInt32(
self.props[interface_name]['MinInterval'])
if self.props[interface_name]['MaxInterval'] is not None:
response['MaxInterval'] = dbus.UInt32(
self.props[interface_name]['MaxInterval'])
response['Includes'] = dbus.Array(
self.props[interface_name]['Includes'], signature='s')

Expand Down
17 changes: 17 additions & 0 deletions bluezero/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ def __init__(self, adapter_address, local_name=None, appearance=None):
self.dongle = adapter.Adapter(adapter_address)
self.local_name = local_name
self.appearance = appearance
self.min_advertising_interval = None
self.max_advertising_interval = None
self.advert = advertisement.Advertisement(1, 'peripheral')
self.ad_manager = advertisement.AdvertisingManager(adapter_address)
self.mainloop = async_tools.EventLoop()

def set_advertising_interval(self, min, max):
"""
Set the advertising interval for the ble service in milliseconds (ms)
Need to set Experimental: True in /etc/bluetooth/main.conf

:param min: min interval of advertising
:param max: max interval of advertising
"""
self.min_advertising_interval = min
self.max_advertising_interval = max

def add_service(self, srv_id, uuid, primary):
"""
Add the service information required
Expand Down Expand Up @@ -126,6 +139,10 @@ def _create_advertisement(self):
self.advert.local_name = self.local_name
if self.appearance:
self.advert.appearance = self.appearance
if self.min_advertising_interval:
self.advert.min_interval = self.min_advertising_interval
if self.max_advertising_interval:
self.advert.max_interval = self.max_advertising_interval

def publish(self):
"""Create advertisement and make peripheral visible"""
Expand Down