天天看點

基于Android studio的WIFI搜尋顯示與WIFI打開

效果圖

基于Android studio的WIFI搜尋顯示與WIFI打開
基于Android studio的WIFI搜尋顯示與WIFI打開

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