简体   繁体   English

在奥利奥 (8.1.0) 上没有获得正确的 Wifi SSID。 它显示<unknown ssid>虽然它连接到带有 SSID 的 wifi

[英]On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing <unknown ssid> though it is connected to a wifi with SSID

I need to check the current connected wifi SSID on android.我需要在android上检查当前连接的wifi SSID。 I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0.我分别用 Oreo 8.1.0 和 Oreo 8.0 检查了诺基亚 6 和 OnePlus 5。 Other phones with different OS version is working fine with this code.具有不同操作系统版本的其他手机可以使用此代码正常工作。 Is there anything wrong with my code?我的代码有什么问题吗?

private WifiInfo wifiInfo;
private String ssid = "";
private WifiManager wifiManager;

private boolean getWifiStatus() {
    wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiInfo = wifiManager.getConnectionInfo();
    ssid = "";
    ssid = wifiInfo.getSSID();
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isWiFi = false;
    if(activeNetwork != null){
        isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    }

    Log.d(TAG, "getWifiStatus: " + ssid);
    if(ssid.contains("TripleMZim") && wifiManager.isWifiEnabled() && isWiFi ){
        return true;
    }
    else{
        return false;
    }
}

permission in Manifest file:清单文件中的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

There are different approaches how you can get the WIFI information (such as SSID) in android.在android中有多种获取WIFI信息(如SSID)的方法。

Some of them already listed here, for example WifiManager service.其中一些已经在此处列出,例如WifiManager服务。

But for OP question:但对于 OP 问题:

Is there anything wrong with my code?我的代码有什么问题吗?

No, the problem is not in your code.不,问题不在您的代码中。 The main reason for the unexpected behavior, is because of the security patch of the last android releases.出现意外行为的主要原因是上一个 android 版本的安全补丁。

Before the patch (or any device that didn't get an update), the hackers could steal sensitive information that was leaked through the wifi info.在补丁(或任何未获得更新的设备)之前,黑客可以窃取通过 wifi 信息泄露的敏感信息。

For example the hackers could obtain the device geolocation, that should be accessible only with the Dangerous Permission LOCATION , that should be requested at runtime and can be revoked at any moment.例如,黑客可以获得设备地理位置,该地理位置应该只能通过Dangerous Permission LOCATION访问,应该在运行时请求并且可以随时撤销。 But in the other hand - the WifiInfo is accessible only with the Normal Permission android.permission.ACCESS_WIFI_STATE which is granted at install time and cannot be revoked.但另一方面 - WifiInfo只能通过正常权限android.permission.ACCESS_WIFI_STATE访问,该权限在安装时授予且无法撤销。 That way, hackers could bypass the permission mechanism and access the data from dangerous permission using only normal permission.这样,黑客就可以绕过权限机制,只使用正常权限就可以从危险权限中访问数据。

The issue was reported and tracked by CVE-2018-9489 , you can read more about it here: Sensitive information expose via WIFI该问题已由CVE-2018-9489报告和跟踪,您可以在此处阅读更多相关信息: 通过 WIFI 暴露的敏感信息

So the reason why you having those problems is because now android blocks the sensitive wifi info, unless the app holds the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION .所以你遇到这些问题的原因是因为现在 android 阻止了敏感的 wifi 信息,除非应用程序持有ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION

So, if you request those permissions explicitly (and allowed by the user), your code should work fine.因此,如果您明确请求这些权限(并且由用户允许),您的代码应该可以正常工作。

For example:例如:

Manifest舱单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Activity活动

private static final int LOCATION = 1;
protected void onStart() {
   super.onStart();
   //Assume you want to read the SSID when the activity is started
   tryToReadSSID();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == LOCATION){
        //User allowed the location and you can read it now
        tryToReadSSID();
    }
}

private void tryToReadSSID() {
    //If requested permission isn't Granted yet
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //Request permission from user
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);
    }else{//Permission already granted 
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
            String ssid = wifiInfo.getSSID();//Here you can access your SSID
            System.out.println(ssid);
        }
    }
}

Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before being able to retrieve the connected SSID as it can infer the users location. Oreo 8.1+ 设备需要粗略位置运行时权限以及位置服务才能检索连接的 SSID,因为它可以推断用户位置。

Further information is available here 可在此处获得更多信息

Which relates to this这与此有关

In Android Oreo you could use ConnectivityManager to know your wifi SSID.在 Android Oreo 中,您可以使用ConnectivityManager来了解您的 wifi SSID。

Permission权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Code to get SSID获取SSID的代码

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
    String ssid = info.getExtraInfo();
    Log.d(TAG, "WiFi SSID: " + ssid);
}

This is tested in Android Oreo 8.1.0.这是在 Android Oreo 8.1.0 中测试的。 You will get SSID enclosed with double quotes.您将获得用双引号括起来的 SSID。

You need to request permission in manifest :您需要在 manifest 中请求权限:

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and request it in your activity :并在您的活动中请求它:

    private void grantPermm()
{

    try
    {
        if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
                  Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)

        {

        }
        else
        {

            ActivityCompat.requestPermissions(MainScreenActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    101);
        }

    }
    catch (Exception xx){}

}

then use this function :然后使用这个功能:

    public  static String  getWifiName(Context context)
{
    String result="";
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (manager.isWifiEnabled())
        {
            WifiInfo wifiInfo = manager.getConnectionInfo();
            if (wifiInfo != null)
            {
                NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
                {
                    result = wifiInfo.getSSID();
                    if(result.equalsIgnoreCase("<unknown ssid>"))
                    {
                        Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
                        result="";
                    }

                    return result;
                }
                else
                {
                    Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
                    return "";
                }
            }
            else
            {
                Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
                return "";
            }
        }
        else
        {
            Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
            return "";
        }
    }
    catch (Exception xx)
    {
        Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
        return "";
    }
}

If you're targeting API 29 (Android 10) then the location permission requested must now be for ACCESS_FINE_LOCATION, not just ACCESS_COARSE_LOCATION.如果您的目标是 API 29 (Android 10),那么请求的位置权限现在必须针对 ACCESS_FINE_LOCATION,而不仅仅是针对 ACCESS_COARSE_LOCATION。 Most of the above sample code is fine, however some answers could be misleading in the text.上面的大部分示例代码都很好,但是有些答案可能会在文本中产生误导。

https://developer.android.com/about/versions/10/privacy/changes https://developer.android.com/about/versions/10/privacy/changes

If you face issues even after trying the Yaswant Narayan and behelit answers, make sure you have enabled the Location/GPS in the device while trying to get the SSID.如果您在尝试 Yaswant Narayan 和 behelit 答案后仍遇到问题,请确保在尝试获取 SSID 时已在设备中启用位置/GPS。 Even though, you have acquired the relevant permissions in Android Oreo and above devices, if the Location/GPS is turned off by the user, then you will get the from those devices.即使您已经在 Android Oreo 及以上设备中获得了相关权限,如果用户关闭了位置/GPS,那么您将从这些设备中获取。

You need to ask for runtime permission of read phone state.您需要请求读取手机状态的运行时权限。 This is included for better security.这是为了更好的安全性。

code is as follows:-代码如下:-

    TelephonyManager telephonyManager = (TelephonyManager) getContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    String IMEI ="";

    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
       ActivityCompat.requestPermissions(getActivity(),
               new String[]{Manifest.permission.READ_PHONE_STATE},
               123);

    }
    int permission = ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.READ_PHONE_STATE);
    if(permission == PackageManager.PERMISSION_GRANTED){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            IMEI = telephonyManager.getImei();
        }
        else{
            IMEI = telephonyManager.getDeviceId();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM