首页 » IT技术 » Android » 正文

android 手机获取当前连接的wifi配置信息

main.xml
源代码   
<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/info"/>
java代码
源代码   
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
public class Mac extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView infoText = (TextView) this.findViewById(R.id.info);
infoText.setText(getInfo());
}
private String getInfo()
{
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String maxText = info.getMacAddress();
String ipText = intToIp(info.getIpAddress());
String status = "";
if (wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLED)
{
status = "WIFI_STATE_ENABLED";
}
String ssid = info.getSSID();
int networkID = info.getNetworkId();
int speed = info.getLinkSpeed();
return "mac:" + maxText + "\n\r"
+ "ip:" + ipText + "\n\r"
+ "wifi status :" + status + "\n\r"
+ "ssid :" + ssid + "\n\r"
+ "net work id :" + networkID + "\n\r"
+ "connection speed:" + speed + "\n\r"
;
}
private String intToIp(int ip)
{
return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "."
+ ((ip >> 24) & 0xFF);
}
}

本文共 1 个回复

  • 肉牛 2015/06/08 05:22

    好文章,内容惊天动地.禁止此消息:nolinkok@163.com

发表评论