Writing Your Own Applications

As soon as you have successfully installed a Bluetooth module on the Arduino, you can communicate with the Arduino using programs such as the “Serial Bluetooth Terminal”.

But maybe you want to write your own applications that communicate in a specific way with the Arduino via Bluetooth: For this case I have a few example programs in python that implement a simple communication interface.

Bluetooth LE

The following examples refer to communication with an LE module. To send values to a Bluetooth Low Energy (BLE) device with Python, you need a suitable BLE Python module - e.g. bleak, the MAC address of the BLE device and the UUID of the characteristic you want to write to.

pip install bleak    

To get an overview of the available BLE devices, their services and characteristics, you can use the following program:

import asyncio
from bleak import BleakScanner, BleakClient

async def main():
    print("Scanning BLE devices...")
    devices = await BleakScanner.discover(timeout=5.0)

    for i, device in enumerate(devices):
        print(f"{i}: {device.name} [{device.address}]")

    # Select device
    index = int(input("Which device? "))
    selected_device = devices[index]

    print(f"Connect to {selected_device.name} ({selected_device.address})...")
    async with BleakClient(selected_device.address) as client:
        connected = await client.is_connected()
        print(f"Connected: {connected}")

        print("ervices & characteristics:")
        for service in client.services:
            print(f"Service: {service.uuid} - {service.description}")
            for char in service.characteristics:
                print(f"  Characteristic: {char.uuid} - Properties: {char.properties}")

if __name__ == "__main__":
    asyncio.run(main())

And here is a simple program to send values to the BLE device:

from bleak import BleakClient
import asyncio

ADDRESS = "64:69:4E:3C:4C:6A" # change to your MAC address
CHAR_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb"

async def main():
    async with BleakClient(ADDRESS) as client:
        print("✅ Connected to BLE device....")
        while True:
            comm = input("Value: ")
            if comm == "q":
                break
            await client.write_gatt_char(CHAR_UUID, comm.encode(), response=False)

asyncio.run(main())

Bluetooth Classic

If your Bluetooth Classic device (like an HC-05/HC-06 or another Serial Port Profile device) is already paired and appears as a COM port (on Windows) or /dev/rfcommX (on Linux), then you can use pyserial just like with any serial device.

pip install pyserial    

This program checks whether your device is accessible via a COM port and via which port:

import serial.tools.list_ports

ports = serial.tools.list_ports.comports()
for port in ports:
    print(f"{port.device} - {port.description}")

And here is a simple program to send values to the BL classic device:

import serial
import time

# Windows: something like 'COM5'
# Linux: something like '/dev/rfcomm0'
PORT = 'COM5'
BAUDRATE = 9600  # Depends on your device, 9600 is typical for HC-05

try:
    ser = serial.Serial(PORT, BAUDRATE, timeout=1)
    time.sleep(2)  # Wait for connection to stabilize (sometimes needed)

    message = "Hello from Python!\n"
    ser.write(message.encode('utf-8'))
    print("Message sent.")

    # Optional: wait for response
    response = ser.readline().decode('utf-8').strip()
    print("Received:", response)

finally:
    ser.close()