这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 62 additions & 48 deletions app/src/main/java/com/termux/api/WifiAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.JsonWriter;
import android.location.LocationManager;

import com.termux.api.util.ResultReturner;

Expand All @@ -23,8 +24,9 @@ public void writeJson(JsonWriter out) throws Exception {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
out.beginObject();
if (info == null) {
out.name("API_ERROR").value("No current connection");

if (info == null || info.getSupplicantState().toString() == "SCANNING") {
out.name("API_ERROR").value("No current wifi connection available");
} else {
out.name("bssid").value(info.getBSSID());
out.name("frequency_mhz").value(info.getFrequency());
Expand All @@ -49,53 +51,65 @@ static void onReceiveWifiScanInfo(TermuxApiReceiver apiReceiver, final Context c
public void writeJson(JsonWriter out) throws Exception {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scans = manager.getScanResults();
if (scans == null) {
out.beginObject().name("API_ERROR").value("Failed getting scan results").endObject();
} else {
out.beginArray();
for (ScanResult scan : scans) {
out.beginObject();
out.name("bssid").value(scan.BSSID);
out.name("frequency_mhz").value(scan.frequency);
out.name("rssi").value(scan.level);
out.name("ssid").value(scan.SSID);
out.name("timestamp").value(scan.timestamp);
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}

if (!gps_enabled) {
out.beginObject().name("API_ERROR").value("Geolocation should be enabled").endObject();
}
else {
if (scans == null) {
out.beginObject().name("API_ERROR").value("Failed getting scan results").endObject();
}
else {
out.beginArray();
for (ScanResult scan : scans) {
out.beginObject();
out.name("bssid").value(scan.BSSID);
out.name("frequency_mhz").value(scan.frequency);
out.name("rssi").value(scan.level);
out.name("ssid").value(scan.SSID);
out.name("timestamp").value(scan.timestamp);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int channelWidth = scan.channelWidth;
String channelWidthMhz = "???";
switch (channelWidth) {
case ScanResult.CHANNEL_WIDTH_20MHZ:
channelWidthMhz = "20";
break;
case ScanResult.CHANNEL_WIDTH_40MHZ:
channelWidthMhz = "40";
break;
case ScanResult.CHANNEL_WIDTH_80MHZ:
channelWidthMhz = "80";
break;
case ScanResult.CHANNEL_WIDTH_80MHZ_PLUS_MHZ:
channelWidthMhz = "80+80";
break;
case ScanResult.CHANNEL_WIDTH_160MHZ:
channelWidthMhz = "160";
break;
}
out.name("channel_bandwidth_mhz").value(channelWidthMhz);
if (channelWidth != ScanResult.CHANNEL_WIDTH_20MHZ) {
// centerFreq0 says "Not used if the AP bandwidth is 20 MHz".
out.name("center_frequency_mhz").value(scan.centerFreq0);
}
if (!TextUtils.isEmpty(scan.operatorFriendlyName)) {
out.name("operator_name").value(scan.operatorFriendlyName.toString());
}
if (!TextUtils.isEmpty(scan.venueName)) {
out.name("venue_name").value(scan.venueName.toString());
}
}
out.endObject();
}
out.endArray();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int channelWidth = scan.channelWidth;
String channelWidthMhz = "???";
switch (channelWidth) {
case ScanResult.CHANNEL_WIDTH_20MHZ:
channelWidthMhz = "20";
break;
case ScanResult.CHANNEL_WIDTH_40MHZ:
channelWidthMhz = "40";
break;
case ScanResult.CHANNEL_WIDTH_80MHZ:
channelWidthMhz = "80";
break;
case ScanResult.CHANNEL_WIDTH_80MHZ_PLUS_MHZ:
channelWidthMhz = "80+80";
break;
case ScanResult.CHANNEL_WIDTH_160MHZ:
channelWidthMhz = "160";
break;
}
out.name("channel_bandwidth_mhz").value(channelWidthMhz);
if (channelWidth != ScanResult.CHANNEL_WIDTH_20MHZ) {
// centerFreq0 says "Not used if the AP bandwidth is 20 MHz".
out.name("center_frequency_mhz").value(scan.centerFreq0);
}
if (!TextUtils.isEmpty(scan.operatorFriendlyName)) {
out.name("operator_name").value(scan.operatorFriendlyName.toString());
}
if (!TextUtils.isEmpty(scan.venueName)) {
out.name("venue_name").value(scan.venueName.toString());
}
}
out.endObject();
}
out.endArray();
}
}
}
});
Expand Down