如何使用 Python 在 Windows 上检索附近无线 LAN 网络的信号强度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2851233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:28:42  来源:igfitidea点击:

How can I retrieve the signal strength of nearby wireless LAN networks on Windows using Python?

pythonwindowssignalswirelesswlan

提问by kame

How can I retrieve the signal strength of nearby wireless LAN networks on Windows using Python?

如何使用 Python 在 Windows 上检索附近无线 LAN 网络的信号强度?

I would like to either show or graph the values.

我想显示或绘制这些值。

回答by fmark

If you are on Windows, you probably want to use the WLAN API, which provides the 'WlanGetAvailableNetworkList()' function (see the API docsfor usage). I am not aware of any python wrappers for WLANAPI.DLLso you may have to wrap it yourself using ctypes. I have a preliminary script that does this (works-for-me), but it may be crufty. You'll want to read the documentation to understand the meaning of all the fields:

如果您使用的是Windows,您可能想要使用 WLAN API,它提供了“WlanGetAvailableNetworkList()”函数(有关用法,请参阅API 文档)。我不知道有任何 python 包装器,WLANAPI.DLL因此您可能必须使用ctypes自己包装它。我有一个执行此操作的初步脚本(对我来说有效),但它可能很粗糙。您需要阅读文档以了解所有字段的含义:

from ctypes import *
from ctypes.wintypes import *
from sys import exit


def customresize(array, new_size):
    return (array._type_*new_size).from_address(addressof(array))

wlanapi = windll.LoadLibrary('wlanapi.dll')

ERROR_SUCCESS = 0

class GUID(Structure):
    _fields_ = [
        ('Data1', c_ulong),
        ('Data2', c_ushort),
        ('Data3', c_ushort),
        ('Data4', c_ubyte*8),
        ]

WLAN_INTERFACE_STATE = c_uint
(wlan_interface_state_not_ready,
 wlan_interface_state_connected,
 wlan_interface_state_ad_hoc_network_formed,
 wlan_interface_state_disconnecting,
 wlan_interface_state_disconnected,
 wlan_interface_state_associating,
 wlan_interface_state_discovering,
 wlan_interface_state_authenticating) = map(WLAN_INTERFACE_STATE, range(0, 8))

class WLAN_INTERFACE_INFO(Structure):
    _fields_ = [
        ("InterfaceGuid", GUID),
        ("strInterfaceDescription", c_wchar * 256),
        ("isState", WLAN_INTERFACE_STATE)
        ]

class WLAN_INTERFACE_INFO_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("InterfaceInfo", WLAN_INTERFACE_INFO * 1)
        ]

WLAN_MAX_PHY_TYPE_NUMBER = 0x8
DOT11_SSID_MAX_LENGTH = 32
WLAN_REASON_CODE = DWORD
DOT11_BSS_TYPE = c_uint
(dot11_BSS_type_infrastructure,
 dot11_BSS_type_independent,
 dot11_BSS_type_any) = map(DOT11_BSS_TYPE, range(1, 4))
DOT11_PHY_TYPE = c_uint
dot11_phy_type_unknown      = 0
dot11_phy_type_any          = 0
dot11_phy_type_fhss         = 1
dot11_phy_type_dsss         = 2
dot11_phy_type_irbaseband   = 3
dot11_phy_type_ofdm         = 4
dot11_phy_type_hrdsss       = 5
dot11_phy_type_erp          = 6
dot11_phy_type_ht           = 7
dot11_phy_type_IHV_start    = 0x80000000
dot11_phy_type_IHV_end      = 0xffffffff 

DOT11_AUTH_ALGORITHM = c_uint
DOT11_AUTH_ALGO_80211_OPEN         = 1
DOT11_AUTH_ALGO_80211_SHARED_KEY   = 2
DOT11_AUTH_ALGO_WPA                = 3
DOT11_AUTH_ALGO_WPA_PSK            = 4
DOT11_AUTH_ALGO_WPA_NONE           = 5
DOT11_AUTH_ALGO_RSNA               = 6
DOT11_AUTH_ALGO_RSNA_PSK           = 7
DOT11_AUTH_ALGO_IHV_START          = 0x80000000
DOT11_AUTH_ALGO_IHV_END            = 0xffffffff

