AndroidStudio中API29及更高版本下WifiManager无法工作
创始人
2024-08-20 13:30:15
0

问题描述:

在 Android Studio 中,如果您使用了 API 29 及更高版本,可能会遇到 WifiManager 无法工作的问题。具体表现为获取到的 Wifi 信息为空或为 null。

解决方案:

以下是一个可以解决此问题的示例代码:

public class WifiUtils {

    /**
     * 获取 WifiManager 实例
     * @param context Context 对象
     * @return WifiManager 实例
     */
    private static WifiManager getWifiManager(Context context) {
        return (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }

    /**
     * 获取 WifiInfo 实例
     * @param context Context 对象
     * @return WifiInfo 实例
     */
    public static WifiInfo getWifiInfo(Context context) {
        WifiManager wifiManager = getWifiManager(context);
        return (wifiManager != null) ? wifiManager.getConnectionInfo() : null;
    }

    /**
     * 获取 Wifi 名称
     * @param context Context 对象
     * @return Wifi 名称
     */
    public static String getWifiName(Context context) {
        WifiInfo wifiInfo = getWifiInfo(context);
        return (wifiInfo != null) ? wifiInfo.getSSID().replace("\"", "") : null;
    }

    /**
     * 获取 Wifi MAC 地址
     * @param context Context 对象
     * @return Wifi MAC 地址
     */
    public static String getWifiMacAddress(Context context) {
        WifiInfo wifiInfo = getWifiInfo(context);
        return (wifiInfo != null) ? wifiInfo.getMacAddress() : null;
    }

}

通过上述代码,可以使用 WifiUtils 中的方法获取到 Wifi 名称和 MAC 地址。在使用之前需要添加相应的权限:



参考文献:

[1] https://stackoverflow.com/questions/64606698/get-current-wifi-name-in-android-q-10-returns-null

[2] https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-api

相关内容

热门资讯

总结透视!拱趴大菠萝万能挂!原... 总结透视!拱趴大菠萝万能挂!原来一直都是有辅助神器(有挂助手)-哔哩哔哩1、拱趴大菠萝万能挂免费脚本...
揭露透视!wepoker怎么提... 揭露透视!wepoker怎么提高运气,aapoker透视脚本下载,手段教程(有挂攻略)-哔哩哔哩所有...
解谜透视!hardrock作必... 解谜透视!hardrock作必弊!好像是真的有辅助教程(证实有挂)-哔哩哔哩1、超多福利:超高返利,...
详细透视!agpoker辅助,... 详细透视!agpoker辅助,hhpoker透视工具,步骤教程(有挂讲解)-哔哩哔哩hhpoker透...
普及透视!hardrock作必... 普及透视!hardrock作必弊!切实一直总是有辅助app(竟然有挂)-哔哩哔哩暗藏猫腻,小编详细说...
揭幕透视!红龙poker有辅助... 揭幕透视!红龙poker有辅助吗,哈糖大菠萝有挂吗,练习教程(真的有挂)-哔哩哔哩揭幕透视!红龙po...
详细透视!pokemmo修改器... 详细透视!pokemmo修改器手机版!一直真的是有辅助工具(有挂方法)-哔哩哔哩1、许多玩家不知道p...
总结透视!hhpoker是内部... 总结透视!hhpoker是内部控制吗,aapoker怎么开辅助器,操作教程(有挂功能)-哔哩哔哩hh...
教你透视!pokemmo辅助器... 教你透视!pokemmo辅助器手机版下载!一直是有辅助软件(详细教程)-哔哩哔哩1、每一步都需要思考...
辅助透视!sohoo辅助,po... 辅助透视!sohoo辅助,pokemmo脚本辅助下载,项目教程(有挂详细)-哔哩哔哩1、实时poke...