1.
import serial
2.
import time
3.
4.
connecting_to_dongle =
0
5.
print("Connecting
to
dongle...")
6.
# Trying
to
connect
to
dongle
until
connected. Make sure the port
and
baudrate
is
the same
as
your
dongle.
7.
# You can check
in
the device manager
to
see what port
then
right-
click
and
choose properties
then
the Port Settings
8.
# tab
to
see the other settings
9.
while
connecting_to_dongle ==
0
:
10.
try
:
11.
console = serial
.
Serial(
12.
port=
'COM14'
,
13.
baudrate=
57600
,
14.
parity="N",
15.
stopbits=
1
,
16.
bytesize=
8
,
17.
timeout=
0
18.
)
19.
if
console
.
is_open
.
__bool__():
20.
connecting_to_dongle =
1
21.
except
:
22.
print("Dongle
not
connected. Please reconnect Dongle.")
23.
time
.
sleep(
5
)
24.
25.
26.
print("\n\nConnected
to
Dongle.\n")
27.
print("\n Welcome
to
the iBeacon example!\n\n")
28.
29.
30.
new_input =
1
31.
while
1
and
console
.
is_open
.
__bool__():
32.
# get keyboard input once
33.
if
(new_input ==
1
):
34.
# Python
2
users
35.
# input = raw_input("Enter the UUID... ")
36.
new_input = input("Enter the UUID (x)
string
with
Major (j), Minor (n)
and
TX (t) (format:
"
37.
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxjjjjnnnntt): ")
38.
time
.
sleep(
0.1
)
39.
# sends the commands
to
the dongle. Important
to
send the \r
as
that
is
the return-key.
40.
console
.
write
(str
.
encode("AT+ADVDATAI="))
41.
console
.
write
(new_input
.
encode())
42.
console
.
write
(
'\r'
.encode())
43.
time
.
sleep(
0.1
)
44.
console
.
write
(str
.
encode("AT+ADVSTART=
0
;
200
;
3000
;
0
;"))
45.
console
.
write
(
'\r'
.encode())
46.
out =
''
47.
# let
's wait one second before reading output (let'
s give device time
to
answer)
48.
time
.
sleep(
1
)
49.
while
console
.
inWaiting() >
0
:
50.
out += console
.
read(console
.
inWaiting()).decode()
51.
else
:
52.
if
not
out
.
isspace():
53.
# We make sure it doesn't print the same message over
and
over again by setting [out]
to
blankspace
54.
# after printing once
and
check
for
blankspace before print again
55.
print(">>" + out)
56.
out = " "