基于Android studio的WIFI搜索显示与WIFI打开

  • Post author:
  • Post category:其他



效果图


main.xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.huangfushi.hfs192025214_wifi.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WIFI控制平台"
        android:layout_gravity="center_horizontal" />
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/startWifi"
    android:text="开启WIFI"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/stopWifi"
        android:text="关闭WIFI"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkWifi"
        android:text="检查WIFI"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/saomiao"
        android:text="扫描WIFI"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:text=" "/>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_wifiinfo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

    </ScrollView>
</LinearLayout>

MainActivity

package com.huangfushi.hfs192025214_wifi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.List;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.net.wifi.ScanResult;
import android.net.wifi.ScanResult;




public class MainActivity extends Activity
{
    private Button startButton = null;
    private Button stopButton = null;
    private Button checkButton = null;
    private Button saomiao;
    private WifiManager wifiManager = null;
    private TextView textView;
    private TextView tv_wifiinfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button) findViewById(R.id.startWifi);
        stopButton = (Button) findViewById(R.id.stopWifi);
        checkButton = (Button) findViewById(R.id.checkWifi);


        saomiao=(Button)findViewById(R.id.saomiao);
        textView=(TextView)findViewById(R.id.tv);
        tv_wifiinfo=(TextView)findViewById(R.id.tv_wifiinfo);
        startButton.setOnClickListener(new StartWifilistener());
        stopButton.setOnClickListener(new StopWifiListener());
        checkButton.setOnClickListener(new CheckWifiListener());
        saomiao.setOnClickListener(new saomiao());
    }

    public void getState(View v) {
        switch (wifiManager.getWifiState()) {
            case 0:
                textView.setText("WiFi正在关闭");
                //Toast.makeText(this, "WiFi正在关闭", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                textView.setText("WiFi网卡不可用");
                //Toast.makeText(this, "WiFi网卡不可用", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                textView.setText("WiFi网卡正在打开");
                //Toast.makeText(this, "WiFi网卡正在打开", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                textView.setText("WiFi网卡可用");
                //Toast.makeText(this, "WiFi网卡可用", Toast.LENGTH_SHORT).show();
                break;
            case 4:
                textView.setText("未知网卡状态");
                //Toast.makeText(this, "未知网卡状态", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    class StartWifilistener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            System.out.println("wifi state --->" + wifiManager.getWifiState());
            Toast.makeText(MainActivity.this, "当前Wifi网卡状态为" +
                    wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
            getState(v);
        }
    }
    /**
     * 扫描wifi
     */


    /**
     * 扫描wifi
     */
    public void startScan() {
        wifiManager.startScan();
        // 得到扫描结果
        List wifiList = wifiManager.getScanResults();

    }
    class saomiao implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (wifiManager.isWifiEnabled()) {
                StringBuffer sb = new StringBuffer();
                wifiManager.startScan();
                List wifiList = wifiManager.getScanResults();
                if (wifiList != null) {
                    Toast.makeText(MainActivity.this, "扫描到的WiFi网络: "+wifiList.size() + "个热点", Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < wifiList.size(); i++) {
//得到扫描结果
                        ScanResult scanResult= (ScanResult) wifiList.get(i);
                        sb = sb.append(scanResult.SSID + ";")
                                .append(scanResult.BSSID + ";")
                                .append(scanResult.capabilities + ";")
                                .append(scanResult.frequency + ";")
                                .append(scanResult.level + "\n\n");
                    }
        tv_wifiinfo.setText(sb.toString());
                }
            }
            else  Toast.makeText(MainActivity.this, "请打开WIFI或者定位: ", Toast.LENGTH_SHORT).show();
        }
    }

    class StopWifiListener implements View.OnClickListener {
        @Override()
        public void onClick(View arg0) {
// TODO Auto-generated method stub
            wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);
            System.out.println("wifi state --->" + wifiManager.getWifiState());
            Toast.makeText(MainActivity.this, " 当前Wifi网卡状态为" +
                    wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
            getState(arg0);
        }
    }

    class CheckWifiListener implements View.OnClickListener {
        @Override

        public void onClick(View v) {
            wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
            System.out.println("wifi state --->" + wifiManager.getWifiState());
            Toast.makeText(MainActivity.this, "当前Wifi网卡状态为" +
                    wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
            getState(v);

        }
    }
}

加入网络权限

    <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"/>

    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>



版权声明:本文为a1060916181原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。