Skip to content

Commit b67ed5a

Browse files
authored
Merge pull request #39 from dhalbert/bleradio-name-address
add BLERadio properties; add name and tx_power to advertisement scan_response
2 parents 668b0bc + c185636 commit b67ed5a

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
@@ -161,14 +161,16 @@ def start_advertising(self, advertisement, scan_response=None, interval=0.1):
161161
Starts advertising the given advertisement.
162162
163163
:param buf scan_response: scan response data packet bytes.
164-
``None`` if no scan response is needed.
164+
If ``None``, a default scan response will be generated that includes
165+
`BLERadio.name` and `BLERadio.tx_power`.
165166
:param float interval: advertising interval, in seconds
166167
"""
167-
scan_response_data = None
168-
if scan_response:
169-
scan_response_data = bytes(scan_response)
168+
if not scan_response:
169+
scan_response = Advertisement()
170+
scan_response.complete_name = self.name
171+
scan_response.tx_power = self.tx_power
170172
self._adapter.start_advertising(bytes(advertisement),
171-
scan_response=scan_response_data,
173+
scan_response=bytes(scan_response),
172174
connectable=advertisement.connectable,
173175
interval=interval)
174176

@@ -243,7 +245,7 @@ def connect(self, advertisement, *, timeout=4):
243245

244246
@property
245247
def connected(self):
246-
"""True if any peers are connected to the adapter."""
248+
"""True if any peers are connected."""
247249
return self._adapter.connected
248250

249251
@property
@@ -257,3 +259,28 @@ def connections(self):
257259
wrapped_connections[i] = self._connection_cache[connection]
258260

259261
return tuple(wrapped_connections)
262+
263+
@property
264+
def name(self):
265+
"""The name for this device. Used in advertisements and
266+
as the Device Name in the Generic Access Service, available to a connected peer.
267+
"""
268+
return self._adapter.name
269+
270+
@name.setter
271+
def name(self, value):
272+
self._adapter.name = value
273+
274+
@property
275+
def tx_power(self):
276+
"""Transmit power, in dBm."""
277+
return 0
278+
279+
@tx_power.setter
280+
def tx_power(self, value):
281+
raise NotImplementedError("setting tx_power not yet implemented")
282+
283+
@property
284+
def address_bytes(self):
285+
"""The device address, as a ``bytes()`` object of length 6."""
286+
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 returned
247+
from `BLERadio.start_scan()`. (read-only)"""
253248
return self._rssi
254249

255250
@classmethod

0 commit comments

Comments
 (0)