Smart USB dongle 2.0

Smart USB dongle 2.0

  • Buy Now
  • Manual
  • AT Commands
  • Help

›Scripts & Tutorials

Manual

  • How it works
  • How to use
  • Known Issues
  • Firmware Updates
  • Release History
  • Troubleshooting

AT-Commands

  • List of AT Commands

Scripts & Tutorials

  • Getting Started
  • Python: iBeacon
  • Python: Eddystone Beacon
  • Python: Scan
  • Python: Scan and Store
  • Python: SPS Script
  • Javascript: Google chrome.serial Beacon

More

  • Links

Scan and Store

Introduction

In this example, we're going to set up the dongle using Python script to scan for nearby Bluetooth devices with a specific Manufacturer Specific (MFS) ID and then store the results together with a timestamp and the MAC Address in a simple text file. For a quick setup, copy the following script and save it on your local directory. You can also get the source code from our GitHub page.

import serial
import time
from datetime import datetime

# Name of file that will be created and store the saved data
file_name = "SavedData.txt"
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 Scan and Store example!\n\n")

# Method for parsing and writing to file
def write_data_to_file(out_data):
    addr_string = ""
    data_string = ""
    now = datetime.now() # Generating a timestamp
    current_time = now.strftime("%H:%M:%S") # Formatting the timestamp
    out_data = out_data.replace('\r','') # Remove return.
    out_data = out_data.replace('\n','') # Remove new line.
    fo = open(file_name, "a")
    for i in range(2,21): # Reading the MAC-Address and saving it into addr_string
        addr_string += out_data[i]
    for x in range(41, 102): # Here the advertising/response data gets stored in the data_string variable
        data_string += out_data[x]
    fo.write("{")
    fo.write("["+current_time+"]")
    fo.write(addr_string)
    fo.write(":")
    fo.write(data_string)
    fo.write("}\n")
    fo.close()


new_input = 1
try:
    while 1 and console.is_open.__bool__():
        # get keyboard input once
        if (new_input == 1):
            # Python 2 users
            # input = raw_input("Enter something such as a Manufacturer Specific (MFS) ID to scan for and store in a file or just leave it blank to scan all: ")
            new_input = input("Enter something such as a Manufacturer Specific (MFS) ID to scan for and store in a file or just leave it blank to scan all: ")
            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+FINDSCANDATA="))
            console.write(new_input.encode())
            console.write('\r'.encode())
            out = ''
            # Let's wait one second before reading output (let's give device time to answer)
            time.sleep(1)
            print("\nCollecting data...\nPress Ctrl-C to stop.")
        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
                # and check for blankspace and that [out] isn't anything else before writing to file
                if not out.__contains__("AT+") and not len(out) <= 106:
                    write_data_to_file(out)
                out = "  "
except KeyboardInterrupt:
    exit()

Open up the command prompt in the directory where the script is located. Start the script by typing

python scan_and_store_example.py

and press Enter.

You should now be prompted to enter what you want to scan for. For example a Manufacturer Specific ID (if you want to scan for Smart Sensor Devices products for example you enter the Flag for MFS Data FF and then our MFS ID: 5B07).

Dongle

The script will then generate a *.txt file in the same directory with the name entered as the file_name variable in the script. For example: *"SavedData.txt"*.

The script will run until you stop it. You can then have a look in the text file...

Dongle

Note that the script will not work if the text file is open or altered during runtime.

And that is how we store data that we scanned from Bluetooth devices. If you want to stop the script, you can simply press control C.

← Python: ScanPython: SPS Script →
  • Introduction
Docs
ManualAT CommandsHelp
Community
YouTubeTwitterLinkedInFacebook
More
Smart Sensor DevicesBleuIOGitHub
Copyright © 2020 Smart Sensor Devices AB.