Android12 (S) 获取wifi名称(SSID)的方法

目录

  • 概述
  • 问题分析
  • 总结

概述

在最近开发过程中需要获取当前wifi的SSID,目前网上一般推荐 mWifiManager.getConnectionInfo() 这个方法来进行获取,但是发现在Android12上这个方法已经被标记为过时,本着用最新方法的想法,决定使用推荐的新方法试试。

问题分析

一言不合看源码,既然该方法被标记为过时,那么应该也会有推荐的方法来使用,源码如下:

 /**
    //path:packages/modules/Wifi/framework/java/android/net/wifi/WifiManager.java
     * Return dynamic information about the current Wi-Fi connection, if any is active.
     * 

* * @return the Wi-Fi information, contained in {@link WifiInfo}. * * @deprecated Starting with {@link Build.VERSION_CODES#S}, WifiInfo retrieval is moved to * {@link ConnectivityManager} API surface. WifiInfo is attached in * {@link NetworkCapabilities#getTransportInfo()} which is available via callback in * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} or on-demand from * {@link ConnectivityManager#getNetworkCapabilities(Network)}. * *

* Usage example: *
{@code
     * final NetworkRequest request =
     *      new NetworkRequest.Builder()
     *      .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
     *      .build();
     * final ConnectivityManager connectivityManager =
     *      context.getSystemService(ConnectivityManager.class);
     * final NetworkCallback networkCallback = new NetworkCallback() {
     *      ...
     *      {@literal @}Override
     *      void onAvailable(Network network) {}
     *
     *      {@literal @}Override
     *      void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
     *          WifiInfo wifiInfo = (WifiInfo) networkCapabilities.getTransportInfo();
     *      }
     *      // etc.
     * };
     * connectivityManager.requestNetwork(request, networkCallback); // For request
     * connectivityManager.registerNetworkCallback(request, networkCallback); // For listen
     * }
*

* Compatibility Notes: *

  • Apps can continue using this API, however newer features * such as ability to mask out location sensitive data in WifiInfo will not be supported * via this API.
  • *
  • On devices supporting concurrent connections (indicated via * {@link #isStaConcurrencyForLocalOnlyConnectionsSupported()}, etc) this API will return * the details of the internet providing connection (if any) to all apps, except for the apps * that triggered the creation of the concurrent connection. For such apps, this API will return * the details of the connection they created. e.g. apps using {@link WifiNetworkSpecifier} will * trigger a concurrent connection on supported devices and hence this API will provide * details of their peer to peer connection (not the internet providing connection). This * is to maintain backwards compatibility with behavior on single STA devices.
  • *

    */
    @Deprecated public WifiInfo getConnectionInfo() { try { return mService.getConnectionInfo(mContext.getOpPackageName(), mContext.getAttributionTag()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }

    根据注释里的提示,谷歌推荐使用NetworkCallback监听网络状态的方法来获取wifi 的ssid,照着写一下,如下:

     final NetworkRequest request =
               new NetworkRequest.Builder()
               .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
               .build();
    
     final ConnectivityManager.NetworkCallback mNetworkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
            }
    
            @Override
            public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(network, networkCapabilities);
                WifiInfo wifiInfo = (WifiInfo) networkCapabilities.getTransportInfo();
                if (wifiInfo != null) {
                String ssid = wifiInfo.getSSID().replace("\"", "").replace("<", "").replace(">", ""));
                } 
            }
        };
    
    	private void requestNetwork() {
            mConnectivityManager.registerNetworkCallback(mRequest, mNetworkCallback);
        }
        
    	 private void unrequestNetwork() {
            mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
        }
    
    

    本以为万事大吉,一切OK,没想到install以后拿到的ssid 一直是 unknow ssid。对于这种情况,第一想法是应用是否缺少权限?查看WifiInfo源码,发现在应用没有"Manifest.permission.ACCESS_FINE_LOCATION"权限时,ssid的确会返回Unknown Ssid。难道这就解决了?看了一下应用的清单文件,发现该有的权限配置都是有的。这~ 给我整不会了!
    纠结了好几天,始终没有找到解决问题的办法。
    偶然在查看NetworkCallback源码的时候好像发现了新大陆。

    //path:packages/modules/Connectivity/framework/src/android/net/ConnectivityManager.java
        /**
         * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
         * changes. Should be extended by applications wanting notifications.
         *
         * A {@code NetworkCallback} is registered by calling
         * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
         * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
         * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
         * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
         * A {@code NetworkCallback} should be registered at most once at any time.
         * A {@code NetworkCallback} that has been unregistered can be registered again.
         */
        public static class NetworkCallback {
            /**
             * No flags associated with this callback.
             * @hide
             */
            public static final int FLAG_NONE = 0;
            /**
             * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
             * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
             * 

    * These include: *

  • Some transport info instances (retrieved via * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo} * contain location sensitive information. *
  • OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).
  • *

    *

    * Note: *

  • Retrieving this location sensitive information (subject to app's location * permissions) will be noted by system.
  • *
  • Without this flag any {@link NetworkCapabilities} provided via the callback does * not include location sensitive info. *

    */ // Note: Some existing fields which are location sensitive may still be included without // this flag if the app targets SDK < S (to maintain backwards compatibility). public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0; /** @hide */ @Retention(RetentionPolicy.SOURCE) @IntDef(flag = true, prefix = "FLAG_", value = { FLAG_NONE, FLAG_INCLUDE_LOCATION_INFO }) public @interface Flag { } /** * All the valid flags for error checking. */ private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO; public NetworkCallback() { this(FLAG_NONE); } public NetworkCallback(@Flag int flags) { if ((flags & VALID_FLAGS) != flags) { throw new IllegalArgumentException("Invalid flags"); } mFlags = flags; } ...
  • 发现NetworkCallback的构造方法中是可以传参数的,并且根据注释可知 FLAG_INCLUDE_LOCATION_INFO 这个flag就是控制onCapabilitiesChanged时 NetworkCapabilities#getTransportInfo() 可以传递一些敏感的位置信息,而获取WifiInfo中的ssid的确需要位置信息。似乎?这就是正解?赶紧加进去试试。

    final ConnectivityManager.NetworkCallback mNetworkCallback = new ConnectivityManager.NetworkCallback(ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO) {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
            }
    
            @Override
            public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(network, networkCapabilities);
                WifiInfo wifiInfo = (WifiInfo) networkCapabilities.getTransportInfo();
                if (wifiInfo != null) {
                String ssid = wifiInfo.getSSID().replace("\"", "").replace("<", "").replace(">", ""));
                } 
            }
        };
    

    方法相同就是在创建NetworkCallback 时传入FLAG_INCLUDE_LOCATION_INFO。
    编译,install,打开应用,ssid完美呈现。
    至此问题解决,这就是正解。

    总结

    遇事不决阅读源码,百思不解可读源码。

    你可能感兴趣的:(Android,S,java,android)