Skip to main content

Like a telescope for Bluetooth Low Energy devices

Project description

BleScope - Bluetooth Low Energy Scanner

This file provides guidance to BleScope's architecture, design patterns, and development commands.

Project Overview

BleScope is a Python-based Bluetooth Low Energy (BLE) scanner application built with FastAPI. Like a telescope for Bluetooth devices, it discovers and tracks BLE devices in the vicinity with real-time updates via WebSockets.

Development Commands

Environment Setup

# Install dependencies using Poetry (required - not pip)
poetry install

# Activate virtual environment
poetry shell

Running the Application

# Run FastAPI server with hot reload
poetry run python src/main.py

# Alternative - run directly after activating shell
python src/main.py

API Testing Commands

# Check application health
curl http://localhost:8000/health

# Check scan status
curl http://localhost:8000/api/v1/scan/status

# Start BLE scanning
curl -X POST http://localhost:8000/api/v1/scan/start

# Stop BLE scanning  
curl -X POST http://localhost:8000/api/v1/scan/stop

# Access web UI
open http://localhost:8000

# Access API documentation
open http://localhost:8000/docs

Architecture Overview

BleScope implements hexagonal architecture with domain-driven design and event-driven communication for Bluetooth Low Energy scanning.

Core Architecture Pattern

  • Hexagonal Architecture: Ports & adapters pattern with clean separation between business logic and external dependencies
  • Domain-Driven Design: Rich domain models with pure business logic in the core
  • Event-Driven: Asynchronous communication through event bus pattern
  • CQRS: Separate command and query handlers for different use cases
  • Dependency Injection: All dependencies wired via FastAPI's DI system

Layer Structure

Domain Layer (src/blescope/scanning/domain/)

  • Core entities: DiscoveredDevice, Scan
  • Domain events: ScanStarted, ScanStopped, DeviceDiscovered
  • Pure Python with no external dependencies

Application Layer (src/blescope/scanning/application/)

  • Ports: Abstract interfaces (BluetoothScanner, ScanRepository, DeviceRepository)
  • Services: ScanManager orchestrates scanning operations
  • Commands: Use case handlers for actions
  • Queries: Read-only operations (GetScanStatusQueryHandler, GetDiscoveredDevicesQueryHandler)

Infrastructure Layer (src/blescope/scanning/infrastructure/)

  • Adapters: Concrete implementations
    • BleakScannerAdapter: Cross-platform BLE scanning via Bleak library
    • InMemoryScanRepository & InMemoryDiscoveredDeviceRepository: In-memory persistence
  • Web: FastAPI routers (scan_router.py)

API Layer (src/blescope/api/)

  • FastAPI application factory (app.py)
  • Dependency injection (dependencies.py)
  • WebSocket manager for real-time updates
  • Static file serving

Shared Layer (src/blescope/shared/)

  • Event bus implementation
  • Configuration via Pydantic Settings
  • Base domain types (DeviceAddress, RSSI)

Real-Time Communication

  • WebSocket endpoint at /ws broadcasts domain events to connected clients
  • Event types: scan_started, scan_stopped, device_discovered
  • Web UI: AlpineJS frontend with TailwindCSS for real-time device updates

Key Technical Details

Dependencies & Requirements

  • Python 3.13+ required (specified in pyproject.toml)
  • Poetry for dependency management (not pip)
  • Bleak library for cross-platform BLE support (Windows/macOS/Linux)
  • FastAPI with uvicorn for async web server

Configuration

  • Settings managed via src/blescope/shared/infrastructure/config.py
  • Environment variables supported via .env file
  • CORS, logging, timeouts configurable

Singleton Pattern

  • Core services are singletons via @lru_cache() in dependencies.py
  • EventBus, Scanner, Repositories shared across application

Cross-Platform BLE Support

  • Windows: WinRT backend
  • macOS: Core Bluetooth backend
  • Linux: BlueZ backend

Important Development Notes

Dependency Injection

All dependencies wired in src/blescope/api/dependencies.py. Core services are singletons:

  • EventBus for event-driven communication
  • BluetoothScanner for BLE operations
  • Repositories for data persistence

Event-Driven Architecture

  • Domain events automatically broadcast to WebSocket clients
  • WebSocketManager subscribes to EventBus for real-time updates
  • Decoupled communication between layers

Application Lifecycle

  • Graceful startup/shutdown in src/main.py
  • BLE scanner properly stopped on application shutdown
  • Thread-safe background task management

File Structure Navigation

  • Main application entry: src/main.py
  • FastAPI app factory: src/blescope/api/app.py
  • Domain logic: src/blescope/scanning/domain/
  • Business logic: src/blescope/scanning/application/
  • External adapters: src/blescope/scanning/infrastructure/
  • Configuration: src/blescope/shared/infrastructure/config.py

Web Interface

  • Static files served from static/ directory
  • AlpineJS-based frontend with real-time WebSocket updates
  • TailwindCSS for styling
  • Device filtering by name and RSSI signal strength

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blescope-0.0.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blescope-0.0.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file blescope-0.0.0.tar.gz.

File metadata

  • Download URL: blescope-0.0.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.2 Darwin/23.6.0

File hashes

Hashes for blescope-0.0.0.tar.gz
Algorithm Hash digest
SHA256 fe00bd43aa5c42875677164bf228c6a8fe080cd38179723c0d7f6318910f1b9e
MD5 e6192e48bb209070a7a9f960f8bf4ab8
BLAKE2b-256 0af7e8259e795d4068beb1a7b0c0e8dd82c2d0553eb9e8138684c5166b665907

See more details on using hashes here.

File details

Details for the file blescope-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: blescope-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.2 Darwin/23.6.0

File hashes

Hashes for blescope-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 86fc085d2983906dbf9df7aee48d6764a4dcef59badc7b10bab05ff3a142019b
MD5 779040742f227d47e02922f2d31ab147
BLAKE2b-256 8fb34bb345e7ff33fb182a806a0a271307578fd6fe79ab5f21a169c1e2712e58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page