From 093a2a7c2cb1e1bc317b2a918751d3debf18a0d0 Mon Sep 17 00:00:00 2001 From: Florent de Lamotte Date: Thu, 24 Apr 2025 14:06:23 +0200 Subject: [PATCH] ble device selector --- pyproject.toml | 2 +- src/meshcore_cli/meshcore_cli.py | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 05a081b..1da0008 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "meshcore-cli" -version = "0.6.15" +version = "0.6.16" authors = [ { name="Florent de Lamotte", email="florent@frizoncorrea.fr" }, ] diff --git a/src/meshcore_cli/meshcore_cli.py b/src/meshcore_cli/meshcore_cli.py index 592503e..02136bd 100644 --- a/src/meshcore_cli/meshcore_cli.py +++ b/src/meshcore_cli/meshcore_cli.py @@ -8,6 +8,7 @@ import time, datetime import getopt, json, shlex, re import logging import requests +from bleak import BleakScanner from pathlib import Path from prompt_toolkit.shortcuts import PromptSession from prompt_toolkit.shortcuts import CompleteStyle @@ -15,6 +16,7 @@ from prompt_toolkit.completion import NestedCompleter from prompt_toolkit.history import FileHistory from prompt_toolkit.formatted_text import ANSI from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.shortcuts import radiolist_dialog from meshcore import TCPConnection, BLEConnection, SerialConnection from meshcore import MeshCore, EventType, logger @@ -1265,6 +1267,8 @@ def usage () : Arguments : -h : prints this help -j : json output + -D : debug + -S : performs a ble scan and ask for device -a
: specifies device address (can be a name) -d : filter meshcore devices with name or address -t : connects via tcp/ip @@ -1290,7 +1294,7 @@ async def main(argv): with open(MCCLI_ADDRESS, encoding="utf-8") as f : address = f.readline().strip() - opts, args = getopt.getopt(argv, "a:d:s:ht:p:b:jDh") + opts, args = getopt.getopt(argv, "a:d:s:ht:p:b:jDhS") for opt, arg in opts : match opt: case "-d" : # name specified on cmdline @@ -1313,6 +1317,27 @@ async def main(argv): case "-h" : usage() return + case "-S" : + devices = await BleakScanner.discover() + choices = [] + for d in devices: + if d.name.startswith("MeshCore-"): + choices.append((d.address, f"{d.address} {d.name}")) + if len(choices) == 0: + logger.error("No BLE device found, exiting") + return + + result = await radiolist_dialog( + title="MeshCore-cli BLE device selector", + text="Chose the device to connect to :", + values=choices + ).run_async() + + if result is None: + logger.info("No choice made, exiting") + return + + address = result if (debug==True): logger.setLevel(logging.DEBUG)