首页 > 代码库 > [android开发篇][ wifi模块] [3] wifiConnectAndCheck

[android开发篇][ wifi模块] [3] wifiConnectAndCheck

package com.softwinner.network.wifi;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.util.Log;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertTrue;

/**
 * @author liuzhipeng
 * Created by Administrator on 2017/7/24.
 */

public class connectAndCheckByJavaInterface {

    private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    private String ssid = "AW2";
    private String password = "1qaz@WSX";
    private String mLogTag = "testWifi";
    private Context context = InstrumentationRegistry.getContext();

    wifiBaseClass testWifi = new wifiBaseClass(context, mDevice, ssid, password, mLogTag);

    @Test
    public void addNetWorkAndConnect() throws Exception {
        Log.i(mLogTag, "start add network");
        testWifi.addNetworkAndConnect(wifiBaseClass.Security.PSK);
        for(int i = 1; i < 11; i++)
        {
            Thread.sleep(6000);
            Log.i(mLogTag, String.format("wait for wifi connected, run time %d second", 6 * i));
            if(wifiBaseClass.isWifiConnected())
            {
                Log.i(mLogTag, "wifi isConnected = " + wifiBaseClass.isWifiConnected());
                break;
            }
            if(i == 10){
                Log.i(mLogTag, "wifi connected fail");
                assertTrue(false);
            }
        }
        String wifiName = testWifi.getNetworkName();
        Log.i(mLogTag, "wifiName = " + wifiName);
        assertTrue("wifi name not equal" + ssid, wifiName.equals(ssid));
        String cmd = "ping -c 4 -w 5 www.baidu.com";
        Boolean isNetworkAvailable = testWifi.isNetworkAvailableByDevice(cmd);
        assertTrue("network is unavailable", isNetworkAvailable);
        Log.i(mLogTag, "isNetworkAvailable = " + isNetworkAvailable);

    }
}

 

[android开发篇][ wifi模块] [3] wifiConnectAndCheck