Skip to content

Commit 238c3f4

Browse files
author
Michael Sklar
committed
modified scan_and_connect to limit scanning to the central and advertising to the peripheral
1 parent 19d0978 commit 238c3f4

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

CLUE_BLE_Morse_Code/code.py

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from adafruit_ble.services.nordic import UARTService
1414

1515
#--| User Config |---------------------------------------------------
16-
MY_NAME = "ME"
17-
FRIENDS_NAME = "FRIEND"
16+
MY_NAME = "CENTRAL"
17+
FRIENDS_NAME = "PERIPHERAL"
1818
#--| User Config |---------------------------------------------------
1919

2020
WAIT_FOR_DOUBLE = 0.05
@@ -73,56 +73,61 @@ def scan_and_connect():
7373
Advertise self while scanning for friend. If friend is found, can
7474
connect by pressing A+B buttons. If friend connects first, then
7575
just stop.
76-
77-
Return is a UART object that can be used for read/write.
7876
'''
7977

80-
print("Advertising.")
81-
central = False
82-
ble.start_advertising(advertisement)
83-
84-
print("Waiting.")
85-
friend = None
86-
while not ble.connected:
87-
88-
if friend is None:
89-
print("Scanning.")
90-
in_label.text = out_label.text = "Scanning..."
91-
for adv in ble.start_scan():
92-
if ble.connected:
93-
# Friend connected with us, we're done
94-
ble.stop_scan()
95-
break
96-
if adv.complete_name == FRIENDS_NAME:
97-
# Found friend, can stop scanning
98-
ble.stop_scan()
99-
friend = adv
100-
print("Found", friend.complete_name)
101-
in_label.text = "Found {}".format(friend.complete_name)
102-
out_label.text = "A+B to connect"
103-
break
104-
else:
105-
if clue.button_a and clue.button_b:
106-
# Connect to friend
107-
print("Connecting to", friend.complete_name)
108-
ble.connect(friend)
109-
central = True
110-
111-
# We're now connected, one way or the other
112-
print("Stopping advertising.")
113-
ble.stop_advertising()
78+
# peripheral
79+
if MY_NAME == "PERIPHERAL":
80+
print("Advertising.")
81+
central = False
82+
ble.start_advertising(advertisement)
83+
84+
while not ble.connected:
85+
if ble.connected:
86+
break
87+
88+
# We're now connected, one way or the other
89+
print("Stopping advertising.")
90+
ble.stop_advertising()
11491

115-
# Return a UART object to use
116-
if central:
117-
print("Central - using my UART service.")
118-
return uart_service
119-
else:
12092
print("Peripheral - connecting to their UART service.")
12193
for connection in ble.connections:
12294
if UARTService not in connection:
12395
continue
12496
return connection[UARTService]
12597

98+
# central
99+
else:
100+
print("Waiting.")
101+
friend = None
102+
while not ble.connected:
103+
if friend is None:
104+
print("Scanning.")
105+
in_label.text = out_label.text = "Scanning..."
106+
for adv in ble.start_scan():
107+
if ble.connected:
108+
# Friend connected with us, we're done
109+
ble.stop_scan()
110+
break
111+
if adv.complete_name == FRIENDS_NAME:
112+
# Found friend, can stop scanning
113+
ble.stop_scan()
114+
friend = adv
115+
print("Found", friend.complete_name)
116+
in_label.text = "Found {}".format(friend.complete_name)
117+
out_label.text = "A+B to connect"
118+
break
119+
else:
120+
if clue.button_a and clue.button_b:
121+
# Connect to friend
122+
print("Connecting to", friend.complete_name)
123+
ble.connect(friend)
124+
central = True
125+
126+
127+
# Return a UART object to use
128+
print("Central - using my UART service.")
129+
return uart_service
130+
126131
#--------------------------
127132
# The main application loop
128133
#--------------------------

0 commit comments

Comments
 (0)