如何用DBus从NetworkManager获取可见SSID?nmcli与dbus-send结果差异解惑
dbus-send Returns Fewer APs Than nmcli device wifi list Let's break down the key differences between what nmcli does and your direct dbus-send call, which explains the inconsistent AP counts you're seeing:
1. nmcli Triggers an Active Scan First
The org.freedesktop.NetworkManager.Device.Wireless.GetAllAccessPoints method you're calling with dbus-send only returns cached results from the last wireless scan. It doesn't initiate a new scan on its own.
On the other hand, nmcli device wifi list automatically does two things:
- First, it sends a request to NetworkManager to start a fresh wireless scan via the
org.freedesktop.NetworkManager.Device.Wireless.RequestScanD-Bus method. - It waits for the scan to complete (or times out after a short period) before fetching the updated list of APs.
That's why after running nmcli, your subsequent dbus-send calls return more APs—nmcli just populated the cache with fresh scan data.
2. nmcli Handles Scan Restrictions & Parameters
NetworkManager has built-in limits to prevent excessive wireless scanning (to save battery and reduce network noise). For example, it might reject repeated scan requests within a short window. nmcli handles these edge cases:
- It checks if a recent scan already exists, and only triggers a new one if needed.
- It uses appropriate scan parameters (like allowing full band scans) that might not be enabled by default when you call
GetAllAccessPointsdirectly.
In your work environment, the cached scan results likely only ever included that single AP (maybe the last scan was triggered ages ago, or restricted to a specific band), while nmcli forces a full scan every time, pulling in all 12+ APs.
How to Get Full AP Results with dbus-send
To replicate what nmcli does, you need to explicitly trigger a scan before fetching the APs. Run these two commands in sequence:
Step 1: Initiate a wireless scan
dbus-send --system --print-reply --dest=org.freedesktop.NetworkManager \ /org/freedesktop/NetworkManager/Devices/4 \ org.freedesktop.NetworkManager.Device.Wireless.RequestScan \ dict:string:string:"timeout", "10"
The timeout parameter tells NetworkManager to run the scan for up to 10 seconds (adjust as needed).
Step 2: Fetch the updated AP list
dbus-send --system --print-reply --dest=org.freedesktop.NetworkManager \ /org/freedesktop/NetworkManager/Devices/4 \ org.freedesktop.NetworkManager.Device.Wireless.GetAllAccessPoints
Note: If you run the scan command too frequently, NetworkManager might reject the request. Wait a minute or two between repeated scans, just like nmcli does.
内容的提问来源于stack exchange,提问作者Michael restore Monica Cellio

