安卓的WIFI

import android.content.Context;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button open, close, shaomiao, info,gone;
    private TextView tv;
    private WifiManager wm;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找ID
        open = (Button) findViewById(R.id.open);
        close = (Button) findViewById(R.id.close);
        shaomiao = (Button) findViewById(R.id.shaomiao);
        info = (Button) findViewById(R.id.info);
        gone = (Button) findViewById(R.id.gone);
        tv = (TextView) findViewById(R.id.tv);
        //得到管理器
        wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        //打开wifi
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //如果WiFi没打开,则设置打开WiFi
                if (!wm.isWifiEnabled()) {
                    wm.setWifiEnabled(true);
                }
            }
        });

        //关闭wifi
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果WiFi打开,则设置关闭WiFi
                if (wm.isWifiEnabled()) {
                    wm.setWifiEnabled(false);
                }
            }
        });

        //扫描wifi
        shaomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开始扫描
                wm.startScan();
                //得到扫描的结果
                List<ScanResult> scanResults = wm.getScanResults();

                StringBuffer sb = new StringBuffer();

                for (ScanResult sr:scanResults){

                    sb.append(sr.BSSID+",,"+sr.SSID+",,"+"\r\n");
                }



                tv.setText(scanResults.toString());
            }
        });
        //得到链接WiFi的信息
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                WifiInfo connectionInfo = wm.getConnectionInfo();

                String bssid = connectionInfo.getBSSID();
                int rssi = connectionInfo.getRssi();
                int ipAddress = connectionInfo.getIpAddress();
                String ssid = connectionInfo.getSSID();

             tv.setText(bssid+"\n"+rssi+"\n"+ipAddress+"\n"+ssid);

            }
        });

        //获取以前链接过的WiFi的信息
        gone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<WifiConfiguration> configuredNetworks = wm.getConfiguredNetworks();
                StringBuffer sb = new StringBuffer();

                for (WifiConfiguration cw:configuredNetworks){
                    sb.append(cw.BSSID+",,"+cw.SSID+",,"+cw.status);

                }
               tv.setText(sb.toString());
            }
        });








        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值