c# - 如何通过bssid连接wifi网络
问题描述
我想连接到 wifi 并根据 BSSID 选择 AP。Windows 上存在 SSID 的 Wifi 配置文件。有多个具有相同 SSID 的 AP。
在CodePlex上找到了 ManagedWifi 的示例代码。
我添加了基于此 描述所需的连接和所有额外原型的重载函数。
该函数运行没有错误,但未连接到指定的 BSSID。有人知道我错过了什么吗?
public void Connect(string Bssid, string Ssid)
{
byte[] desBssid = Bssid.Split(':').Select(x => Convert.ToByte(x, 16)).ToArray();
Wlan.NDIS_OBJECT_HEADER ndoh;
ndoh.Type = Wlan.NDIS_OBJECT_TYPE_DEFAULT;
ndoh.Revision = Wlan.DOT11_BSSID_LIST_REVISION_1;
ndoh.Size = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Wlan.DOT11_BSSID_LIST));
Wlan.DOT11_BSSID_LIST desBssidList = new Wlan.DOT11_BSSID_LIST();
desBssidList.Header = ndoh;
desBssidList.uNumOfEntries = 1;
desBssidList.uTotalNumOfEntries = 1;
Wlan.DOT11_MAC_ADDRESS bssid = new Wlan.DOT11_MAC_ADDRESS();
bssid.Dot11MacAddress = desBssid;
desBssidList.BSSIDs = new Wlan.DOT11_MAC_ADDRESS[1];
desBssidList.BSSIDs[0] = bssid;
IntPtr desBssidListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desBssidList));
Marshal.StructureToPtr(desBssidList, desBssidListPtr, false);
Wlan.DOT11_SSID dot11Ssid = new Wlan.DOT11_SSID();
dot11Ssid.ucSSID = Ssid;
dot11Ssid.uSSIDLength = (uint)dot11Ssid.ucSSID.Length;
IntPtr dot11SsidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dot11Ssid));
Marshal.StructureToPtr(dot11Ssid, dot11SsidPtr, false);
Wlan.WlanConnectionParameters connectionParams = new Wlan.WlanConnectionParameters();
connectionParams.wlanConnectionMode = Wlan.WlanConnectionMode.DiscoveryUnsecure;
connectionParams.profile = null;
connectionParams.dot11SsidPtr = dot11SsidPtr;
connectionParams.dot11BssType = Wlan.Dot11BssType.Infrastructure;
connectionParams.flags = 0;
connectionParams.desiredBssidListPtr = desBssidListPtr;
Connect(connectionParams);
}
关于连接失败的日志:
Message : WLAN AutoConfig service failed to connect to a wireless network.
Network Adapter: Intel(R) Dual Band Wireless-AC 7260
Interface GUID: {b081a2ec-8010-4c13-be59-251a69dd12ae}
Connection Mode: Connection to an unsecure network without a profile
Profile Name: <ssid of APs>
SSID: <ssid of APs>
BSS Type: Infrastructure
Failure Reason:The specific network is not available.
RSSI: 255
Id : 8002
Version : 0
Qualifiers :
Level : 2
Task : 24010
Opcode : 191
Keywords : -9223372036317903360
RecordId : 1151
ProviderName : Microsoft-Windows-WLAN-AutoConfig
ProviderId : 9580d7dd-0379-4658-9870-d5be7d52d6de
LogName : Microsoft-Windows-WLAN-AutoConfig/Operational
ProcessId : 4000
ThreadId : 564
MachineName : <machine name>
UserId : S-1-5-18
TimeCreated : 22/06/2019 11:30:41
ActivityId :
RelatedActivityId :
ContainerLog : Microsoft-Windows-WLAN-AutoConfig/Operational
MatchedQueryIds : {}
Bookmark : System.Diagnostics.Eventing.Reader.EventBookmark
LevelDisplayName : Error
OpcodeDisplayName : Failure
TaskDisplayName : AcmConnection
KeywordsDisplayNames : {}
Properties : {System.Diagnostics.Eventing.Reader.EventProperty, System.Diagnostics.Eventing.Reader.EventProperty, System.Diagnostics.Eventing.Reader.EventProperty,
System.Diagnostics.Eventing.Reader.EventProperty...}
解决方案
Castorix 是对的。当指定 mac 时,仍然可以使用基于配置文件的连接模式。
查看修改后的代码:
public void Connect(string Bssid, string Ssid, string Pwd)
{
byte[] desBssid = Bssid.Split(':').Select(x => Convert.ToByte(x, 16)).ToArray();
Wlan.NDIS_OBJECT_HEADER ndoh;
ndoh.Type = Wlan.NDIS_OBJECT_TYPE_DEFAULT;
ndoh.Revision = Wlan.DOT11_BSSID_LIST_REVISION_1;
ndoh.Size = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Wlan.DOT11_BSSID_LIST));
Wlan.DOT11_BSSID_LIST desBssidList = new Wlan.DOT11_BSSID_LIST();
desBssidList.Header = ndoh;
desBssidList.uNumOfEntries = 1;
desBssidList.uTotalNumOfEntries = 1;
Wlan.DOT11_MAC_ADDRESS bssid = new Wlan.DOT11_MAC_ADDRESS();
bssid.Dot11MacAddress = desBssid;
desBssidList.BSSIDs = new Wlan.DOT11_MAC_ADDRESS[1];
desBssidList.BSSIDs[0] = bssid;
IntPtr desBssidListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desBssidList));
Marshal.StructureToPtr(desBssidList, desBssidListPtr, false);
Wlan.DOT11_SSID dot11Ssid = new Wlan.DOT11_SSID();
dot11Ssid.ucSSID = Ssid;
dot11Ssid.uSSIDLength = (uint)dot11Ssid.ucSSID.Length;
IntPtr dot11SsidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dot11Ssid));
Marshal.StructureToPtr(dot11Ssid, dot11SsidPtr, false);
string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>", Ssid, Pwd);
SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
Wlan.WlanConnectionParameters connectionParams = new Wlan.WlanConnectionParameters();
connectionParams.wlanConnectionMode = Wlan.WlanConnectionMode.Profile;
connectionParams.profile = Ssid;
connectionParams.dot11SsidPtr = dot11SsidPtr;
connectionParams.dot11BssType = Wlan.Dot11BssType.Any;
connectionParams.flags = 0;
connectionParams.desiredBssidListPtr = desBssidListPtr;
Connect(connectionParams);
}
推荐阅读
- java - 我不能声明或使用 java swing 对象
- jquery - jQuery Validate:仅在表单成功验证后启用提交按钮
- kotlin - 确保主 JVM 程序会崩溃,即使使用 Kotlin Coroutines 启动也是如此
- javascript - 简化三元运算符
- azure - Ansible中模板内的模板?
- python - Python(WSGI Web 应用程序):上传的文件在第一次被读取之前存储在哪里?
- postgresql - 确定查询命中的关系
- javascript - 在 ASP.NET 中访问 JavaScript 变量
- php -
希望一切安好。
我想显示一系列徽章,这些徽章将根据包含特定字符串的字段显示不同的图像。例如,像这样的事情(虽然我知道这可能是不正确的)
<?php if (stripos($business, "local") !== false) echo "badge-
- c# - 值不能为空 参数名称:destinationTimeZone