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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 = "[email protected]";
    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的主要内容,如果未能解决你的问题,请参考以下文章

3-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(项目功能演示--远程升级WIFI模块程序)

3-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案基础篇(项目功能演示--GPRS )

Android开发—智能家居系列:UDP通信发送指令

WIFI模块开发教程之W600网络篇1:AP模式下TCP Client通信

WIFI模块开发教程之W600网络篇2:AP模式下TCP Server通信

Android开发—智能家居系列:用手机对WIFI模块进行配置