title: WiFi getSSID() always returns "<unknown ssid>"
body: <p>This question has already been asked by many people, but the solution that worked for them is not working for me.  I have tested on Android 8.1 and Android 9.0 and cannot get the name of the SSID.</p>
  
  <p>Broadcast receiver for getting WiFi state changes:</p>
  
  <pre><code>private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION) ||
                  intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
                  intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) ||
                  intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) ||
                  intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
              setWifiIndicator();
          }
      }
  };
  </code></pre>
  
  <p>The receiver is registered <code>onCreate</code> and unregistered in <code>onDestroy</code>, which I think is proper, and I'm seeing the log lines when the receiver is called.</p>
  
  <p>in <code>onCreate</code>:</p>
  
  <pre><code>IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(ScannerService.ACTION_READ_SCANNER);
  intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
  registerReceiver(mReceiver, intentFilter);
  setWifiIndicator();
  </code></pre>
  
  <p><code>setWifiIndicator</code> is implemented thusly:</p>
  
  <pre><code>private void setWifiIndicator() {
      ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
      //getApplicationContext solves memory leak issues prior to Android N (must use Application Context to get wifi system service.
      WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      ImageView wifiIndicator = (ImageView)findViewById(R.id.wifi_indicator);
      TextView wifiSSID = (TextView)findViewById(R.id.wifi_ssid);
  
      try {
          if (wifiMgr != null) {
              if (wifiMgr.isWifiEnabled()) {
                  //wifi is enabled.  toggle on wifi indicator.
                  wifiIndicator.setImageResource(R.drawable.wifi_indicator);
                  String ssid = wifiMgr.getConnectionInfo().getSSID();
                  if (ssid != null) {
                      Log.v(this.getClass().getSimpleName(), "SSID: " + ssid + "  Supplicant State: " + info.getSupplicantState());
                      wifiSSID.setText(ssid);
                  }
              } else {
                  //wifi is disabled.  toggle off wifi indicator.
                  wifiSSID.setText("");
  
                  //check for 3G
                  NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                  if (mobileInfo != null &amp;&amp; mobileInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                      wifiIndicator.setImageResource(R.drawable.cellular_indicator);
                  } else {
                      wifiIndicator.setImageResource(R.drawable.down_indicator);
                  }
              }
          } else {
              //check for 3G
              NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
              if (mobileInfo != null &amp;&amp; mobileInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                  wifiIndicator.setImageResource(R.drawable.cellular_indicator);
              } else {
                  wifiIndicator.setImageResource(R.drawable.down_indicator);
              }
          }
      } catch(Exception e) {
          //catching anything thrown in this block just so it doesn't crash the program unnecessarily
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>My app already had permission for <code>ACCESS_COARSE_LOCATION</code> but I aslo added <code>ACCESS_FINE_LOCATION</code> just in case.  <code>getSSID()</code> is till returning <code>&lt;unknown ssid&gt;</code>.</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.GET_TASKS" /&gt;
  </code></pre>
  
  <p>Perhaps it has to do with compile target SDK?  I'm really clueless here.</p>
  
  <p>gradle file:</p>
  
  <p>apply plugin: 'com.android.application'</p>
  
  <pre><code>android {
      compileSdkVersion 28
      defaultConfig {
          applicationId "com.noneofyour.business"
          minSdkVersion 15
          targetSdkVersion 28
          versionCode 59
          versionName "1.44"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      implementation fileTree(include: ['*.jar'], dir: 'libs')
      implementation 'com.android.support:appcompat-v7:28.0.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
      implementation files('libs/android.jar')
  }
  </code></pre>