Skip to content

Commit

Permalink
fix get WifiInfo on Android 9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
XuXiangJun committed Oct 9, 2018
1 parent 49d6bbb commit f467e1a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
buildToolsVersion '28.0.2'

defaultConfig {
applicationId "com.espressif.iot_esptouch_demo"
Expand Down
Expand Up @@ -10,6 +10,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
Expand Down Expand Up @@ -73,11 +74,24 @@ public void onReceive(Context context, Intent intent) {
return;
}

WifiManager wifiManager = (WifiManager) context.getApplicationContext()
.getSystemService(WIFI_SERVICE);
assert wifiManager != null;

switch (action) {
case WifiManager.NETWORK_STATE_CHANGED_ACTION:
WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
WifiInfo wifiInfo;
if (intent.hasExtra(WifiManager.EXTRA_WIFI_INFO)) {
wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
} else {
wifiInfo = wifiManager.getConnectionInfo();
}
onWifiChanged(wifiInfo);
break;
case LocationManager.PROVIDERS_CHANGED_ACTION:
onWifiChanged(wifiManager.getConnectionInfo());
onLocationChanged();
break;
}
}
};
Expand All @@ -103,7 +117,7 @@ protected void onCreate(Bundle savedInstanceState) {
TextView versionTV = findViewById(R.id.version_tv);
versionTV.setText(IEsptouchTask.ESPTOUCH_VERSION);

if (Build.VERSION.SDK_INT >= 28) {
if (isSDKAtLeastP()) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
String[] permissions = {
Expand Down Expand Up @@ -145,8 +159,15 @@ protected void onDestroy() {
}
}

private boolean isSDKAtLeastP() {
return Build.VERSION.SDK_INT >= 28;
}

private void registerBroadcastReceiver() {
IntentFilter filter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
if (isSDKAtLeastP()) {
filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
}
registerReceiver(mReceiver, filter);
mReceiverRegistered = true;
}
Expand Down Expand Up @@ -192,6 +213,22 @@ private void onWifiChanged(WifiInfo info) {
}
}

private void onLocationChanged() {
boolean enable;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager == null) {
enable = false;
} else {
boolean locationGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean locationNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
enable = locationGPS || locationNetwork;
}

if (!enable) {
mMessageTV.setText(R.string.location_disable_message);
}
}

@Override
public void onClick(View v) {
if (v == mConfirmBtn) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -10,4 +10,5 @@
<string name="package_multicast">Multicast</string>
<string name="confirm">Confirm</string>
<string name="wifi_5g_message">Device dose not support 5G Wifi</string>
<string name="location_disable_message">Location(GPS) is disable</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.0'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu Apr 05 19:47:58 CST 2018
#Mon Oct 08 17:53:18 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
Binary file modified releases/apk/esptouch.apk
Binary file not shown.

0 comments on commit f467e1a

Please sign in to comment.