DOT11_CIPHER_ALGORITHM = c_uint
DOT11_CIPHER_ALGO_NONE            = 0x00
DOT11_CIPHER_ALGO_WEP40           = 0x01
DOT11_CIPHER_ALGO_TKIP            = 0x02
DOT11_CIPHER_ALGO_CCMP            = 0x04
DOT11_CIPHER_ALGO_WEP104          = 0x05
DOT11_CIPHER_ALGO_WPA_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_RSN_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_WEP             = 0x101
DOT11_CIPHER_ALGO_IHV_START       = 0x80000000
DOT11_CIPHER_ALGO_IHV_END         = 0xffffffff 

WLAN_AVAILABLE_NETWORK_CONNECTED = 1
WLAN_AVAILABLE_NETWORK_HAS_PROFILE = 2

WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES = 0x00000001
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES = 0x00000002

class DOT11_SSID(Structure):
    _fields_ = [
        ("SSIDLength", c_ulong),
        ("SSID", c_char * DOT11_SSID_MAX_LENGTH)
        ]

class WLAN_AVAILABLE_NETWORK(Structure):
    _fields_ = [
        ("ProfileName", c_wchar * 256),
        ("dot11Ssid", DOT11_SSID),
        ("dot11BssType", DOT11_BSS_TYPE),
        ("NumberOfBssids", c_ulong),
        ("NetworkConnectable", c_bool),
        ("wlanNotConnectableReason", WLAN_REASON_CODE),
        ("NumberOfPhyTypes", c_ulong),
        ("dot11PhyTypes", DOT11_PHY_TYPE * WLAN_MAX_PHY_TYPE_NUMBER),
        ("MorePhyTypes", c_bool),
        ("wlanSignalQuality", c_ulong),
        ("SecurityEnabled", c_bool),
        ("dot11DefaultAuthAlgorithm", DOT11_AUTH_ALGORITHM),
        ("dot11DefaultCipherAlgorithm", DOT11_CIPHER_ALGORITHM),
        ("Flags", DWORD),
        ("Reserved", DWORD)
        ]

class WLAN_AVAILABLE_NETWORK_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("Network", WLAN_AVAILABLE_NETWORK * 1)
        ]

WlanOpenHandle = wlanapi.WlanOpenHandle
WlanOpenHandle.argtypes = (DWORD, c_void_p, POINTER(DWORD), POINTER(HANDLE))
WlanOpenHandle.restype = DWORD

WlanEnumInterfaces = wlanapi.WlanEnumInterfaces
WlanEnumInterfaces.argtypes = (HANDLE, c_void_p, 
                               POINTER(POINTER(WLAN_INTERFACE_INFO_LIST)))
WlanEnumInterfaces.restype = DWORD

WlanGetAvailableNetworkList = wlanapi.WlanGetAvailableNetworkList
WlanGetAvailableNetworkList.argtypes = (HANDLE, POINTER(GUID), DWORD, c_void_p, 
                                        POINTER(POINTER(WLAN_AVAILABLE_NETWORK_LIST)))
WlanGetAvailableNetworkList.restype = DWORD

WlanFreeMemory = wlanapi.WlanFreeMemory
WlanFreeMemory.argtypes = [c_void_p]


