Skip to content

Commit 093a2a7

Browse files
committed
ble device selector
1 parent 3345e41 commit 093a2a7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "meshcore-cli"
7-
version = "0.6.15"
7+
version = "0.6.16"
88
authors = [
99
{ name="Florent de Lamotte", email="[email protected]" },
1010
]

src/meshcore_cli/meshcore_cli.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import getopt, json, shlex, re
99
import logging
1010
import requests
11+
from bleak import BleakScanner
1112
from pathlib import Path
1213
from prompt_toolkit.shortcuts import PromptSession
1314
from prompt_toolkit.shortcuts import CompleteStyle
1415
from prompt_toolkit.completion import NestedCompleter
1516
from prompt_toolkit.history import FileHistory
1617
from prompt_toolkit.formatted_text import ANSI
1718
from prompt_toolkit.key_binding import KeyBindings
19+
from prompt_toolkit.shortcuts import radiolist_dialog
1820

1921
from meshcore import TCPConnection, BLEConnection, SerialConnection
2022
from meshcore import MeshCore, EventType, logger
@@ -1265,6 +1267,8 @@ def usage () :
12651267
Arguments :
12661268
-h : prints this help
12671269
-j : json output
1270+
-D : debug
1271+
-S : performs a ble scan and ask for device
12681272
-a <address> : specifies device address (can be a name)
12691273
-d <name> : filter meshcore devices with name or address
12701274
-t <hostname> : connects via tcp/ip
@@ -1290,7 +1294,7 @@ async def main(argv):
12901294
with open(MCCLI_ADDRESS, encoding="utf-8") as f :
12911295
address = f.readline().strip()
12921296

1293-
opts, args = getopt.getopt(argv, "a:d:s:ht:p:b:jDh")
1297+
opts, args = getopt.getopt(argv, "a:d:s:ht:p:b:jDhS")
12941298
for opt, arg in opts :
12951299
match opt:
12961300
case "-d" : # name specified on cmdline
@@ -1313,6 +1317,27 @@ async def main(argv):
13131317
case "-h" :
13141318
usage()
13151319
return
1320+
case "-S" :
1321+
devices = await BleakScanner.discover()
1322+
choices = []
1323+
for d in devices:
1324+
if d.name.startswith("MeshCore-"):
1325+
choices.append((d.address, f"{d.address} {d.name}"))
1326+
if len(choices) == 0:
1327+
logger.error("No BLE device found, exiting")
1328+
return
1329+
1330+
result = await radiolist_dialog(
1331+
title="MeshCore-cli BLE device selector",
1332+
text="Chose the device to connect to :",
1333+
values=choices
1334+
).run_async()
1335+
1336+
if result is None:
1337+
logger.info("No choice made, exiting")
1338+
return
1339+
1340+
address = result
13161341

13171342
if (debug==True):
13181343
logger.setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)