Skip to content

Commit

Permalink
termux-wifi-scaninfo needs Location enabled
Browse files Browse the repository at this point in the history
Due to https://issuetracker.google.com/issues/37060483:

        WifiManager#getScanResults() returns an empty array list
        if GPS is turned off

the user needs to enable Location on the device for termux-wifi-scaninfo
to work. So show "Location needs to be enabled on the device" if necessary.
  • Loading branch information
fornwall committed Jun 5, 2018
1 parent 0ead304 commit de8d68a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/com/termux/api/WifiAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
Expand Down Expand Up @@ -43,6 +44,11 @@ public void writeJson(JsonWriter out) throws Exception {
});
}

static boolean isLocationEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

static void onReceiveWifiScanInfo(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {
@Override
Expand All @@ -51,6 +57,11 @@ public void writeJson(JsonWriter out) throws Exception {
List<ScanResult> scans = manager.getScanResults();
if (scans == null) {
out.beginObject().name("API_ERROR").value("Failed getting scan results").endObject();
} else if (scans.isEmpty() && !isLocationEnabled(context)) {
// https://issuetracker.google.com/issues/37060483:
// "WifiManager#getScanResults() returns an empty array list if GPS is turned off"
String errorMessage = "Location needs to be enabled on the device";
out.beginObject().name("API_ERROR").value(errorMessage).endObject();
} else {
out.beginArray();
for (ScanResult scan : scans) {
Expand Down

0 comments on commit de8d68a

Please sign in to comment.