我正在开发一个Android应用程序,我想让这个应用程序能够告诉我有多少设备连接到我所连接的当前接入点。什么是最好的解决方案。我不想要设备的细节而只需要连接到特定接入点的设备数量。我想将这些数据用于我的Web GUI,它将显示每个接入点的利用率趋势。请建议如何做到这一点。
到目前为止,我已经编写了应用程序,告诉我不同的信息,如ESSID,BSSID,信号强度,延迟等。我想添加另一个字段,告诉我连接到我连接的接入点的设备数量。到目前为止,我的代码如下所示:此应用程序将所有信息以json格式发送到数据库,并且从该数据库中我想显示不同性能和利用率参数的趋势。有人可以帮助我实现获取连接设备列表的目标。
package com.uhperk.mobilesensor;
import android.content.Context;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.SystemClock;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.URL;
import java.util.Formatter;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
public double lon, lat, alt, acc;
public Boolean isBetterLocation;
public Long delay;
@Override
protected void onCreate(Bundle savedInstanceState) {
if(Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// GPS
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.v("HELLO", "New location was found");
lat = location.getLatitude();
lon = location.getLongitude();
alt = location.getAltitude();
acc = location.getAccuracy();
Log.v("Latitude", Double.toString(lat));
Log.v("Longitude", Double.toString(lon));
Log.v("Altitude", Double.toString(alt));
Log.v("Accuracy", Double.toString(acc));
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Button buttonTest = (Button) findViewById(R.id.buttonTest);
buttonTest.setOnClickListener(new View.OnClickListener() {
//Implementing Accuracy on location information
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > (1000 * 60 * 2);
boolean isSignificantlyOlder = timeDelta < -(1000 * 60 * 2);
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 2;
boolean isMoreAccurate = accuracyDelta < 2;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public void onClick(View v) {
// WIFI
WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
int level = wifiInfo.getRssi();
String mac = wifiInfo.getMacAddress();
String essid = wifiInfo.getSSID();
String bssid = wifiInfo.getBSSID();
int ipAddress = wifiInfo.getIpAddress();
String ipstr = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
Log.v("Signal Level", Integer.toString(level));
Log.v("MAC Address", mac);
Log.v("ESSID", essid);
Log.v("BSSID", bssid);
Log.v("IP Address", ipstr);
TextView essidLab = (TextView) findViewById(R.id.essidLab);
TextView bssidLab = (TextView) findViewById(R.id.bssidLab);
TextView macLab = (TextView) findViewById(R.id.macLab);
TextView IPLab = (TextView) findViewById(R.id.IPLab);
TextView signalLab = (TextView) findViewById(R.id.signalLab);
TextView latLabel = (TextView) findViewById(R.id.latLab);
TextView lonLabel = (TextView) findViewById(R.id.lonLab);
TextView altLabel = (TextView) findViewById(R.id.AltLab);
TextView accLabel = (TextView) findViewById(R.id.AccLab);
essidLab.setText(essid);
bssidLab.setText(bssid);
macLab.setText(mac);
IPLab.setText(ipstr);
signalLab.setText(Integer.toString(level));
latLabel.setText(Double.toString(lat));
lonLabel.setText(Double.toString(lon));
altLabel.setText(Double.toString(alt));
accLabel.setText(Double.toString(acc));
//Ping Trace
//try {
//DefaultHttpClient client = new DefaultHttpClient();
//HttpParams httpParameters = client.getParams();
//int timeoutConnection = 3000;
//HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
//int timeoutSocket = 5000;
//HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
//HttpGet httpGet = new HttpGet("http://172.xx.xx.xxx/bigfile");
//httpGet.setHeader("Accept", "application/json");
//httpGet.setHeader("Content-type", "application/json");
//long start = System.currentTimeMillis();
//HttpResponse httpResponse = client.execute(httpGet);
//InputStream inputStream = httpResponse.getEntity().getContent();
//byte[] buffer = new byte[4096];
//while(inputStream.read(buffer) > 0) {
//}
//long end = System.currentTimeMillis();
//delay = end - start;
//} catch(IOException e) {
//}
//Log.v("RTT", Long.toString(delay));
//TextView delLab = (TextView) findViewById(R.id.Delaytime);
//delLab.setText(Long.toString(delay));
//send data
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("lat", lat);
jsonObject.accumulate("lon", lon);
jsonObject.accumulate("ss", level);
jsonObject.accumulate("mac", mac);
jsonObject.accumulate("essid", essid);
jsonObject.accumulate("bssid", bssid);
jsonObject.accumulate("ip", ipstr);
jsonObject.accumulate("alt", alt);
jsonObject.accumulate("acc", acc);
jsonObject.accumulate("res", delay);
// and so on ...
String json = jsonObject.toString();
StringEntity se = new StringEntity(json);
Log.d("JSON", json);
DefaultHttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpPost httpPost = new HttpPost("http://172.xx.xx.xxx:81/push");
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = client.execute(httpPost);
String result;
InputStream inputStream = httpResponse.getEntity().getContent();
} catch (JSONException e) {
Log.d("JSON", e.getLocalizedMessage());
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (Exception e){
throw new RuntimeException("Exception while calling URL:"+ "http://172.xx.xx.xxx/bigfile", e);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}