B
billy
i've created the following code based on the classes i've found in the wmi
test console. it works however, both this code and in the wmi test console
it appears i'm getting two rssi signals from the same wifi nic...the first
has the "instancename" of the nic driver...the second is just a "?"...each
have differing signal strengths. can someone explain? also, will there
definitely always only be one "active" wifi nic at any given time? i don't
want to display information for one nic whilst another (wifi or not) is
really the one with the active network connection.
tia...here's the code (sorry for the text wrapping):
' ===============
Imports System.Management
Public Class Network
#Region " structures "
Public Structure WirelessAccessPoint
Dim NetworkCard As String
Dim SignalStrength As Long
Dim SSID As String
End Structure
#End Region
#Region " properties "
Public Shared ReadOnly Property Connected() As Boolean
Get
Try
Dim assignedIp As String =
GetHostByName(GetHostName()).AddressList(0).ToString
Return Not assignedIp = Net.IPAddress.Parse("127.0.0.1").ToString
Catch ex As Exception
Return False
End Try
End Get
End Property
#End Region
#Region " methods "
Public Shared Function WirelessAccessPoints() As WirelessAccessPoint()
Dim device As ManagementObject
Dim deviceName As String
Dim deviceOptions As New ObjectGetOptions
Dim devices() As String
Dim waps() As wirelessAccessPoint
Try
' get wap device names
Dim deviceClass As New ManagementClass("root/WMI",
"MSNDis_80211_BSSIList", deviceOptions)
For Each device In deviceClass.GetInstances
deviceName = device.GetPropertyValue("InstanceName").ToString()
If Not deviceName.ToLower.EndsWith("miniport") Then
Dim index As Integer = 0
If Not devices Is Nothing Then index = devices.Length
ReDim Preserve devices(index)
devices(index) = deviceName
End If
Next
If Not devices Is Nothing Then
' force a rescan of available waps
Dim scanClass As New ManagementClass("root/WMI",
"MSNDis_80211_BssIdListScan", deviceOptions)
Dim scanObject As ManagementObject = scanClass.CreateInstance()
For Each deviceName In devices
scanObject("Active") = True
scanObject("InstanceName") = deviceName
scanObject("UnusedParameter") = 1
scanObject.Put()
'now, get signal strength
Dim sql As String = "SELECT * " &
vbCrLf & _
"FROM MSNDis_80211_BSSIList " &
vbCrLf & _
"WHERE Active = 'TRUE' " &
vbCrLf & _
"AND InstanceName = '" & deviceName
& "'"
Dim query As New ManagementObjectSearcher("root/WMI", sql)
Dim wap As ManagementBaseObject
Dim wapCollection As ManagementObjectCollection = query.Get()
Dim wapEnumerator As
ManagementObjectCollection.ManagementObjectEnumerator =
wapCollection.GetEnumerator
wapEnumerator.MoveNext()
For Each wap In
wapEnumerator.Current.Properties("Ndis80211BSSIList").Value
Dim wirelessAccessPoint As New wirelessAccessPoint
With wirelessAccessPoint
.NetworkCard = deviceName
.SignalStrength =
CType(wap.GetPropertyValue("Ndis80211Rssi").ToString, Long)
.SSID = New
String(Encoding.ASCII.GetChars(CType(wap.GetPropertyValue("Ndis80211Ssid"),
Byte()))).Trim
End With
Dim index As Integer = 0
If Not waps Is Nothing Then index = waps.Length
ReDim Preserve waps(index)
waps(index) = wirelessAccessPoint
Next
Next
End If
Return waps
Finally
End Try
Return Nothing
End Function
#End Region
End Class
test console. it works however, both this code and in the wmi test console
it appears i'm getting two rssi signals from the same wifi nic...the first
has the "instancename" of the nic driver...the second is just a "?"...each
have differing signal strengths. can someone explain? also, will there
definitely always only be one "active" wifi nic at any given time? i don't
want to display information for one nic whilst another (wifi or not) is
really the one with the active network connection.
tia...here's the code (sorry for the text wrapping):
' ===============
Imports System.Management
Public Class Network
#Region " structures "
Public Structure WirelessAccessPoint
Dim NetworkCard As String
Dim SignalStrength As Long
Dim SSID As String
End Structure
#End Region
#Region " properties "
Public Shared ReadOnly Property Connected() As Boolean
Get
Try
Dim assignedIp As String =
GetHostByName(GetHostName()).AddressList(0).ToString
Return Not assignedIp = Net.IPAddress.Parse("127.0.0.1").ToString
Catch ex As Exception
Return False
End Try
End Get
End Property
#End Region
#Region " methods "
Public Shared Function WirelessAccessPoints() As WirelessAccessPoint()
Dim device As ManagementObject
Dim deviceName As String
Dim deviceOptions As New ObjectGetOptions
Dim devices() As String
Dim waps() As wirelessAccessPoint
Try
' get wap device names
Dim deviceClass As New ManagementClass("root/WMI",
"MSNDis_80211_BSSIList", deviceOptions)
For Each device In deviceClass.GetInstances
deviceName = device.GetPropertyValue("InstanceName").ToString()
If Not deviceName.ToLower.EndsWith("miniport") Then
Dim index As Integer = 0
If Not devices Is Nothing Then index = devices.Length
ReDim Preserve devices(index)
devices(index) = deviceName
End If
Next
If Not devices Is Nothing Then
' force a rescan of available waps
Dim scanClass As New ManagementClass("root/WMI",
"MSNDis_80211_BssIdListScan", deviceOptions)
Dim scanObject As ManagementObject = scanClass.CreateInstance()
For Each deviceName In devices
scanObject("Active") = True
scanObject("InstanceName") = deviceName
scanObject("UnusedParameter") = 1
scanObject.Put()
'now, get signal strength
Dim sql As String = "SELECT * " &
vbCrLf & _
"FROM MSNDis_80211_BSSIList " &
vbCrLf & _
"WHERE Active = 'TRUE' " &
vbCrLf & _
"AND InstanceName = '" & deviceName
& "'"
Dim query As New ManagementObjectSearcher("root/WMI", sql)
Dim wap As ManagementBaseObject
Dim wapCollection As ManagementObjectCollection = query.Get()
Dim wapEnumerator As
ManagementObjectCollection.ManagementObjectEnumerator =
wapCollection.GetEnumerator
wapEnumerator.MoveNext()
For Each wap In
wapEnumerator.Current.Properties("Ndis80211BSSIList").Value
Dim wirelessAccessPoint As New wirelessAccessPoint
With wirelessAccessPoint
.NetworkCard = deviceName
.SignalStrength =
CType(wap.GetPropertyValue("Ndis80211Rssi").ToString, Long)
.SSID = New
String(Encoding.ASCII.GetChars(CType(wap.GetPropertyValue("Ndis80211Ssid"),
Byte()))).Trim
End With
Dim index As Integer = 0
If Not waps Is Nothing Then index = waps.Length
ReDim Preserve waps(index)
waps(index) = wirelessAccessPoint
Next
Next
End If
Return waps
Finally
End Try
Return Nothing
End Function
#End Region
End Class