Martin Thoma
Martin Thoma

Reputation: 136187

How can I get the signal strength of WLANs with Python?

The following code gets all access points and displays their SSID as well as their Strength property. However, I have no idea how this property is encoded:

import NetworkManager
for dev in NetworkManager.NetworkManager.GetDevices():
    if dev.DeviceType != NetworkManager.NM_DEVICE_TYPE_WIFI:
        continue
    aps = [ap for ap in dev.SpecificDevice().GetAccessPoints()]
    for ap in sorted(aps, key=lambda ap: ap.Ssid):
        print(u"%s:: %s" % (ap.Ssid, ap.Strength.encode("unicode_escape")))

I get something like:

blabla1:: 2  <- The strongest signal
blabla2:: \x0c <- The weakest signal
DarkRoom:: \x13
FRITZ!Box 6340 Cable:: \n
FRITZ!Box 6360 Cable:: \x0e
FRITZ!Box 7272:: \x0c
FRITZ!Box Fon WLAN 7270:: \t
blabla3:: \x07
blabla4:: \x11
blabla5:: \x0c
blabla6:: (
blabla7:: \x07
blabla8:: \n
teco:: \n
wkit-802.1x:: \x14
blabla9:: \t

How do I get a meaningful value out of this?

Upvotes: 1

Views: 5780

Answers (1)

kasper Taeymans
kasper Taeymans

Reputation: 7026

Try updating your python-networkmanager code. Visiting it's github you can see that the code is updated 18 days ago with the note: " Decode signal strengths to integers"

you can see the code changes here: https://github.com/seveas/python-networkmanager/commit/079ebfef92dec3d8ad49d9a5df1e569c68e3355a

the code where you interested in is (byte to integer conversion):

struct.unpack('B', strength)[0]

Note that the result is a tuple, hence [0].more info about unpacking byte strings can be found in the python docs:

https://docs.python.org/2/library/struct.html

Upvotes: 3

Related Questions