88import getopt , json , shlex , re
99import logging
1010import requests
11+ from bleak import BleakScanner
1112from pathlib import Path
1213from prompt_toolkit .shortcuts import PromptSession
1314from prompt_toolkit .shortcuts import CompleteStyle
1415from prompt_toolkit .completion import NestedCompleter
1516from prompt_toolkit .history import FileHistory
1617from prompt_toolkit .formatted_text import ANSI
1718from prompt_toolkit .key_binding import KeyBindings
19+ from prompt_toolkit .shortcuts import radiolist_dialog
1820
1921from meshcore import TCPConnection , BLEConnection , SerialConnection
2022from 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