Scripts
Introduction
To use these scripts you will need to have Python installed. Both Python2 and Python3 should work but the scripts are made with Python3 in mind. But there are comments where the script need to be edited to work with Python2. You will also need to install the module pySerial. The easiest way to install it is through pip (which you should already have after installing Python) by running:
Python2:
pip install pyserial
Python3:
python3 -m pip install pyserial
Now you need to download the script you want to use, either via our GitHub page or just copy it from below, paste in a text editor and save it as .py-file. Then open up the command prompt inside the directory where the script you want to run is. Then run:
python scriptname.py
NOTE: You will need to manually change the COM port in the script to which ever your dongle uses. The number may vary from machine to machine.
iBeacon Example
import serial
import time
connecting_to_dongle = 0
print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings
while connecting_to_dongle == 0:
try:
console = serial.Serial(
port='COM14',
baudrate=57600,
parity="N",
stopbits=1,
bytesize=8,
timeout=0
)
if console.is_open.__bool__():
connecting_to_dongle = 1
except:
print("Dongle not connected. Please reconnect Dongle.")
time.sleep(5)
print("\n\nConnected to Dongle.\n")
print("\n Welcome to the iBeacon example!\n\n")
new_input = 1
while 1 and console.is_open.__bool__():
# get keyboard input once
if (new_input == 1):
# Python 2 users
# input = raw_input("Enter the UUID... ")
new_input = input("Enter the UUID (x) string with Major (j), Minor (n) and TX (t) (format:"
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxjjjjnnnntt): ")
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+ADVDATAI="))
console.write(new_input.encode())
console.write('\r'.encode())
time.sleep(0.1)
console.write(str.encode("AT+ADVSTART=0;200;3000;0;"))
console.write('\r'.encode())
out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while console.inWaiting() > 0:
out += console.read(console.inWaiting()).decode()
else:
if not out.isspace():
# We make sure it doesn't print the same message over and over again by setting [out] to blankspace
# after printing once and check for blankspace before print again
print(">>" + out)
out = " "
Follow the Youtube tutorial. Full source also available on GitHub.
Scan Examples
import serial
import time
connecting_to_dongle = 0
print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings
while connecting_to_dongle == 0:
try:
console = serial.Serial(
port='COM14',
baudrate=57600,
parity="N",
stopbits=1,
bytesize=8,
timeout=0
)
if console.is_open.__bool__():
connecting_to_dongle = 1
except:
print("Dongle not connected. Please reconnect Dongle.")
time.sleep(5)
print("\n\nConnected to Dongle.\n")
print("\nWelcome to the Bluetooth device Scanning example!\n\n")
new_input = "NEW-INPUT"
while 1 and console.is_open.__bool__():
# get keyboard input once
if (new_input == "NEW-INPUT"):
# Python 2 users
# input = raw_input("Select:\n1) If you... ")
new_input = input("Select:\n1) If you'd like to scan for devices without a timer to stop.\n2)"
" If you'd like to scan for devices for a selected period of time.\n"
"3) If you'd like to scan a specific device.\n>>")
if new_input == "1":
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+CENTRAL"))
console.write('\r'.encode())
time.sleep(0.1)
console.write(str.encode("AT+GAPSCAN"))
console.write('\r'.encode())
elif new_input == "2":
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+CENTRAL"))
console.write('\r'.encode())
input_time = input("Please select amount of time the scanning should continue: ")
while not input_time.isdigit():
input_time = input("Sorry, unacceptable time.\n"
"Please select amount of time the scanning should continue: ")
console.write(str.encode("AT+GAPSCAN="))
console.write(input_time.encode())
console.write('\r'.encode())
elif new_input == "3":
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+CENTRAL"))
console.write('\r'.encode())
time.sleep(0.1)
input_adress = input("Please enter type ([0] or [1]) and the address (xx:xx:xx:xx:xx:xx) of the device you "
"\nwish to scan (format:[x]xx:xx:xx:xx:xx:xx): ")
console.write(str.encode("AT+SCANTARGET="))
console.write(input_adress.encode())
console.write('\r'.encode())
else:
print("That was not a choice. Please choose one of the options.")
new_input="NEW-INPUT"
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
out = ""
while console.inWaiting() > 0:
out += console.read(console.inWaiting()).decode()
else:
if not out.isspace():
# We make sure it doesn't print the same message over and over again by setting [out] to blankspace
# after printing once and check for blankspace before print again
print(out + " ")
out = " "
Follow the Youtube tutorial. Full source also available on GitHub.
SPS Examples
import serial
import time
connecting_to_dongle = 0
print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings
while connecting_to_dongle == 0:
try:
console = serial.Serial(
port='COM14',
baudrate=57600,
parity="N",
stopbits=1,
bytesize=8,
timeout=0
)
if console.is_open.__bool__():
connecting_to_dongle = 1
except:
print("Dongle not connected. Please reconnect Dongle.")
time.sleep(5)
print("\n\nConnected to Dongle.\n")
print("\nWelcome to the Serial Port Service (SPS) example!\n\n")
# Python 2 users
# input = raw_input("Choose \n1) for Peripheral...
role_input = input("Choose \n1) for Peripheral Role\n2) for Central role\n>> ")
while not (role_input == "1" or role_input == "2"):
role_input = input("Please choose 1 or 2.\nChoose 1 for Peripheral Role or 2 for Central role: ")
connected = "0";
while 1 and console.is_open.__bool__():
out = ""
while console.inWaiting() > 0:
out += console.read(console.inWaiting()).decode()
if not out.isspace():
# We make sure it doesn't print the same message over and over again by setting [out] to blankspace
# after printing once and check for blankspace before print again
print(out + " ")
out = " "
if role_input == "1":
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+ADVSTART"))
console.write('\r'.encode())
print("Please wait for Central to connect before typing.")
while connected == "0":
out += console.read(console.inWaiting()).decode()
time.sleep(5)
if not out.isspace():
# We make sure it doesn't print the same message over and over again by setting [out] to blankspace
# after printing once and check for blankspace before print again
print(out + " ")
if out.__contains__("CONNECTED."):
connected = "1"
out = " "
console.write(str.encode("AT+SPSRECEIVE"))
console.write('\r'.encode())
role_input = "0"
elif role_input == "2":
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+CENTRAL"))
console.write('\r'.encode())
input_address = input("Please address ([x]xx:xx:xx:xx:xx:xx) of Peripheral device: ")
console.write(str.encode("AT+GAPCONNECT="))
console.write(input_address.encode())
console.write('\r'.encode())
print("Please wait to connect to Peripheral before typing.")
while connected == "0":
out += console.read(console.inWaiting()).decode()
time.sleep(5)
if not out.isspace():
# We make sure it doesn't print the same message over and over again by setting [out] to blankspace
# after printing once and check for blankspace before print again
print(out + " ")
if out.__contains__("CONNECTED."):
connected = "1"
out = " "
console.write(str.encode("AT+SPSRECEIVE"))
console.write('\r'.encode())
role_input = "0"
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
# Wait to open terminal until the device is connected
if connected == "1":
terminal_input = input(">>")
time.sleep(0.1)
# sends the commands to the dongle. Important to send the \r as that is the return-key.
console.write(str.encode("AT+SPSSEND="))
console.write(terminal_input.encode())
console.write('\r'.encode())
time.sleep(0.5)
console.write(str.encode("AT+SPSRECEIVE"))
console.write('\r'.encode())
Full source also available on GitHub.