Security Example using Python library
Introduction
In this tutorial, we're going to pair two BleuIO dongle securely using Python script.
Connect two dongle into your computer that has Python installed.
One dongle will take on the Central role and the other will take on the Peripheral role and The example script will help pair two dongles using predefined passkey.
- Update the script to set peripheral dongles mac address.
- Your selected passkey.
- Central and peripheral dongles mac address.
# (C) 2021 Smart Sensor Devices AB
import time
from bleuio_lib.bleuio_funcs import BleuIo
passkey = "123456"
peripheral_dongle_addr = (
"[0]40:48:FD:E5:2D:B9" # Enter your peripheral dongles mac address here
)
connect_with_passkey = False
connect_with_numeric_comparision = False
choice = "0"
print("Welcome to the security example!\r\n")
print(
"Choose:\r\n1). For connection with passkey\r\n2). For connection with Numeric Comparision"
)
choice = input("\r\n>>")
if choice == "1":
connect_with_passkey = True
elif choice == "2":
connect_with_numeric_comparision = True
else:
print("You must choose 1 or 2. Exiting script...")
quit()
# Start
#Enter your Peripheral dongle com port
peripehral_dongle = BleuIo(port="COM6")
peripehral_dongle.start_daemon()
#Enter your Central dongle com port
central_dongle = BleuIo(port="COM5")
central_dongle.start_daemon()
central_status = central_dongle.ati()
for i in central_status:
# Checks if dongle is in Peripheral
# If it is it puts the dongle in Central Mode
if i.__contains__("Peripheral"):
setCentral = central_dongle.at_central()
peripehral_status = peripehral_dongle.ati()
for i in peripehral_status:
# Checks if dongle is in Central
# If it is it puts the dongle in Peripheral Mode to allow advertising
if i.__contains__("Central"):
setCentral = peripehral_dongle.at_peripheral()
if connect_with_passkey:
print(peripehral_dongle.at_set_passkey(passkey))
print(peripehral_dongle.at_gapiocap("0"))
print(peripehral_dongle.at_sec_lvl("3"))
print(peripehral_dongle.at_advstart())
print(central_dongle.at_gapiocap("2"))
if connect_with_numeric_comparision:
print(peripehral_dongle.at_gapiocap("1"))
print(peripehral_dongle.at_sec_lvl("3"))
print(peripehral_dongle.at_advstart())
print(central_dongle.at_gapiocap("1"))
print(central_dongle.at_numcompa("0"))
print("Waiting for connection...")
time.sleep(2)
connection = central_dongle.at_gapconnect(peripheral_dongle_addr, 5)
for y in connection:
if "CONNECTED." in str(y):
print("Connected!")
if connect_with_passkey:
print(central_dongle.at_enter_passkey(passkey))
if connect_with_numeric_comparision:
print(central_dongle.at_numcompa())
time.sleep(2)
print(central_dongle.at_sec_lvl())
print(peripehral_dongle.at_sec_lvl())
exit_msg = input("Press enter to exit.")
print(central_dongle.at_gapdisconnect())
print(peripehral_dongle.at_advstop())
central_dongle.stop_daemon()
peripehral_dongle.stop_daemon()
quit()