if __name__ == '__main__':
    NegotiatedVersion = DWORD()
    ClientHandle = HANDLE()
    ret = WlanOpenHandle(1, None, byref(NegotiatedVersion), byref(ClientHandle))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))

    # find all wireless network interfaces
    pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
    ret = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))

    try:
        ifaces = customresize(pInterfaceList.contents.InterfaceInfo,
                              pInterfaceList.contents.NumberOfItems)
        # find each available network for each interface
        for iface in ifaces:
            print("Interface: {}".format(iface.strInterfaceDescription))
            pAvailableNetworkList = pointer(WLAN_AVAILABLE_NETWORK_LIST())
            ret = WlanGetAvailableNetworkList(ClientHandle, 
                                        byref(iface.InterfaceGuid),
                                        0,
                                        None,
                                        byref(pAvailableNetworkList))
            if ret != ERROR_SUCCESS:
                exit(FormatError(ret))
            try:
                avail_net_list = pAvailableNetworkList.contents
                networks = customresize(avail_net_list.Network, 
                                        avail_net_list.NumberOfItems)
                for network in networks:
                    print("SSID: {}, quality: {:2d}%".format(
                        network.dot11Ssid.SSID[:network.dot11Ssid.SSIDLength].decode(),
                        network.wlanSignalQuality))
            finally:
                WlanFreeMemory(pAvailableNetworkList)
    finally:
        WlanFreeMemory(pInterfaceList)

On linux, you have a couple of choices:

linux 上,您有几个选择:

The standard options is to parse the output of iwlist -scan. However, if you are currently connected to a wlan and not running as root, it only returns the currently connected wlan.

标准选项是解析iwlist -scan. 但是,如果您当前连接到 wlan 并且未以 root 身份运行,则它只会返回当前连接的 wlan。

If you need more than this, the best way is to query the wireless manager daemon. On modern, linuxes, this is usually NetworkManager, although wicdis becoming more popular. Both of these managers can be queried using dbus. Unless you can control what the clients have on their systems, you might need to support both of these options, or at least one option and have a fallback for iwlist.

如果您需要更多,最好的方法是查询无线管理器守护程序。在现代的 linux 上,这通常是NetworkManager,尽管wicd变得越来越流行。可以使用 查询这两个管理器dbus。除非您可以控制客户端在其系统上拥有的内容,否则您可能需要同时支持这两个选项,或至少支持一个选项,并有一个后备iwlist.

回答by gmag11

If you don't want to deal with Windows API you could use method shown herewith a slight modification.

如果您不想处理 Windows API,您可以使用此处显示的方法稍加修改。

You can use this command:

你可以使用这个命令:

netsh wlan show networks mode=Bssid

using this code

使用此代码

import subprocess
results = subprocess.check_output(["netsh", "wlan", "show", "network", "mode=Bssid"])

This will give you additiona "Signal" information as a percentage. This can be converted to RSSI using this method.

这将为您提供额外的“信号”信息作为百分比。可以使用此方法将其转换为 RSSI 。

回答by Joshua

Well you could invoke the system command to get that info (Linux = iwlist), parse the result, and display it.

好吧,您可以调用系统命令来获取该信息(Linux = iwlist),解析结果并显示它。

回答by millertime

@fmark I just wanted to say THANK YOU so much for this awesome post. At my work I have been trying to get RSSI from the connected AP in Windows, and you deserve many thanks for me finally getting it done. I'm currently listing all APs, but I'm working on modifying it. I wanted to offer a recommendation towards your code. I'm not very experienced with C or the Python CTypes module, but I think I might have found a possible bug. I'm really not sure what difference it makes, but I noticed:

@fmark 我只想对这篇很棒的帖子说声谢谢。在我的工作中,我一直试图从 Windows 中连接的 AP 获取 RSSI,您应该非常感谢我终于完成了它。我目前正在列出所有 AP,但我正在修改它。我想对您的代码提出建议。我对 C 或 Python CTypes 模块不是很有经验,但我想我可能发现了一个可能的错误。我真的不确定它有什么不同,但我注意到:

You define enums like this:

您可以这样定义枚举:

DOT11_BSS_TYPE = c_uint
(dot11_BSS_type_infrastructure,
 dot11_BSS_type_independent,
 dot11_BSS_type_any) = map(DOT11_BSS_TYPE, xrange(1, 4))

But then other times you define something very similar like this:

但是其他时候你定义的东西非常相似:

