none
Code to connect Mobile phone hot spot to wi-fi

    Question

  • When I turn on my phone hot spot it appears in the wi-fi list. I know the name of the hot-spot.

    In code I want to set the name of the hot spot, then have the code connect or dis-connect the device to/from wi-fi.

    I am using Visual studio 2010, any help much appreciated, have looked elsewhere to no avail

    Monday, March 11, 2019 11:48 PM

Answers

  • After searching, I get the the summary.

    To do something like shell, you can try this sample:

    Sub connectTo(ByVal name As String)
        Dim p = "netsh.exe"
        Dim sInfo As New ProcessStartInfo(p, "wlan connect " & name)
        sInfo.CreateNoWindow = True
        sInfo.WindowStyle = ProcessWindowStyle.Hidden
        Process.Start(sInfo)
    End Sub

    But this cannot get return value.

    And another way with Native Wifi Api is:

        Dim wlanConnectionParameters As New WLAN_CONNECTION_PARAMETERS
        wlanConnectionParameters.dot11BssType = DOT11_BSS_TYPE.dot11_BSS_type_any
        wlanConnectionParameters.dwFlags = 0
        wlanConnectionParameters.strProfile = "Tom-Jones"
        wlanConnectionParameters.wlanConnectionMode = WLAN_CONNECTION_MODE.wlan_connection_mode_profile
        WlanConnect(ClientHandle, pInterfaceGuid, wlanConnectionParameters, IntPtr.Zero)
    "ClientHandle" is return by WlanOpenHandle, "pInterfaceGuid" is the GUID of the interface to use for the connection.
    According to msdn, "The WlanConnect function returns immediately. To be notified when a connection is established or when no further connections will be attempted, a client must register for notifications by calling WlanRegisterNotification."

    Best Regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by x38class Monday, March 18, 2019 5:13 AM
    Thursday, March 14, 2019 7:55 AM

All replies

  • Hi,

    Well, I'm little confused. What are you trying to develop? A windows 7 app or a windows 8 app?


    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 12, 2019 6:39 AM
  • Windows 10

    I want to replicate in code, the action of clicking on the wifi icon, selecting a network, connecting & dis-connecting in vb.net 2010

    Tuesday, March 12, 2019 10:29 PM
  • Hi,

    As I know, Visual Studio 2010 could not develop UWP apps that running in windows 10. Are you developing UWP apps or win32 apps?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 3:31 AM
  • I have not come across UWP before, so looked it up, appears to me to be able to run on all devices that relate to W10.

    My app is only for my pc running W10, so I presume my answer will be Win 32, so it is a stand alone app only being used on my pc for my benefit & is not going anywhere else.

    Wednesday, March 13, 2019 5:54 AM
  • Also I do not understand why my question was posted here, it was in VB net questions & somebody there KINDLY suggested here.

    I am not experienced enough to know why it should be posted here, maybe because I mentioned phone, which has nothing to do with the original question

    Wednesday, March 13, 2019 6:17 AM
  • Hi,

    I'm sorry to say that if it's a win32 app, the problem will be out of the scope of UWP forum. But I'll help you to move this thread to General Windows Desktop Development Issues forum as it is more related to desktop development.

    Thank you for your patience.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 8:15 AM
  • Hello,

    You can use shell command "netsh wlan", here is the document on msdn: 

    https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc755301(v=ws.10)

    Or you can reference to the Native Wifi Api to connect/disconnect(with WlanConnect/WlanDisconnect):

    https://docs.microsoft.com/en-us/windows/desktop/nativewifi/about-native-wifi

    Best Regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 9:04 AM
  • Thanks to Drake for providing the links, which I have studied, however my technical programming ability does not help me in being able to write code to piece together the required commands to achieve my goal.

    Is there an example somewhere that is similar to:

    Imports Wi_Fi_Connection

    dim Connection_name as string = "Tom Jones"

    Function Connect_to_wi-fi(Connection_name) as boolean

    do something like shell(some api or exe, Connection_name)

    end function

    IF Connect_to_wi-fi("Tom-Jones") = true then

    execute my other code

    else

    msgbox( "Cannot connect to " & Connection_name)

    end if

    Wednesday, March 13, 2019 11:22 PM
  • After searching, I get the the summary.

    To do something like shell, you can try this sample:

    Sub connectTo(ByVal name As String)
        Dim p = "netsh.exe"
        Dim sInfo As New ProcessStartInfo(p, "wlan connect " & name)
        sInfo.CreateNoWindow = True
        sInfo.WindowStyle = ProcessWindowStyle.Hidden
        Process.Start(sInfo)
    End Sub

    But this cannot get return value.

    And another way with Native Wifi Api is:

        Dim wlanConnectionParameters As New WLAN_CONNECTION_PARAMETERS
        wlanConnectionParameters.dot11BssType = DOT11_BSS_TYPE.dot11_BSS_type_any
        wlanConnectionParameters.dwFlags = 0
        wlanConnectionParameters.strProfile = "Tom-Jones"
        wlanConnectionParameters.wlanConnectionMode = WLAN_CONNECTION_MODE.wlan_connection_mode_profile
        WlanConnect(ClientHandle, pInterfaceGuid, wlanConnectionParameters, IntPtr.Zero)
    "ClientHandle" is return by WlanOpenHandle, "pInterfaceGuid" is the GUID of the interface to use for the connection.
    According to msdn, "The WlanConnect function returns immediately. To be notified when a connection is established or when no further connections will be attempted, a client must register for notifications by calling WlanRegisterNotification."

    Best Regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by x38class Monday, March 18, 2019 5:13 AM
    Thursday, March 14, 2019 7:55 AM
  • Excellent, I am impressed, now I just need to expand on that to add password and later disconnect, any help with that much appreciated.

    I am looking at the moment to resolve myself, but am running out of time as dinner is about to be served!

    Friday, March 15, 2019 5:51 AM
  • To Add password, you should set it in the profile:

    Dim profileXml As String = "<?xmlversion=\"1.0\"encoding=\"US-ASCII\"?>\  
    <WLANProfilexmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\  
    <name>Tom-Jones</name>\  
    <SSIDConfig>\  
    <SSID>\  
    <name>Tom-Jones</name>\  
    </SSID>\  
    </SSIDConfig>\   
    <connectionType>ESS</connectionType>\  
    <connectionMode>auto</connectionMode>\  
    <autoSwitch>false</autoSwitch>\  
    <MSM>\  
    <security>\  
    <authEncryption>\  
    <authentication>WPA2PSK</authentication>\  
    <encryption>AES</encryption>\  
    <useOneX>false</useOneX>\  
    </authEncryption>\  
    <sharedKey>\  
    <keyType>passPhrase</keyType>\  
    <protected>false</protected>\  
    <keyMaterial>Password</keyMaterial>\  
    </sharedKey>\  
    </security>\  
    </MSM>\  
    </WLANProfile>"
    Dim Wlanreason As WLAN_REASON_CODE;
    WlanSetProfile(ClientHandle, pInterfaceGuid, 0, profileXml,IntPtr.Zero,TRUE,IntPtr.Zero,&Wlanreason); 

    DisConnect:

    WlanDisconnect(ClientHandle, &pInterfaceGuid, NULL);

    You could also reference to this thread:

    https://social.msdn.microsoft.com/Forums/ie/en-US/76d0d2c0-f966-48f3-afc7-0369e278a596/native-wifi-how-to-connect-a-wifi-network-with-a-password?forum=windowssdk

    Best Regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, March 18, 2019 3:01 AM
  • Thanks for your code & info above, hopefully I will be able to implement it successfully
    Monday, March 18, 2019 5:14 AM