Android WiFi Manager

提供: KiyomiWiki
移動: 案内検索

概要

Wi-Fi のON/OFF及びWi-FiのスキャンをするAndroidアプリのサンプルソースコードです。

開発環境

Android Studio 1.5.1
動作確認端末:Nexus 5 Android 5.1.1

ソースコード

  • MainActivity.java
package net.kiyomi2013.wifimanager;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Switch;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn_scan;
    Switch sw_wifi;
    WifiManager wifiManager;
    boolean disp = false;
    Context myContext;
    BroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myContext = this;

        sw_wifi = (Switch)findViewById(R.id.sw_wifi);
        sw_wifi.setOnClickListener(this);

        btn_scan = (Button)findViewById(R.id.button_scan);
        btn_scan.setOnClickListener(this);

        wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);

        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(wifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        iFilter.addAction(wifiManager.WIFI_STATE_CHANGED_ACTION);

        mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    if(wifiManager.isWifiEnabled()) {
                        sw_wifi.setChecked(true);
                        disp = true;
                        wifiManager.startScan();
                    }
                    else {
                        sw_wifi.setChecked(false);
                        disp = false;
                        ListView lv = (ListView)findViewById(R.id.listView_scan);
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(myContext, android.R.layout.simple_list_item_multiple_choice);
                        lv.setAdapter(adapter);
                    }

                } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                    if (disp) {
                        ListView lv = (ListView) findViewById(R.id.listView_scan);
                        List<ScanResult> apList = wifiManager.getScanResults();
                        String[] aps = new String[apList.size()];
                        for (int i = 0; i < apList.size(); i++) {
                            aps[i] = "SSID:" + apList.get(i).SSID + "  "
                                    + apList.get(i).frequency + "MHz  "
                                    + apList.get(i).level + "dBm" + "\n"
                                    + apList.get(i).capabilities;
                        }
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(myContext, android.R.layout.simple_list_item_1, aps);
                        lv.setAdapter(adapter);
                    }
                }
            }
        };

        if(wifiManager.isWifiEnabled()) {
            sw_wifi.setChecked(true);
            try{
            }catch(Exception e){}
            disp = true;
            wifiManager.startScan();
        }
        else {
            sw_wifi.setChecked(false);
        }
        this.registerReceiver(mReceiver, iFilter);
    }

    @Override
    public void onClick(View v) {
        if ( v == sw_wifi) {
            if (sw_wifi.isChecked()) {
                wifiManager.setWifiEnabled(true);
                try{
                }catch(Exception e){}
                disp = true;
                wifiManager.startScan();
            }
            else {
                disp = false;
                wifiManager.setWifiEnabled(false);
                ListView lv = (ListView)findViewById(R.id.listView_scan);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
                lv.setAdapter(adapter);
            }

        }

        if (v == btn_scan) {
            wifiManager.startScan();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(mReceiver);
    }

}
  • activity_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: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="net.kiyomi2013.wifimanager.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wi-Fi"
            android:id="@+id/sw_wifi"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SCAN"
            android:id="@+id/button_scan"
            android:layout_marginLeft="20dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView_scan" />
    </LinearLayout>
</LinearLayout>
  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.kiyomi2013.wifimanager">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>