如果要透過程式來啟動WIFI ,.net compact framework 尚未提供任何指令,必需引用外部檔案coredll.dll所提供 api來驅動wifi,可以使用 DevicePowerNotify() API 要求釋放裝置最低電力需求及 SetDevicePower() API 設定系統電源狀態
Step1:開啟vs2008 新增一個vb 智慧型裝置專案,在表單上新增一個按鍵用來開關wifi,在來將滑鼠移到方案總管到SmartDeviceProject1下的參考,按下滑鼠右鍵加入參考
Step2:選擇Microsoft.WindowsMobile、Microsoft.WindowsMobile.Status元件按下確定
Step3:撰寫Form1.vb表單程式碼
01 Imports ...System.Runtime.InteropServices
02 Imports Microsoft.WindowsMobile.Status
03
04 Public Class Form1
05 <DllImport("coredll.dll")> Public Shared Function SetDevicePower() Shared Function SetDevicePower( _
06 ByVal pvDevice As String, ByVal df As Integer, ByVal ds As CEDEVICE_POWER_STATE) As Integer
07 End Function
08 Enum CEDEVICE_POWER_STATE
09 PwrDeviceUnspecified = -1
10 D0 = 0 'Full On: full power, full functionality
11 D1 = 1 'Low Power On: fully functional at low power/performance
12 D2 = 2 'Standby: partially powered with automatic wake
13 D3 = 3 ' Sleep: partially powered with device initiated wake
14 D4 = 4 'Off: unpowered
15 PwrDeviceMaximum
16 End Enum
17 <DllImport("coredll.dll")> Public Shared Function DevicePowerNotify() Shared Function DevicePowerNotify(ByVal device As String, ByVal state As CEDEVICE_POWER_STATE, ByVal flags As Integer) As Integer
18 End Function
19 Friend WithEvents wifi As SystemState = New SystemState(SystemProperty.WiFiStatePowerOn)
20 Public Sub wifi_power_on() Sub wifi_power_on()
21 Try
22 '開啟wifi,此機碼在[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\State]下因為每台的wifi名稱不同
23 '所以你要自已去找出來TNETW12511這個是我的網卡名稱,你只要找出來後把這個文字替換就可以了
24 '電源管理員預設實作理解下列 GUID:
25 '{A32942B7-920C-486b-B0E6-92A702A99B35}—一般可以管理電源的裝置
26 '{8DD679CE-8AB4-43c8-A14A-EA4963FAA715}—可管理電源的阻斷裝置
27 '{98C5250D-C29A-4985-AE5F-AFE5367E5006}—可管理電源的 NDIS Miniport
28
29 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\TNETW12511", CEDEVICE_POWER_STATE.D0, 1)
30 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\TNETW12511", 1, CEDEVICE_POWER_STATE.D0)
31 Application.DoEvents()
32 Catch
33 End Try
34 End Sub
35 Public Sub wifi_power_off() Sub wifi_power_off()
36 Try
37 '關閉wifi
38 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\TNETW12511", CEDEVICE_POWER_STATE.D4, 1)
39 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\TNETW12511", 1, CEDEVICE_POWER_STATE.D4)
40 Application.DoEvents()
41 Catch
42 End Try
43 End Sub
44 Private Sub Button1_Click() Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
45 '判斷畫面的按鍵的文字來開關wifi
46 If Me.Button1.Text = "開" Then
47 wifi_power_on()
48 Else
49 wifi_power_off()
50 End If
51 End Sub
52
53 Private Sub wifi_Changed() Sub wifi_Changed(ByVal sender As Object, ByVal args As Microsoft.WindowsMobile.Status.ChangeEventArgs) Handles wifi.Changed
54 '判斷wifi狀態變更事件所回傳的值,來變更目前的wifi狀態值
55 If args.NewValue = "1" Then
56 Me.Button1.Text = "關"
57 Me.BackColor = Color.Green
58 Else
59 Me.Button1.Text = "開"
60 Me.BackColor = Color.Silver
61 End If
62 End Sub
63 End Class
Step4:按下偵錯\開始偵錯來部署應用程式測試一下
Step5:首先來看一下未開啟wifi無線區網網路的設定,在來畫面的按鍵開按一下
Step6:這時候wifi開啟,收到wifi狀態值畫面都更新了,同時我們也來看一下wifi無線區網網路的設定畫面
Step7:源碼下載
參考:http://www.pinvoke.net/default.aspx/coredll/DevicePowerNotify.html