scan_g1_devices
Discover and list available G1 Bluetooth devices by scanning for "G1_" named devices. Returns device details, addresses, and signal strength in a structured JSON response.
Instructions
Scan for available G1 devices.
Returns:
Dict[str, Any]: JSON response with scan results including:
- result: "success" or "error"
- devices: List of discovered devices with their properties
- count: Number of devices found
- error: Error message if scan failed
Note:
This performs an actual BLE scan for devices with names containing "G1_" pattern.
Returns a structured list of discovered devices with their addresses and signal strength.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- mcp_server.py:73-115 (handler)Main tool handler for scan_g1_devices. Performs BLE scan using NordicBLEUARTManager, handles exceptions, processes device list to infer side (right/left/unknown) from name patterns, and returns structured JSON with result, devices list, and count.@server.tool() async def scan_g1_devices() -> Dict[str, Any]: """Scan for available G1 devices. Returns: Dict[str, Any]: JSON response with scan results including: - result: "success" or "error" - devices: List of discovered devices with their properties - count: Number of devices found - error: Error message if scan failed Note: This performs an actual BLE scan for devices with names containing "G1_" pattern. Returns a structured list of discovered devices with their addresses and signal strength. """ try: devices = await ble_manager.scan_for_devices(filter_pattern="G1_") except Exception as e: logger.error(f"Scan failed: {e}") return { "result": "error", "error": str(e), "devices": [], "count": 0 } # Process devices to extract side information and format properly processed_devices = [] for device in devices: device_info = { "name": device['name'], "id": device['address'], "side": "right" if "_R_" in device['name'] else "left" if "_L_" in device['name'] else "unknown", "rssi": device.get('rssi') if device.get('rssi') != 'N/A' else None } processed_devices.append(device_info) return { "result": "success", "devices": processed_devices, "count": len(processed_devices) }
- mcp_server.py:73-73 (registration)FastMCP decorator that registers the scan_g1_devices function as a tool.@server.tool()
- g1_uart_manager.py:52-89 (helper)Core BLE scanning method in NordicBLEUARTManager class. Uses BleakScanner.discover with 10s timeout, filters by name pattern (default 'G1_'), extracts name/address/RSSI/metadata, stores BLEDevice objects, returns list of device dicts.async def scan_for_devices(self, filter_pattern: str = "G1_") -> List[Dict[str, Any]]: """Scan for BLE devices, optionally filtering by name pattern""" logger.info("Starting BLE device scan...") try: # Perform BLE scan - this is the only operation that could throw an exception devices = await BleakScanner.discover(timeout=10.0) except Exception as e: logger.error(f"Scan failed: {e}") return [] # Process scan results - this won't throw exceptions device_list = [] self.scanned_devices = [] # Clear previous scan results for device in devices: if not device.name: continue # Filter by pattern if specified if filter_pattern and filter_pattern not in device.name: continue # Store the actual BLEDevice object for reuse self.scanned_devices.append(device) device_info = { "name": device.name, "address": device.address, "rssi": getattr(device, 'rssi', 'N/A'), "metadata": getattr(device, 'metadata', {}) } device_list.append(device_info) logger.info(f"Found device: {device.name} ({device.address})") logger.info(f"Scan complete. Found {len(device_list)} devices") return device_list
- mcp_server.py:34-72 (helper)Helper function that scans for G1 devices and auto-connects to the first 'right' device (containing '_R_' in name). Used by other tools like send_g1_message.async def auto_connect_to_right_device(): """Automatically scan for and connect to the first right G1 device found""" try: logger.info("Auto-connecting to right G1 device...") # Scan for devices devices = await ble_manager.scan_for_devices(filter_pattern="G1_") if not devices: logger.warning("No G1 devices found during auto-connect") return False # Find the first device with "_R_" in the name (right device) right_device = None for device in devices: if "_R_" in device['name']: right_device = device break if not right_device: logger.warning("No right G1 device found during auto-connect") return False logger.info(f"Found right G1 device: {right_device['name']} ({right_device['address']})") # Attempt to connect success = await ble_manager.connect_to_device(right_device['address']) if success: logger.info(f"Auto-connect successful to {right_device['name']}") return True else: logger.warning(f"Auto-connect failed to {right_device['name']}") return False except Exception as e: logger.error(f"Auto-connect failed: {e}") return False
- mcp_server.py:75-87 (schema)Docstring defining the tool's input (none) and output schema: JSON dict with 'result', 'devices' (list of dicts with name/id/side/rssi), 'count', or 'error'."""Scan for available G1 devices. Returns: Dict[str, Any]: JSON response with scan results including: - result: "success" or "error" - devices: List of discovered devices with their properties - count: Number of devices found - error: Error message if scan failed Note: This performs an actual BLE scan for devices with names containing "G1_" pattern. Returns a structured list of discovered devices with their addresses and signal strength. """