Skip to content

Commit cf8c59b

Browse files
committed
add BLERadio properties; add name and tx_power to advertisement scan_response
1 parent 795dd52 commit cf8c59b

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

adafruit_ble/__init__.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,16 @@ def start_advertising(self, advertisement, scan_response=None, interval=0.1):
153153
Starts advertising the given advertisement.
154154
155155
:param buf scan_response: scan response data packet bytes.
156-
``None`` if no scan response is needed.
156+
If ``None``, a default scan response will be generated that includes
157+
`BLERadio.name` and `BLERadio.tx_power`.
157158
:param float interval: advertising interval, in seconds
158159
"""
159-
scan_response_data = None
160-
if scan_response:
161-
scan_response_data = bytes(scan_response)
160+
if not scan_response:
161+
scan_response = Advertisement()
162+
scan_response.complete_name = self.name
163+
scan_response.tx_power = self.tx_power
162164
self._adapter.start_advertising(bytes(advertisement),
163-
scan_response=scan_response_data,
165+
scan_response=bytes(scan_response),
164166
connectable=advertisement.connectable,
165167
interval=interval)
166168

@@ -235,7 +237,7 @@ def connect(self, advertisement, *, timeout=4):
235237

236238
@property
237239
def connected(self):
238-
"""True if any peers are connected to the adapter."""
240+
"""True if any peers are connected."""
239241
return self._adapter.connected
240242

241243
@property
@@ -249,3 +251,28 @@ def connections(self):
249251
wrapped_connections[i] = self._connection_cache[connection]
250252

251253
return tuple(wrapped_connections)
254+
255+
@property
256+
def name(self):
257+
"""The default name for this device. Used in advertisements and
258+
as the Device Name in the Generic Access Service, available to a connected peer.
259+
"""
260+
return self._adapter.name
261+
262+
@name.setter
263+
def name(self, value):
264+
self._adapter.name = value
265+
266+
@property
267+
def tx_power(self):
268+
"""Transmit power, in dBm."""
269+
return 0
270+
271+
@tx_power.setter
272+
def tx_power(self, value):
273+
raise NotImplementedError("setting tx_power not yet implemented")
274+
275+
@property
276+
def address_bytes(self):
277+
"""The device address, as a ``bytes()`` object of length 6."""
278+
return self._adapter.address.address_bytes

adafruit_ble/advertising/__init__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,7 @@ class Advertisement:
220220
# """Data size in a regular BLE packet."""
221221

222222
def __init__(self):
223-
"""Create an advertising packet.
224-
225-
:param buf data: if not supplied (None), create an empty packet
226-
if supplied, create a packet with supplied data. This is usually used
227-
to parse an existing packet.
228-
"""
223+
"""Create an advertising packet."""
229224
self.data_dict = {}
230225
self.address = None
231226
self._rssi = None
@@ -248,8 +243,8 @@ def from_entry(cls, entry):
248243

249244
@property
250245
def rssi(self):
251-
"""Signal strength of the scanned advertisement. Only available on Advertisement's created
252-
from ScanEntrys. (read-only)"""
246+
"""Signal strength of the scanned advertisement. Only available on Advertisements created
247+
from ScanEntry objects. (read-only)"""
253248
return self._rssi
254249

255250
@classmethod

0 commit comments

Comments
 (0)