API Docs

Control and query Wi-Fi interfaces, scan for networks, and watch for Wi-Fi events.

Built on CoreWLAN. Fields that reveal network identity — ssid, bssid, countryCode, and BSSIDs inside scan results — are only populated once Location Services is enabled and the user has authorized this app; call hs.permissions.requestLocation() first (the same gate hs.location uses). Without authorization these fields are null/omitted, not errors.

Watching for Wi-Fi events

hs.wifi.on('ssidChange', info => console.log("SSID changed on " + info.interface))

Properties

This module has no properties.

Methods

hs.wifi.interfaces() -> string[]

Returns the names of all Wi-Fi interfaces attached to the system (e.g. ["en0"]).

hs.wifi.interfaces() -> string[]
string[]
an array of interface name strings
console.log(hs.wifi.interfaces())

hs.wifi.interfaceDetails(interface) -> [String: Any]

Returns detailed information about a Wi-Fi interface.

This is safe to call frequently. The interface's cached scan results and its configuration are available from cachedScanResults() and configuration(), which are kept separate because fetching them can leak memory inside macOS.

active reports whether the interface is in use, and does not need Location Services authorization, so it's a way to check that Wi-Fi is connected without that permission.

hs.wifi.interfaceDetails(interface) -> [String: Any]
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
[String: Any]
a table with keys interface, active, power, ssid, bssid, security, interfaceMode, activePHYMode, rssi, noise, transmitRate, transmitPower, countryCode, hardwareAddress, wlanChannel, supportedChannels; or null if the interface doesn't exist. ssid/bssid/countryCode/wlanChannel may be absent without Location Services authorization.
const info = hs.wifi.interfaceDetails()
if (info) console.log(info.ssid + " on channel " + info.wlanChannel.number)

hs.wifi.cachedScanResults(interface) -> [[String: Any]]

Returns the networks an interface found in its most recent scan, without starting a new scan.

Avoid calling this frequently: it has been seen to leak memory each time macOS refreshes the scan cache, so polling it in a long-running configuration can grow Hammerspoon's memory use. Use scanNetworks() for a fresh scan, or on('scanCacheUpdated', ...) to learn when the cache changes.

hs.wifi.cachedScanResults(interface) -> [[String: Any]]
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
[[String: Any]]
an array of network tables, with the same keys as scanNetworks(); or null if the interface doesn't exist
const networks = hs.wifi.cachedScanResults() ?? []
networks.forEach(n => console.log(n.ssid + " (" + n.rssi + " dBm)"))

hs.wifi.configuration(interface) -> [String: Any]

Returns an interface's configuration: its remembered network profiles and administrator settings.

Avoid calling this frequently: macOS leaks memory on every call, so polling this function in a long-running configuration grows Hammerspoon's memory use.

hs.wifi.configuration(interface) -> [String: Any]
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
[String: Any]
a table with keys rememberJoinedNetworks, requireAdministratorForAssociation, requireAdministratorForIBSSMode, requireAdministratorForPower, networkProfiles, where networkProfiles is an array of tables with keys ssid, security; or null if the interface doesn't exist. Profile ssids may be absent without Location Services authorization.
const config = hs.wifi.configuration()
if (config) console.log(config.networkProfiles.length + " remembered networks")

hs.wifi.currentNetwork(interface) -> string

Returns the SSID of the network currently joined on an interface.

hs.wifi.currentNetwork(interface) -> string
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
string
the SSID string, or null if not joined to a network (or Location Services is not authorized)
console.log(hs.wifi.currentNetwork())

hs.wifi.setPower(state, interface) -> boolean

Turns a Wi-Fi interface on or off.

hs.wifi.setPower(state, interface) -> boolean
Name Type Description
state boolean true to power the interface on, false to power it off
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
boolean
true if the power state was changed successfully, false if the interface doesn't exist or the change failed (see the Console for the reason)
hs.wifi.setPower(false) // turn Wi-Fi off

hs.wifi.disassociate(interface) -> None

Disconnects an interface from its current network.

hs.wifi.disassociate(interface) -> None
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
None
hs.wifi.disassociate()

hs.wifi.associate(ssid, passphrase, interface) -> Promise<boolean>

Scans for a network by SSID and joins it. Enterprise networks are not supported.

This can take several seconds; it runs off the main thread so it does not block the app.

hs.wifi.associate(ssid, passphrase, interface) -> Promise<boolean>
Name Type Description
ssid string the SSID of the network to join
passphrase string the network passphrase; required for WEP/WPA/WPA2/WPA3 Personal networks
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
Promise<boolean>
A Promise that resolves true if joined successfully, false if no network with that SSID was found, or rejects if the interface doesn't exist or the association attempt fails
hs.wifi.associate("MyNetwork", "hunter2")
    .then(joined => console.log(joined ? "Joined" : "Network not found"))
    .catch(err => console.log("Error: " + err))

hs.wifi.scanNetworks(interface) -> Promise<object[]>

Scans for visible Wi-Fi networks.

This can take a few seconds; it runs off the main thread so it does not block the app.

hs.wifi.scanNetworks(interface) -> Promise<object[]>
Name Type Description
interface string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
Promise<object[]>
A Promise that resolves to an array of network tables, each with keys ssid, bssid, rssi, noise, ibss, countryCode, beaconInterval, security, phyModes, wlanChannel, informationElementData; or rejects if the interface doesn't exist or the scan fails. bssid/countryCode may be absent without Location Services authorization. informationElementData is raw beacon/probe-response data returned as an array of byte values (0-255) rather than a string, since it can contain sequences that are unsafe to render as text.
hs.wifi.scanNetworks().then(networks => {
    networks.forEach(n => console.log(n.ssid + " (" + n.rssi + " dBm)"))
})

hs.wifi.on(event, listener) -> None

Register a listener for a Wi-Fi interface event. Info keys by event:

  • "ssidChange", "bssidChange", "countryCodeChange", "linkChange", "modeChange", "powerChange", "scanCacheUpdated": interface: string
  • "linkQualityChange": interface: string, rssi: number, transmitRate: number
hs.wifi.on(event, listener) -> None
Name Type Description
event any The event to listen for
listener any Called with an info dictionary describing the event; see the keys listed above
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.wifi.on('ssidChange', info => console.log("SSID changed on " + info.interface))
    hs.wifi.on('linkQualityChange', info => console.log(info.interface + ": " + info.rssi + " dBm"))
} catch (err) {
    console.error(err.message)
}

hs.wifi.off(event, listener) -> None

Remove a previously registered Wi-Fi event listener.

hs.wifi.off(event, listener) -> None
Name Type Description
event any The event the listener was registered for
listener any The function originally passed to on
None
const onSsidChange = info => console.log(info)
hs.wifi.on('ssidChange', onSsidChange)
// later…
hs.wifi.off('ssidChange', onSsidChange)

hs.wifi.once(event, listener) -> None

Register a listener that fires at most once for a Wi-Fi interface event.

hs.wifi.once(event, listener) -> None
Name Type Description
event any The event to listen for
listener any Called once, then automatically removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.wifi.once('ssidChange', info => console.log("First SSID change: " + info.interface))
} catch (err) {
    console.error(err.message)
}