DOT11_PHY_TYPE = c_uint
dot11_phy_type_unknown      = 0
dot11_phy_type_any          = 0
dot11_phy_type_fhss         = 1
dot11_phy_type_dsss         = 2
dot11_phy_type_irbaseband   = 3
dot11_phy_type_ofdm         = 4
dot11_phy_type_hrdsss       = 5
dot11_phy_type_erp          = 6
dot11_phy_type_ht           = 7
dot11_phy_type_IHV_start    = 0x80000000
dot11_phy_type_IHV_end      = 0xffffffff 

I think the second snippit should be modeled like the first, but I could be wrong. Here was my idea:

我认为第二个 snippit 应该像第一个一样建模,但我可能是错的。这是我的想法:

DOT11_PHY_TYPE = c_uint
(dot11_phy_type_unknown,
 dot11_phy_type_any,
 dot11_phy_type_fhss,
 dot11_phy_type_dsss,
 dot11_phy_type_irbaseband,
 dot11_phy_type_ofdm,
 dot11_phy_type_hrdsss,
 dot11_phy_type_erp,
 dot11_phy_type_ht,
 dot11_phy_type_IHV_start,
 dot11_phy_type_IHV_end) = map(DOT11_PHY_TYPE,
                               [0,0,1,2,3,4,
                                5,6,7,0x80000000,
                                0xffffffff])

This way those values would get correctly mapped to DOT11_PHY_TYPE. Perhaps I am completely wrong, but for future folks like myself, I just wanted whatever is stumbled on here to be correct :) Thanks again, @fmark.

这样,这些值将正确映射到 DOT11_PHY_TYPE。也许我完全错了,但对于像我这样的未来人,我只是希望在这里偶然发现的任何东西都是正确的:) 再次感谢,@fmark。

回答by Pedro G.

So.. I based myself on @fmarks code (which was a live-safer for me), and you should add the WlanScan function, or else the RSSI values will not refresh.

所以..我基于@fmarks 代码(这对我来说更安全),你应该添加WlanScan 函数,否则 RSSI 值不会刷新

Here is my extended code!

这是我的扩展代码!

All props to @fmarks!

@fmarks 的所有道具!

__author__ = 'Pedro Gomes'

import time
from ctypes import *
from ctypes.wintypes import *
from sys import exit

def customresize(array, new_size):
    return (array._type_*new_size).from_address(addressof(array))

wlanapi = windll.LoadLibrary('wlanapi.dll')

ERROR_SUCCESS = 0

class GUID(Structure):
    _fields_ = [
        ('Data1', c_ulong),
        ('Data2', c_ushort),
        ('Data3', c_ushort),
        ('Data4', c_ubyte*8),
        ]

WLAN_INTERFACE_STATE = c_uint
(wlan_interface_state_not_ready,
 wlan_interface_state_connected,
 wlan_interface_state_ad_hoc_network_formed,
 wlan_interface_state_disconnecting,
 wlan_interface_state_disconnected,
 wlan_interface_state_associating,
 wlan_interface_state_discovering,
 wlan_interface_state_authenticating) = map(WLAN_INTERFACE_STATE, xrange(0, 8))

class WLAN_INTERFACE_INFO(Structure):
    _fields_ = [
        ("InterfaceGuid", GUID),
        ("strInterfaceDescription", c_wchar * 256),
        ("isState", WLAN_INTERFACE_STATE)
    ]

class WLAN_INTERFACE_INFO_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("InterfaceInfo", WLAN_INTERFACE_INFO * 1)
    ]

WLAN_MAX_PHY_TYPE_NUMBER = 0x8
DOT11_SSID_MAX_LENGTH = 32
WLAN_REASON_CODE = DWORD

DOT11_BSS_TYPE = c_uint
(dot11_BSS_type_infrastructure,
 dot11_BSS_type_independent,
 dot11_BSS_type_any) = map(DOT11_BSS_TYPE, xrange(1, 4))

