android 获取wifi信息
16lz
2021-01-26
package cn.yws.getwifi;import java.lang.reflect.Field;import java.net.InetAddress;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.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private static final int REQUEST_ENABLE_BT = 3;private WifiManager mWifi;private BluetoothAdapter bAdapt;private String btMac;private String WifiMac;private TextView mac;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button = (Button) this.findViewById(R.id.button1);button.setOnClickListener(new ButtonClickListener());mac = (TextView) findViewById(R.id.macView);mWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);bAdapt = BluetoothAdapter.getDefaultAdapter();GetWifi();}class ButtonClickListener implements OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1: {GetWifi();}break;default:break;}}}public void GetWifi() {if (!mWifi.isWifiEnabled()) {mWifi.setWifiEnabled(true);}WifiInfo wifiInfo = mWifi.getConnectionInfo();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";}mac.setTextSize(16);String ipString = "";// 本机在WIFI状态下路由分配给的IP地址 /**获得IP地址的方法一: int ipAddress = wifiInfo.getIpAddress(); if (ipAddress != 0) { ipString = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff)); }*///Log.i(this.getClass().getSimpleName(), TypeUtil.typeToString("wifiInfo", wifiInfo));// 获得IP地址的方法二(反射的方法): try { Field field = wifiInfo.getClass().getDeclaredField("mIpAddress"); field.setAccessible(true); InetAddress address = (InetAddress) field.get(wifiInfo); ipString=address.getHostAddress(); System.out.println("ipString:" + ipString); } catch (Exception e) { e.printStackTrace(); } // 查看已经连接上的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获取wifi IP 地址:" +ipString);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);}}
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.yws.getwifi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/macView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/macView" android:text="再次获取" /></RelativeLayout>
更多相关文章
- 47 Android(安卓)fragment 接口回调
- Android(安卓)Studio 蓝牙配对
- 自定义数据适配器
- Android(安卓)获取存储空间
- android 屏幕截屏源码分享
- Android之ListView优化
- android bluetooth开发基础-7管理连接
- Android(安卓)的通话代码练习
- Android(安卓)GPS工具 (暂存)