代码很简单,通过wifiManager 获取wifiinfo,从而获取bssid,
public static String getWifiSSID(Context context) {
String bssid = "";
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
if (manager == null) {
return bssid;
}
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting() && activeNetworkInfo.getType() == TYPE_WIFI) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager == null) {
return bssid;
}
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
bssid = connectionInfo.getBSSID();
}
return bssid;
}
几个注意的地方吧:
1:getActiveNetworkInfo方法时需要添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2:直接通过上述方法执行时,会抛出RuntimeException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zh.wifiinfotest/com.zh.wifiinfotest.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10138 nor current process has android.permission.ACCESS_WIFI_STATE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3915)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4098)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2475)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8401)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087)
Caused by: java.lang.SecurityException: WifiService: Neither user 10138 nor current process has android.permission.ACCESS_WIFI_STATE.
at android.os.Parcel.createException(Parcel.java:2082)
at android.os.Parcel.readException(Parcel.java:2050)
at android.os.Parcel.readException(Parcel.java:1998)
at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:2374)
at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:2338)
at com.zh.wifiinfotest.MainActivity.getWifiSSID(MainActivity.java:39)
at com.zh.wifiinfotest.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:8121)
at android.app.Activity.performCreate(Activity.java:8109)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3888)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4098)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2475)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8401)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087)
Caused by: android.os.RemoteException: Remote stack trace:
at android.app.ContextImpl.enforce(ContextImpl.java:2025)
at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:2053)
at com.android.server.wifi.WifiServiceImpl.enforceAccessPermission(WifiServiceImpl.java:1130)
at com.android.server.wifi.WifiServiceImpl.getConnectionInfo(WifiServiceImpl.java:2740)
at android.net.wifi.IWifiManager$Stub.onTransact(IWifiManager.java:1047)
callee: null 4649/12306
添加权限android.permission.ACCESS_WIFI_STATE。
3:继续执行。这次获取到了bssid,但是取得的值却是默认值:02:00:00:00:00:00。
这是什么问题?
我们看下获取wifiInfo的方法:
/**
* Return dynamic information about the current Wi-Fi connection, if any is active.
* <p>
* In the connected state, access to the SSID and BSSID requires
* the same permissions as {@link #getScanResults}. If such access is not allowed,
* {@link WifiInfo#getSSID} will return {@link #UNKNOWN_SSID} and
* {@link WifiInfo#getBSSID} will return {@code "02:00:00:00:00:00"}.
* {@link WifiInfo#getPasspointFqdn()} will return null.
* {@link WifiInfo#getPasspointProviderFriendlyName()} will return null.
*
* @return the Wi-Fi information, contained in {@link WifiInfo}.
*/
public WifiInfo getConnectionInfo() {
try {
return mService.getConnectionInfo(mContext.getOpPackageName(),
mContext.getAttributionTag());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
可以看到注释中有这么一句话,获取bssid和ssid需要getScanResults相同的权限。
/**
* Return the results of the latest access point scan.
* @return the list of access points found in the most recent scan. An app must hold
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION} permission
* in order to get valid results.
*/
public List<ScanResult> getScanResults() {
try {
return mService.getScanResults(mContext.getOpPackageName(),
mContext.getAttributionTag());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
好吧,需要ACCESS_FINE_LOCATION权限。
添加权限后,获得bssid.
标签:java,BSSID,app,Wifi,ActivityThread,获取,return,android,os From: https://www.cnblogs.com/zhjing/p/17087024.html