网站地图    收藏    合作   
<

快捷菜单 返回顶部

Android SDK 提供了 WIFI 开发的相关 API,被保存在 android.net.wifi 和 android.net.wifi.p2p 包下。借助于 Android SDK 提供的相关开发类,可以方便地在 Android 系统的手机上开发基于 WIFI 的应用程序。

实例 WIFIDemo 演示了使用 WIFI 进行连接设备搜索并获取相应信息的过程,运行效果如图 1 所示。

WIFIDemo 运行结果
图 1  WIFIDemo 运行结果

实例 WIFIDemo 中所使用的布局文件 main.xml 的内容如下:
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
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">
 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
 
            <Button
                android:id="@+id/open_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打开wifi" />
 
            <Button
                android:id="@+id/close_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关闭wifi" />
 
            <Button
                android:id="@+id/check_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="检查wifi" />
 
            <Button
                android:id="@+id/search_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="扫描wifi" />
        </LinearLayout>
 
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="null" />
    </LinearLayout>
</ScrollView>
实例 WIFIDemo 中 AndroidManifest.xml 文件的代码如下:
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="introduction.android.wifidemo">
 
    <uses-sdk android:minSdkVersion="10" />
 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
实例 WIFIDemo 中主 Activity 文件 MainActivity.java 的代码如下:
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package introduction.android.wifidemo;
 
import java.util.List;
 
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    private Button open_bt, close_bt, check_bt, search_bt;
    private TextView textView;
    private WifiManager wifiManager;
    private WifiInfo wifiInfo;
    private ScrollView scrollView;
    private List WifiConfiguration;
    private ScanResult scanResult;
    private List<ScanResult> WifiList;
    private StringBuffer stringBuffer = new StringBuffer();
 
    /**
     * Called when the activity is first created.
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        scrollView = (ScrollView) findViewById(R.id.mScrollView);
        open_bt = (Button) findViewById(R.id.open_bt);
        close_bt = (Button) findViewById(R.id.close_bt);
        check_bt = (Button) findViewById(R.id.check_bt);
        search_bt = (Button) findViewById(R.id.search_bt);
        textView = (TextView) findViewById(R.id.text);
 
        open_bt.setOnClickListener(new open_btListener());
        close_bt.setOnClickListener(new close_btListener());
        check_bt.setOnClickListener(new check_btListener());
        search_bt.setOnClickListener(new search_btListener());
 
    }
 
    class search_btListener implements OnClickListener {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            wifiManager.startScan();
            WifiList = wifiManager.getScanResults();
            wifiInfo = wifiManager.getConnectionInfo();
            if (stringBuffer != null) {
                stringBuffer = new StringBuffer();
            }
            stringBuffer = stringBuffer.append("Wifi名").append(" ").append("Wifi地址").append(" ")
                    .append("Wifi频率").append("").append("Wifi信号")
                    .append("\n");
            if (WifiList != null) {
                for (int i = 0; i < WifiList.size(); i++) {
                    scanResult = WifiList.get(i);
                    stringBuffer = stringBuffer.append(scanResult.SSID).append(" ")
                            .append(scanResult.BSSID).append(" ")
                            .append(scanResult.frequency).append(" ")
                            .append(scanResult.level).append("\n");
                    textView.setText(stringBuffer.toString());
                }
 
                stringBuffer = stringBuffer.append
                        ("-----------------------------------------------").append("\n");
                textView.setText(stringBuffer.toString());
                stringBuffer = stringBuffer.append("当前Wifi—BSSID").append(":").append(wifiInfo.getBSSID()).append("\n")
                        .append("当前wifi—HiddenSSID").append(": ").append(wifiInfo.getHiddenSSID()).append("\n")
                        .append("当前Wifi—IpAddress").append(": ").append(wifiInfo.getIpAddress()).append("\n")
                        .append("当前Wifi—LinkSpeed").append(": ").append(wifiInfo.getLinkSpeed()).append("\n")
                        .append("当前Wifi—MacAddress").append(": ").append(wifiInfo.getMacAddress()).append("\n")
                        .append("当前Wifi—Network ID").append(": ").append(wifiInfo.getNetworkId()).append("\n")
                        .append("当前Wifi—RSSI").append(": ").append(wifiInfo.getRssi()).append("\n")
                        .append("当前Wifi—SSID").append(": ").append(wifiInfo.getSSID()).append("\n")
                        .append("-----------------------------------------------").append("\n")
                        .append("全部打印出关于本机Wifi信息").append(": ").append
                                (wifiInfo.toString());
                textView.setText(stringBuffer.toString());
            }
            //stringBuffer=stringBuffer.append("-----------------------------------------------").append("\n");
            //textView.setText()
        }
    }
 
    class check_btListener implements OnClickListener {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            wifiManager = (WifiManager) MainActivity.this
                    .getSystemService(Context.WIFI_SERVICE);
            System.out.println(wifiManager.getWifiState());
            Toast.makeText(MainActivity.this,
                    "当前网卡状态为:" + change(), Toast.LENGTH_SHORT)
                    .show();
        }
    }
 
 
    class close_btListener implements OnClickListener {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            wifiManager = (WifiManager) MainActivity.this
                    .getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);
            System.out.println(wifiManager.getWifiState());
            Toast.makeText(MainActivity.this,
                    "当前网卡状态为" + change(), Toast.LENGTH_SHORT)
                    .show();
        }
    }
 
    class open_btListener implements OnClickListener {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            System.out.println(wifiManager.getWifiState());
            Toast.makeText(MainActivity.this,
                    "当前网卡状态为" + change(), Toast.LENGTH_SHORT)
                    .show();
        }
    }
 
    public String change() {
        String temp = null;
        switch (wifiManager.getWifiState()) {
            case 0:
                temp = "wifi正在关闭ING";
                break;
            case 1:
                temp = "wifi已经关闭";
                break;
            case 2:
                temp = "wifi正在打开ING";
                break;
            case 3:
                temp = "wifi已经打开";
                break;
            default:
                break;
        }
        return temp;
    }
}

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com