Developing BLE Applications : A Tutorial for Linux/MacOS

December 18, 2023
Developing BLE Applications : A Tutorial for Linux/MacOS

Bluetooth Low Energy (BLE) technology has become increasingly popular for creating wireless applications with low power consumption. In this tutorial, we’ll explore the process of scanning for nearby BLE devices, connecting to a specific device, and reading characteristics using the BleuIO BLE USB dongle. The tutorial is designed for Linux/MacOS environments.

Introduction to BleuIO

BleuIO is a versatile Bluetooth Low Energy USB dongle that simplifies BLE application development. It supports the AT command set, allowing developers to interact with the dongle using familiar commands. Whether you’re a seasoned BLE developer or just getting started, BleuIO offers a convenient and efficient way to work with BLE applications.

Forget complex libraries and complicated programming. Develop BLE applications effortlessly through simple AT commands.

Setting Up the Development Environment

Before diving into the tutorial, ensure you have the following prerequisites:

  • BleuIO USB Dongle: Connect the BleuIO dongle to your computer.
  • Python: Ensure that Python is installed on your system.

Writing the BLE Application Script

The provided Python script demonstrates the process of connecting to the BleuIO dongle, scanning for nearby BLE devices, and reading characteristics. Let’s break down the key components of the script:

# Importing necessary modules
import serial
import time

# Establishing connection to the BleuIO dongle
connecting_to_dongle = 0
print("Connecting to dongle...")

while connecting_to_dongle == 0:
    try:
        # Configuring the serial connection
        console = serial.Serial(
            port='/dev/cu.usbmodem4048FDE52CF21',
            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)

# Sending commands to the BleuIO dongle
console.write(str.encode("AT+CENTRAL"))
console.write('\r'.encode())
time.sleep(0.1)
console.write(str.encode("AT+GAPSCAN=3"))
console.write('\r'.encode())
time.sleep(3.5)
console.write(str.encode("AT+GAPCONNECT=[1]D1:79:29:DB:CB:CC"))
console.write('\r'.encode())
console.write(str.encode("AT+GATTCREAD=0013"))
console.write('\r'.encode())

# Waiting for the dongle to respond
time.sleep(1)
out = ""
while console.inWaiting() > 0:
    out += console.read(console.inWaiting()).decode()
else:
    if not out.isspace():
        print(out + " ")
        out = " "

Explanation:

1. Connecting to BleuIO:

  • Download and install a serial terminal emulator like screen or minicom.
  • Ensure your dongle is connected and note its port (e.g., /dev/cu.usbmodem4048FDE52CF21).
  • Run the script with the correct port and baudrate (57600) in your terminal.
  • The script attempts connection until successful, then sends essential AT commands:
    • AT+CENTRAL: Configures BleuIO as a central device (scanning/connecting).
    • AT+GAPSCAN=3: Starts scanning for BLE devices for 3 seconds.

2. Selecting and Connecting:

  • The scan results will appear in your terminal.
  • Identify the desired device, usually by name or MAC address.
  • Replace D1:79:29:DB:CB:CC in the script with your device’s MAC address.
  • Send AT+GAPCONNECT=[1]D1:79:29:DB:CB:CC to connect to the device (replace the mac address with your desired device if needed).

3. Reading a Characteristic:

  • Every BLE device exposes characteristics containing specific data.
  • Replace 0013 in AT+GATTCREAD with the characteristic’s UUID to read its value.
  • This script reads the characteristic with UUID 0013 and prints its value. Currently its showing the device type.

The outout

The output will be shown on the screen as

Value read: SSD002/2B
Hex: 0x5353443030322F3242
Size: 9

Understanding the Code:

  • The script uses serial library to communicate with BleuIO via AT commands.
  • While loops ensure connection success and read all available data.
  • The script parses output and filters empty space to avoid repetitive printing.

In this tutorial, we’ve covered the basics of working with the BleuIO BLE USB dongle on Linux/MacOS systems. The simplicity of the AT command interface and cross-platform compatibility make BleuIO an excellent choice for BLE application development. Experiment with the provided script, explore additional AT commands from BleuIO, and unlock the full potential of BLE applications with BleuIO. Happy coding!

Share this post on :
Follow us on LinkedIn :

Order Now