DOT11_PHY_TYPE = c_uint
dot11_phy_type_unknown      = 0
dot11_phy_type_any          = 0
dot11_phy_type_fhss         = 1
dot11_phy_type_dsss         = 2
dot11_phy_type_irbaseband   = 3
dot11_phy_type_ofdm         = 4
dot11_phy_type_hrdsss       = 5
dot11_phy_type_erp          = 6
dot11_phy_type_ht           = 7
dot11_phy_type_IHV_start    = 0x80000000
dot11_phy_type_IHV_end      = 0xffffffff

DOT11_AUTH_ALGORITHM = c_uint
DOT11_AUTH_ALGO_80211_OPEN         = 1
DOT11_AUTH_ALGO_80211_SHARED_KEY   = 2
DOT11_AUTH_ALGO_WPA                = 3
DOT11_AUTH_ALGO_WPA_PSK            = 4
DOT11_AUTH_ALGO_WPA_NONE           = 5
DOT11_AUTH_ALGO_RSNA               = 6
DOT11_AUTH_ALGO_RSNA_PSK           = 7
DOT11_AUTH_ALGO_IHV_START          = 0x80000000
DOT11_AUTH_ALGO_IHV_END            = 0xffffffff

DOT11_CIPHER_ALGORITHM = c_uint
DOT11_CIPHER_ALGO_NONE            = 0x00
DOT11_CIPHER_ALGO_WEP40           = 0x01
DOT11_CIPHER_ALGO_TKIP            = 0x02
DOT11_CIPHER_ALGO_CCMP            = 0x04
DOT11_CIPHER_ALGO_WEP104          = 0x05
DOT11_CIPHER_ALGO_WPA_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_RSN_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_WEP             = 0x101
DOT11_CIPHER_ALGO_IHV_START       = 0x80000000
DOT11_CIPHER_ALGO_IHV_END         = 0xffffffff

WLAN_AVAILABLE_NETWORK_CONNECTED = 1
WLAN_AVAILABLE_NETWORK_HAS_PROFILE = 2

WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES = 0x00000001
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES = 0x00000002

class DOT11_SSID(Structure):
    _fields_ = [
        ("SSIDLength", c_ulong),
        ("SSID", c_char * DOT11_SSID_MAX_LENGTH)
    ]


class WLAN_AVAILABLE_NETWORK(Structure):
    _fields_ = [
        ("ProfileName", c_wchar * 256),
        ("dot11Ssid", DOT11_SSID),
        ("dot11BssType", DOT11_BSS_TYPE),
        ("NumberOfBssids", c_ulong),
        ("NetworkConnectable", c_bool),
        ("wlanNotConnectableReason", WLAN_REASON_CODE),
        ("NumberOfPhyTypes", c_ulong),
        ("dot11PhyTypes", DOT11_PHY_TYPE * WLAN_MAX_PHY_TYPE_NUMBER),
        ("MorePhyTypes", c_bool),
        ("wlanSignalQuality", c_ulong),
        ("SecurityEnabled", c_bool),
        ("dot11DefaultAuthAlgorithm", DOT11_AUTH_ALGORITHM),
        ("dot11DefaultCipherAlgorithm", DOT11_CIPHER_ALGORITHM),
        ("Flags", DWORD),
        ("Reserved", DWORD)
    ]

class WLAN_AVAILABLE_NETWORK_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("Network", WLAN_AVAILABLE_NETWORK * 1)
    ]


DOT11_MAC_ADDRESS = c_ubyte * 6

DOT11_CIPHER_ALGORITHM = c_uint
DOT11_CIPHER_ALGO_NONE            = 0x00
DOT11_CIPHER_ALGO_WEP40           = 0x01
DOT11_CIPHER_ALGO_TKIP            = 0x02

DOT11_PHY_TYPE = c_uint
DOT11_PHY_TYPE_UNKNOWN  = 0
DOT11_PHY_TYPE_ANY         = 0
DOT11_PHY_TYPE_FHSS        = 1
DOT11_PHY_TYPE_DSSS        = 2
DOT11_PHY_TYPE_IRBASEBAND  = 3
DOT11_PHY_TYPE_OFDM        = 4
DOT11_PHY_TYPE_HRDSSS      = 5
DOT11_PHY_TYPE_ERP         = 6
DOT11_PHY_TYPE_HT          = 7
DOT11_PHY_TYPE_IHV_START   = 0X80000000
DOT11_PHY_TYPE_IHV_END     = 0XFFFFFFFF

