Seamless BLE Device Integration on Linux with BleuIO

October 13, 2023
Seamless BLE Device Integration on Linux with BleuIO

Bluetooth Low Energy (BLE) is a wireless communication technology commonly used in various IoT and wearable devices. With the right tools and libraries, working with BLE devices on Linux becomes easy and efficient. In this tutorial, we’ll explore how to use the BleuIO dongle and the associated Python library to scan for nearby BLE devices, connect to a device, and read its characteristics, specifically the device name.

Prerequisites

Before we begin, ensure you have the following:

  • BleuIO Dongle: You’ll need a BleuIO dongle, a versatile BLE device capable of working on any platform.
  • BleuIO Python Library: Install the BleuIO Python library, which provides the necessary tools for interacting with the BleuIO dongle. You can install it using pip:
pip install bleuio

Now that you have the prerequisites in place, let’s dive into the process.

Step 1: Setting up the Python Script

First, let’s set up a Python script to work with the BleuIO dongle. Here’s a script that demonstrates how to scan for nearby BLE devices, connect to one of them, and read characteristics.

import time
from datetime import datetime
from bleuio_lib.bleuio_funcs import BleuIO

# Creating a callback function for scan results
def my_scan_callback(scan_input):
    print("\n\nmy_scan_callback: " + str(scan_input))

# Creating a callback function for events
def my_evt_callback(evt_input):
    cbTime = datetime.now()
    currentTime = cbTime.strftime("%H:%M:%S")
    print("\n\n[" + str(currentTime) + "] my_evt_callback: " + str(evt_input))

# Initiating the BleuIO dongle
my_dongle = BleuIO()

# Registering the callback functions
my_dongle.register_evt_cb(my_evt_callback)
my_dongle.register_scan_cb(my_scan_callback)

# Switch to Central or Dual Gap Role
my_dongle.at_dual()

# Start scanning for devices
my_dongle.at_gapscan(3)

# Wait for a few seconds to allow devices to be discovered
time.sleep(4)

# Connect to a device using its MAC address
my_dongle.at_gapconnect('[1]D1:79:29:DB:CB:CC')

# Wait for the connection to establish
time.sleep(4)

# Read characteristics with handle '0003', which contains the device name
my_dongle.at_gattcread('0003')

# Wait briefly
time.sleep(1)

Step 2: Running the Script

Save the script to a Python file, for example, bleuio_ble_tutorial.py. Then, run the script using your Python interpreter.

The script performs the following actions:

  • Initiates the BleuIO dongle and sets up callback functions for scan results and events.
  • Switches to Central or Dual Gap Role.
  • Scans for nearby BLE devices for a specified duration (3 seconds in this example).
  • Connects to a specific device using its MAC address.
  • Waits for the connection to establish.
  • Reads the characteristics with handle ‘0003’, which typically contains the device name.
  • Waits briefly before exiting.

The scan results and characteristic data will be displayed on the terminal.

Output :

Working with BLE devices on Linux using the BleuIO dongle and Python library is a straightforward process. You can use this script as a starting point to interact with BLE devices and further develop your BLE applications. Remember to customize the script to suit your specific needs, and explore the wealth of possibilities that BLE technology offers in the world of IoT and wireless communication.

Share this post on :
Follow us on LinkedIn :

Order Now