AndroLua学习(11)——网络管理

一、WIFI操作
1、WIFI相关信息

import "android.content.Context"
wifi = activity.Context.getSystemService(Context.WIFI_SERVICE)
wi1 = wifi.isWifiEnabled() --WIFI是否打开
wi2 = wifi.getWifiState() --WIFI状态
print(string.match(tostring(wifi.getConfiguredNetworks()),[[KeyMgmt: (.-) P]])) --WIFI加密类型
wifiInfo = wifi.getConnection

2、WIFI是否已连接
connManager = activity.getSystemService(Context.CONNECTIVITY_SERVICE)
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if tostring(mWifi):find(“none)”) then
–未连接
else
–连接
end

3、打开关闭wifi

import "android.content.Context"
wifi = activity.Context.getSystemService(Context.WIFI_SERVICE)
wifi.setWifiEnabled(true)--关闭则false

4、wifi信号强度

wifiManager=activity.Context.getSystemService(Context.WIFI_SERVICE);
wifiInfo=wifiManager.getConnectionInfo();
print(wifiInfo.getRssi())

5、网络名称

wifiManager=activity.Context.getSystemService(Context.WIFI_SERVICE);
wifiInfo=wifiManager.getConnectionInfo();
print(wifiInfo.getSSID())

6、SSID是否隐藏

wifiManager=activity.Context.getSystemService(Context.WIFI_SERVICE);
wifiInfo=wifiManager.getConnectionInfo();
print(wifiInfo.getHiddenSSID())

二、数据网络操作
1、数据网络是否连接

manager = activity.getSystemService(Context.CONNECTIVITY_SERVICE);  
gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
if tostring(gprs)== "CONNECTED" then
print"当前数据网络"
end

三、其他
1、查看IP
–查看某网站IP地址
address=InetAddress.getByName(“www.10010.com”);

–查看本机IP地址
address=InetAddress.getLocalHost();

–查看IP地址
wifi = activity.Context.getSystemService(Context.WIFI_SERVICE).getDhcpInfo()
string.match(tostring(wifi),”ipaddr(.-)gate”)

2、获取DNS
import “android.content.Context”

–获取Dns1
wifi = activity.Context.getSystemService(Context.WIFI_SERVICE).getDhcpInfo()
print(string.match(tostring(wifi),”dns1 (.-) dns2″))

–获取Dns2
wifi = activity.Context.getSystemService(Context.WIFI_SERVICE).getDhcpInfo()
dns2 = string.match(tostring(wifi),”dns2 (.-) D”)