class WLAN_RATE_SET(Structure):
    _fields_ = [
        ("uRateSetLength", c_ulong),
        ("usRateSet", c_ushort * 126)
    ]

class WLAN_BSS_ENTRY(Structure):
    _fields_ = [
        ("dot11Ssid",DOT11_SSID),
        ("uPhyId",c_ulong),
        ("dot11Bssid", DOT11_MAC_ADDRESS),
        ("dot11BssType", DOT11_BSS_TYPE),
        ("dot11BssPhyType", DOT11_PHY_TYPE),
        ("lRssi", c_long),
        ("uLinkQuality", c_ulong),
        ("bInRegDomain", c_bool),
        ("usBeaconPeriod",c_ushort),
        ("ullTimestamp", c_ulonglong),
        ("ullHostTimestamp",c_ulonglong),
        ("usCapabilityInformation",c_ushort),
        ("ulChCenterFrequency", c_ulong),
        ("wlanRateSet",WLAN_RATE_SET),
        ("ulIeOffset", c_ulong),
        ("ulIeSize", c_ulong)]

class WLAN_BSS_LIST(Structure):
    _fields_ = [
        ("TotalSize", DWORD),
        ("NumberOfItems", DWORD),
        ("NetworkBSS", WLAN_BSS_ENTRY * 1)
    ]

class WLAN_AVAILABLE_NETWORK_LIST_BSS(Structure):
    _fields_ = [
        ("TotalSize", DWORD),
        ("NumberOfItems", DWORD),
        ("Network", WLAN_BSS_ENTRY * 1)
    ]

WlanOpenHandle = wlanapi.WlanOpenHandle
WlanOpenHandle.argtypes = (DWORD, c_void_p, POINTER(DWORD), POINTER(HANDLE))
WlanOpenHandle.restype = DWORD

WlanCloseHandle = wlanapi.WlanCloseHandle
WlanCloseHandle.argtypes = (HANDLE, c_void_p)
WlanCloseHandle.restype = DWORD

WlanEnumInterfaces = wlanapi.WlanEnumInterfaces
WlanEnumInterfaces.argtypes = (HANDLE, c_void_p,
                               POINTER(POINTER(WLAN_INTERFACE_INFO_LIST)))
WlanEnumInterfaces.restype = DWORD

WlanGetAvailableNetworkList = wlanapi.WlanGetAvailableNetworkList
WlanGetAvailableNetworkList.argtypes = (HANDLE, POINTER(GUID), DWORD, c_void_p,
                                        POINTER(POINTER(WLAN_AVAILABLE_NETWORK_LIST)))
WlanGetAvailableNetworkList.restype = DWORD

WlanGetNetworkBssList = wlanapi.WlanGetNetworkBssList
WlanGetNetworkBssList.argtypes = (HANDLE, POINTER(GUID),POINTER(GUID),POINTER(GUID), c_bool, c_void_p,
                                  POINTER(POINTER(WLAN_BSS_LIST)))
WlanGetNetworkBssList.restype = DWORD


WlanFreeMemory = wlanapi.WlanFreeMemory
WlanFreeMemory.argtypes = [c_void_p]


WlanScan = wlanapi.WlanScan
WlanScan.argtypes = (HANDLE, POINTER(GUID),c_void_p,c_void_p, c_void_p)
WlanScan.restype = DWORD




def get_interface():

    NegotiatedVersion = DWORD()
    ClientHandle = HANDLE()
    ret = WlanOpenHandle(1, None, byref(NegotiatedVersion), byref(ClientHandle))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))
        # find all wireless network interfaces
    pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
    ret = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))
    try:
        ifaces = customresize(pInterfaceList.contents.InterfaceInfo,
                              pInterfaceList.contents.NumberOfItems)
        # find each available network for each interface
        for iface in ifaces:
            #print "Interface: %s" % (iface.strInterfaceDescription)
            interface = iface.strInterfaceDescription

    finally:
        WlanFreeMemory(pInterfaceList)
        return interface

