Sing
Sing

Reputation: 4052

Can I programmatically turn WiFi on and off Windows Phone 8.1?

Can I programmatically turn WiFi on and off on Windows Phone 8.1?

If so, how to turn wifi on/off on C#?

Actually, I already do this in another platform:

<DllImport("coredll.dll")> Public Shared Function DevicePowerNotify(ByVal device As String, ByVal state As CEDEVICE_POWER_STATE, ByVal flags As Integer) As Integer  
End Function  

<DllImport("coredll.dll")> Public Shared Function SetDevicePower( _  
ByVal pvDevice As String, _  
ByVal df As Integer, _  
ByVal ds As CEDEVICE_POWER_STATE) As Integer  
End Function  

Public Shared Sub wifi_power_on()  
    Try  
        DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\cardname", CEDEVICE_POWER_STATE.D0, 1)  
        SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\cardname", 1, CEDEVICE_POWER_STATE.D0)  
        Application.DoEvents()  
    Catch  
    End Try  

End Sub  

Public Shared Sub wifi_power_off()  
    Try  
        DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\cardname", CEDEVICE_POWER_STATE.D4, 1)  
        SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\cardname", 1, CEDEVICE_POWER_STATE.D4)  
        Application.DoEvents()  
    Catch  
    End Try  
End Sub 

But in Wondows Phone 8.1 , I don't know how to did the same things in C# this time.

I'm afraid it isn't support in the same way?

This is what I've try in C#:

    [DllImport("coredll.dll")]
    public static extern int DevicePowerNotify(string device, CEDEVICE_POWER_STATE state, int flags);

    [DllImport("coredll.dll")]
    public static extern int SetDevicePower(string pvDevice, int df, CEDEVICE_POWER_STATE ds);

    public static void wifi_power_on()
    {
        try
        {
            DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\cardName", CEDEVICE_POWER_STATE.D0, 1);
            SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\cardName", 1, CEDEVICE_POWER_STATE.D0);
            Application.DoEvents();
        }
        catch
        {
        }

    }

    public static void wifi_power_off()
    {
        try
        {
            DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\cardName", CEDEVICE_POWER_STATE.D4, 1);
            SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\cardName", 1, CEDEVICE_POWER_STATE.D4);
            Application.DoEvents();
        }
        catch
        {
        }
    }

Upvotes: 1

Views: 1220

Answers (1)

pumpkinszwan
pumpkinszwan

Reputation: 1352

In Windows Phone (all versions from 7.0 to the current 8.1) you can not programmatically turn WiFi off or on. This is because the platform leaves these kinds of settings in the user's control at all times.

The closest you can get is to programmatically launch the WiFi settings page where the user can modify the setting:

await Launcher.LaunchUriAsync(new Uri("ms-settings-wifi:"));

Upvotes: 1

Related Questions