Android獲取當前連接的WIFI相關信息
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.widget.TextView;/**
- 獲取手機WIFI的MAC地址
@author 單紅宇 / public class GetmacipinfoActivity extends Activity { /* Called when the activity is first created. / private static final int REQUEST_ENABLE_BT = 3; private WifiManager mWifi; private BluetoothAdapter bAdapt; private String btMac; private String WifiMac;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main); mWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (!mWifi.isWifiEnabled()) { mWifi.setWifiEnabled(true); } WifiInfo wifiInfo = mWifi.getConnectionInfo(); bAdapt = BluetoothAdapter.getDefaultAdapter(); if (bAdapt != null) { if (!bAdapt.isEnabled()) { Intent enBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enBT, REQUEST_ENABLE_BT); } btMac = bAdapt.getAddress(); } else { btMac = "No Bluetooth Device!"; } if ((WifiMac = wifiInfo.getMacAddress()) == null) { WifiMac = "No Wifi Device"; } TextView mac = (TextView) findViewById(R.id.macView); mac.setTextSize(16);
// 查看已經連接上的WIFI信息,在Android的SDK中為我們提供了一個叫做WifiInfo的對象,這個對象可以通過WifiManager.getConnectionInfo()來獲取。WifiInfo中包含了當前連接中的相關信息。
// getBSSID() 獲取BSSID屬性
// getDetailedStateOf() 獲取客戶端的連通性
// getHiddenSSID() 獲取SSID 是否被隱藏
// getIpAddress() 獲取IP 地址
// getLinkSpeed() 獲取連接的速度
// getMacAddress() 獲取Mac 地址
// getRssi() 獲取802.11n 網絡的信號
// getSSID() 獲取SSID
// getSupplicanState() 獲取具體客戶端狀態的信息
StringBuffer sb = new StringBuffer();
sb.append("\n獲取BSSID屬性(所連接的WIFI設備的MAC地址):" + wifiInfo.getBSSID());
// sb.append("getDetailedStateOf() 獲取客戶端的連通性:");
sb.append("\n\n獲取SSID 是否被隱藏:"+ wifiInfo.getHiddenSSID());
sb.append("\n\n獲取IP 地址:" + wifiInfo.getIpAddress());
sb.append("\n\n獲取連接的速度:" + wifiInfo.getLinkSpeed());
sb.append("\n\n獲取Mac 地址(手機本身網卡的MAC地址):" + WifiMac);
sb.append("\n\n獲取802.11n 網絡的信號:" + wifiInfo.getRssi());
sb.append("\n\n獲取SSID(所連接的WIFI的網絡名稱):" + wifiInfo.getSSID());
sb.append("\n\n獲取具體客戶端狀態的信息:" + wifiInfo.getSupplicantState());
mac.setText("WIFI網絡信息: " + sb.toString() + "\n\n藍牙MAC: " + btMac);
}
}</pre>