title: How to connect to wifi network by bssid
body: <p>I would like to connect to wifi and select AP based on BSSID. Wifi profile exists for SSID on Windows. There are multiple AP with the same SSID.</p>
  
  <p>Found example code of ManagedWifi on <a href="https://archive.codeplex.com/?p=managedwifi" rel="nofollow noreferrer">CodePlex</a>. </p>
  
  <p>I have added an overloading function of connect and all extra prototype, that needed based on <a href="https://social.msdn.microsoft.com/Forums/healthvault/en-US/d11150ae-9bb7-4e44-9c57-6a411b9772d7/cant-connect-to-a-specific-ap-using-wlanconnect?forum=vistawirelesssdk" rel="nofollow noreferrer">this</a>  description.</p>
  
  <p>The function running without error but not connecting to specified BSSID. Anyone has any idea what I missed?</p>
  
  <pre><code>public void Connect(string Bssid, string Ssid)
  {
      byte[] desBssid = Bssid.Split(':').Select(x =&gt; 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);
  }
  </code></pre>
  
  <p>The log about failing connection:</p>
  
  <pre><code>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: &lt;ssid of APs&gt;
                         SSID: &lt;ssid of APs&gt;
                         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          : &lt;machine name&gt;
  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...}
  </code></pre>