class MAC_BSSID_POWER:
    """Classe para os valores retirados"""
    def __init__(self, mac, bssid):

        self.mac = str(mac)
        self.bssid = str(bssid)
        self.valores = []

    def addPower(self,power):
        self.valores.append(int(power))

    def getBssid(self):
        return self.bssid
    def getPowers(self):
        return self.valores
    def getMac(self):
        return self.mac


def get_BSSI():

    BSSI_Values={}

    NegotiatedVersion = DWORD()
    ClientHandle = HANDLE()
    ret = WlanOpenHandle(1, None, byref(NegotiatedVersion), byref(ClientHandle))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))
        # find all wireless network interfaces
    pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
    ret = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))
    try:
        ifaces = customresize(pInterfaceList.contents.InterfaceInfo,
                              pInterfaceList.contents.NumberOfItems)
        # find each available network for each interface
        for iface in ifaces:
            # print "Interface: %s" % (iface.strInterfaceDescription)

            pAvailableNetworkList2 = pointer(WLAN_BSS_LIST())


            ret2 = WlanGetNetworkBssList(ClientHandle,
                                         byref(iface.InterfaceGuid),
                                         None,
                                         None,True,None,
                                         byref(pAvailableNetworkList2))
            if ret2 != ERROR_SUCCESS:
                exit(FormatError(ret2))
            try:
                retScan = WlanScan(ClientHandle,byref(iface.InterfaceGuid),None,None,None)
                if retScan != ERROR_SUCCESS:
                    exit(FormatError(retScan))
                avail_net_list2 = pAvailableNetworkList2.contents
                networks2 = customresize(avail_net_list2.NetworkBSS,
                                         avail_net_list2.NumberOfItems)

                for network in networks2:

                    SSID = str(network.dot11Ssid.SSID[:network.dot11Ssid.SSIDLength])
                    BSSID = ':'.join('%02x' % b for b in network.dot11Bssid).upper()
                    signal_strength = str(network.lRssi)

                #    print "SSID: " + SSID + " BSSID: "+ BSSID+ " SS: "+signal_strength

                    BSSI_Values[BSSID] = [SSID,signal_strength]

                    #print "Total "+str(len(networks2))
                    #print BSSI_Values

            finally:
                WlanFreeMemory(pAvailableNetworkList2)
                WlanCloseHandle(ClientHandle,None)
    finally:
        WlanFreeMemory(pInterfaceList)
    return BSSI_Values


def get_BSSI_times_and_total_seconds(times,seconds):

    BSSI_to_return = {}

    for i in range(0,seconds*times):
        time_to_sleep = float(1.0/times)
        time.sleep(time_to_sleep)
        got_bssi_temp = get_BSSI()

        for bssi in got_bssi_temp:
            if not BSSI_to_return.get(bssi):
                BSSI_to_return[bssi] = MAC_BSSID_POWER(bssi,got_bssi_temp[bssi][0])
                BSSI_to_return[bssi].addPower( got_bssi_temp[bssi][1] )

                #BSSI_to_return[bssi] = [got_bssi_temp[bssi][1]]

            else:
                BSSI_to_return[bssi].addPower( got_bssi_temp[bssi][1] )
                #BSSI_to_return[bssi].append(got_bssi_temp[bssi][1])
        print "Medicao "+str(i)+" de "+str(seconds*times)
    print BSSI_to_return
    return BSSI_to_return



if __name__ == '__main__':

    #print get_interface()
    import time
    test = get_BSSI()
    for i in range(0,10):
        time.sleep(0.5)
        oldTest = test
        test = get_BSSI()
        print "Teste: "+str(i)
        if oldTest == test:
            print "IGUAL"
        else:
            print "DIFERENTE"
        print test
    print "End"