Android Wifi搜索

 

       今天看了一下wifi的文档,写了一个简易的wifi的打开和搜索的功能。

      那么,启用wifi是必须用真机调试的,权限也是必须的:

   

1
2
3
4
5
6
7
8
9
10
<!-- 配置Wifi訪問權限 -->
   
  <uses-permission 
      android:name="android.permission.ACCESS_WIFI_STATE"
      />
   
  <!-- 配置改變Wifi狀態的權限 -->
   
 
  <uses-permission  android:name="android.permission.CHANGE_WIFI_STATE"/>

 

  然后呢布局文件里面就只有4个按钮:打开wifi,关闭wifi,wifi状态,wifi状态信息:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/openWifi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="打开Wifi" />
 
    <Button
        android:id="@+id/closedWifi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="关闭Wifi" />
 
     
     
        <Button
        android:id="@+id/wifiState"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="Wifi状态" />
         
         
          <Button
        android:id="@+id/wifiStateInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="Wifi状态信息" />
     
</LinearLayout>

 

      代码里面的注释已经很详细了,我在这里就不过多的解释了,看代码你懂的:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cn.zuxia.android.app;
 
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
 
public class TestWifiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // 获取wifiManager对象
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }
 
    // 定义操作Wifi的管理对象WifiManager
    private WifiManager mWifiManager;
 
    public void onAction(View v) {
 
        int id = v.getId();
        // wifi的状态值
        int state = mWifiManager.getWifiState();
 
        switch (id) {
 
        case R.id.openWifi:
            // 如果wifi打开就关闭
            if (WifiManager.WIFI_STATE_DISABLED == state) {
 
                Toast.makeText(this, "正在打開Wifi.....", Toast.LENGTH_SHORT)
                        .show();
                mWifiManager.setWifiEnabled(true);// 將值設置為true表示打開wifi
            } else {
 
                Toast.makeText(this, "Wifi已經打開", Toast.LENGTH_SHORT).show();
            }
 
            break;
        case R.id.closedWifi:
 
            // 如果wifi关闭就打开
            if (WifiManager.WIFI_STATE_ENABLED == state) {
 
                Toast.makeText(this, "正在關閉Wifi.......", Toast.LENGTH_SHORT)
                        .show();
                mWifiManager.setWifiEnabled(false);// 將值設置為false表示關閉wifi
            } else {
 
                Toast.makeText(this, "Wifi已經關閉", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.wifiState:
 
            Toast.makeText(this, "Wifi狀態為:" + state, Toast.LENGTH_SHORT).show();
 
            break;
 
        case R.id.wifiStateInfo:
 
            // 得到WifiInfo對象
 
            // WifiInfo wifiInfo=mWifiManager.getConnectionInfo();
            //
            // if(null!=wifiInfo){
            //
            // System.out.println(wifiInfo.getBSSID()+",,"+wifiInfo.getMacAddress()+",,,"+wifiInfo.getIpAddress());
            // }else{
            // Toast.makeText(this, "返回的WifiInfo對象為空",
            // Toast.LENGTH_SHORT).show();
            // }
 
            // 存储搜索到的wifi
            List<WifiConfiguration> configurations = mWifiManager
                    .getConfiguredNetworks();
 
            System.out.println(configurations.size());
            for (WifiConfiguration wifiConfiguration : configurations) {
 
                System.out.println(wifiConfiguration.wepKeys + "@@@"
                        + wifiConfiguration.status);
 
            }
 
            break;
        }
 
    }
 
}

 

      那么这个简单的wifi的打开和关闭的简单功能就已经实现了,希望有朋友们有帮助,有什么问题的话还请留言吧!!

Android Wifi搜索

上一篇:下载linaro android 4.4.2 for panda4460


下一篇:高仿沙发管家远程安装界面——Android作为HTTP服务器读取Assets网页文件