Skip to content

Commit 121a070

Browse files
committed
scanner work
1 parent 4710b1f commit 121a070

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

adafruit_ble/scanner.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
`adafruit_ble.scanner`
2424
====================================================
2525
26-
UART-style communication.
26+
Scan for nearby BLE Devices
2727
2828
* Author(s): Dan Halbert for Adafruit Industries
2929
@@ -63,22 +63,21 @@ def scan(self, timeout, *, interval=0.1, window=0.1):
6363
Must be in the range 0.0025 - 40.959375 seconds.
6464
:param float window: the duration (in seconds) to scan a single BLE channel.
6565
`window` must be <= `interval`.
66-
:returns: advertising packets found
67-
:rtype: list of `adafruit_ble.ScanEntry` objects
68-
"""
69-
return [ScanEntry(entry) for entry in self._scanner.scan(timeout, interval=interval, window=window)]
66+
:returns a list of `adafruit_ble.ScanEntry` objects.
7067
68+
"""
69+
return [ScanEntry(e) for e in self._scanner.scan(timeout, interval=interval, window=window)]
7170

7271
class ScanEntry:
7372
"""
7473
Information about an advertising packet from a BLE device received by a `Scanner`.
7574
76-
:param bleio.ScanEntry entry: lower-level ScanEntry returned from `bleio.Scanner`.
77-
This constructor is normally used only `Scanner`.
75+
:param bleio.ScanEntry scan_entry: lower-level ScanEntry returned from `bleio.Scanner`.
76+
This constructor is normally used only by `Scanner`.
7877
"""
7978

80-
def __init__(self, entry):
81-
self._bleio_entry = entry
79+
def __init__(self, scan_entry):
80+
self._bleio_scan_entry = scan_entry
8281

8382
def item(self, item_type):
8483
"""Return the bytes in the advertising packet for given the element type.
@@ -96,23 +95,23 @@ def item(self, item_type):
9695
# Type doesn't match: skip to next item.
9796
i += item_length + 1
9897
else:
99-
return adv_bytes[i + 2:i + item_length]
98+
return adv_bytes[i + 2:i + 1 + item_length]
10099
return None
101100

102101
@property
103102
def advertisement_bytes(self):
104103
"""The raw bytes of the received advertisement."""
105-
return self._bleio_entry.raw_data
104+
return self._bleio_scan_entry.advertisement_bytes
106105

107106
@property
108107
def rssi(self):
109108
"""The signal strength of the device at the time of the scan. (read-only)."""
110-
return self._bleio_entry.rssi
109+
return self._bleio_scan_entry.rssi
111110

112111
@property
113112
def address(self):
114113
"""The address of the device. (read-only)."""
115-
return self._bleio_entry.address
114+
return self._bleio_scan_entry.address
116115

117116
@property
118117
def name(self):
@@ -146,3 +145,22 @@ def service_uuids(self):
146145
def manufacturer_specific_data(self):
147146
"""Manufacturer-specific data in the advertisement, returned as bytes."""
148147
return self.item(AdvertisingPacket.MANUFACTURER_SPECIFIC_DATA)
148+
149+
def matches(self, other):
150+
"""True if two scan entries appear to be from the same device. Their
151+
addresses and advertisement_bytes must match.
152+
"""
153+
return self.address == other.address and self.advertisement_bytes == other.advertisement_bytes
154+
155+
@classmethod
156+
def unique(self, scan_entries):
157+
"""Discard duplicate scan entries that appear to be from the same device.
158+
159+
:param sequence scan_entries: ScanEntry objects
160+
:returns list: list with duplicates removed
161+
"""
162+
unique = []
163+
for entry in scan_entries:
164+
if not any(entry.matches(unique_entry) for unique_entry in unique):
165+
unique.append(entry);
166+
return unique

adafruit_ble/uuid.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_ble.uuid`
24+
====================================================
25+
26+
BLE UUIDs
27+
28+
* Author(s): Dan Halbert for Adafruit Industries
29+
30+
"""
31+
32+
from bleio import UUID as bleio_UUID
33+
34+
UUID = bleio_UUID
35+
"""`adafruit_ble.UUID` is the same as `bleio.UUID`"""

0 commit comments

Comments
 (0)