i want to send the data to server from an android app. some times i cant able to sync the whole data to server i think because of wifi db level if so what is the minimum db level can i use to start the sync. can anyone please explain me in that.
To get the db of the wifi i am using the following code
WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResult = wifiManager.getScanResults();
for (int i = 0; i < scanResult.size(); i++) {
Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal
}
return info.isConnected();
Related
I have implemented an app which will allow user to connect to a desired network.
If the SSID record is already remembered by phone. It will notify user to manually delete the SSID record.
Here is some sample code
temp = -1;
for (int i = 0; i < WiFi.ConfiguredNetworks.Count; i++)
{
if (String.Compare(WiFi.ConfiguredNetworks[i].Ssid, ("\"" + SSID + "\"")) == 0)
{
temp = WiFi.ConfiguredNetworks[i].NetworkId;
break;
}
}
if(temp != -1) //This SSID is already remembered by phone
{
bool success = WiFi.RemoveNetwork(temp)
if(success == false)
{
//Call some function to notify user to manually remove the network
}
}
Things happened at //Call some function to notify user to manually remove the network.
Sometimes the user goes to the wifi system page. They found out that there is no record of this SSID.
In my opinion, if RemoveNetwork() return failed, that means this SSID is already remembered by phone.
And it is not build by the current app.
Is that wrong?
Issue happened on device Nokia 8(Android 9).
As the docs for WifiManager say:
Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false.
If your API level is below 29 then you probably aren't passing a valid NetworkId.
I have a method to detect available wifi access ppoints. My method works well but when I am not in the range I still getiing the SSID of the last scan results displayed in my xml file though I am out of the range of this SSID.
private void check_wifi_available() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(this, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
}
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "SV-"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
// Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
// Matcher m = p.matcher(ssid);
// if(m.matches()){}else{}
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
}
}
if (updatedResults.size() > 0) {
String a = deliverBestAccessPoint(updatedResults);
textWifi.setText(a.toString());
}
}
}
This is best handled by the operating system. The best you could do is set up a timer to periodically scan for WiFi devices and update the results.
Other than that, on rooted devices you may be able to manually send 802.11 requests to the access point/router and do a timeout check for replies.
To clarify: the operating system, when it is scanning for devices, sends out a broadcast message and reports what devices it hears back from. When devices are toward the edge of the 'range' they may report as being available even if connecting and maintaining a connection is problematic because the signal is not strong enough.
EDIT:
For what it's worth, ScanResult has a "level" member variable that reports the signal strength. You could do some more fine filtering for low-strength results. http://developer.android.com/reference/android/net/wifi/ScanResult.html
jMdns is a great java library to provide zeroconf/bonjour capabilities to your Android application.
I successfully using this in a project up until Android 4.0 Ice Cream Sandwich aka ICS, once Android 4.0 devices were starting to be used more often I am facing application not working.
I have tested application in android 4.0 earlier, it show me list of Discover Devices but in android 4.0 or later it show nothing.
I've tested this demo "https://github.com/twitwi/AndroidDnssdDemo" on 4.1 but its not working.
I have written Below code based on suggestion mention in "http://snctln.com/2012/08/03/jmdns-and-android-4-0/"
private android.net.wifi.WifiManager.MulticastLock lock;
private android.os.Handler handler = new android.os.Handler();
private JmDNS jmdns = null;
public WifiManager wifi;
private void setUp()
{
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
try {
jmdns.create(_bindingAddress);
ServiceInfo[] infos = jmdns.list("_afpovertcp._tcp.local.");
for (int i=0; i < infos.length; i++) {
Log.i("Servic : ",infos[i].getName()+"");
// notifyUser("\nServic : "+infos[i].getName()+"");
}
} catch (IOException e) {
e.printStackTrace();
}
}
your suggestion are appreciable
My simple guess would be that it's trying to download data on the main thread. This is not possible from Ice Cream Sandwich and forward. Try to look at logcat while it runs on the phone, it should clearly show a warning/error message about it if it's the case.
You can let it run via the main thread ala
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
I am working with an App which contains web service things.
In that I need to know the status when the Internet speed is low. How to find the internet speed level in Android?
For example, Consider if I am using 2Mbps connection in my cell phone and when it slows to 50Kbps I need to notice that situation by making a Toast or Alert.
Thanks.
If you are connected to WiFi you can find the speed of the connection using WifiManager :
WifiInfo wifiInfo = wifiManger.getConnectionInfo();
and then from the WifiInfo you can get the current speed :
int speedMbps = wifiInfo.getLinkSpeed();
If you are on 3G, I don't think there is a standard way of finding out, maybe you can assume automatically that 3G is slow.
This is specilally to detect internet connection speed by
facebook sdk
ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
This is the code for getting speed of your internet while connected to wifi.
WifiManager wifiManager = (WifiManager)
this.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> wifiList = wifiManager.getScanResults();
for (ScanResult scanResult : wifiList) {
int level = WifiManager.calculateSignalLevel(scanResult.level, 5);
String net=String.valueOf(level);
// Toast.makeText(MainActivity.this,net,Toast.LENGTH_LONG).show();
}
// Level of current connection.here rssi is the value of internet speed whose value
// can be -50,-60 and some others,you can find the speed values easily on internet.
int rssi = wifiManager.getConnectionInfo().getRssi();
int level = WifiManager.calculateSignalLevel(rssi, 5);
String net=String.valueOf(rssi);
Toast.makeText(MainActivity.this,net,Toast.LENGTH_LONG).show();
// -100 is the minimum speed value of your internet.
if(rssi < -100) {
slowInternet=false;
}
What I want to do is to count 3G traffic and WiFi traffic respectively. Now I know how to do with WiFi. Below is the source code for WiFi. By this way I can count WiFi traffic for all the android phones of all manufactures. But I haven't found a similar way for 3g. Does anyone know?
//to get wifi interface
private static String getProp(String prop){
String output = "";
try{
Class<?> sp = Class.forName("android.os.SystemProperites");
Method get = sp.getMethod("get",String.class);
output = (String)get.invoke(null,prop);
}catch(Exception e){
e.printStackTrace();
}
return output;
}
//to get the traffic from system file
...
...
if (connectinTpe == ConnectivityManager.TYPE_WIFI){
String wifiInterface = getProp("wifi.interface");
if(wifiInterface == null || "".equals(wifiInterface)) wifiInterface = "eth0";
rxFile = "/sys/class/net/" +wifiInterface+ "/statistics/rx_bytes";
txFile = "/sys/class/net/" +wifiInterface+ "/statistics/tx_bytes";
}
...
...
Starting from API level 8 (Android 2.2) there is a class TrafficStats which provides what you need:
Class that provides network traffic statistics. These statistics
include bytes transmitted and received and network packets transmitted
and received, over all interfaces, over the mobile interface, and on a
per-UID basis.
On the older versions you can use the approach you mentioned (i.e. reading file content of /sys/class/net/... files). This blog post contains an excellent mapping between TrafficStats methods and file locations. And this SO post contains the source its author used to read those files values. According to it you should first try to read number from "/sys/class/net/rmnet0/statistics/rx_bytes" file (for "received bytes" value) and if it fails try "/sys/class/net/ppp0/statistics/rx_bytes" instead.
to get the current type of connection you can use the TelephonyManager: http://developer.android.com/reference/android/telephony/TelephonyManager.html
first check if the device is connected to the default mobile data connection and then check the connection type:
if (connectinTpe == ConnectivityManager.TYPE_MOBILE)
{
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int curConnectionType = tm.getNetworkType();
if(curConnectionType >= /*connection type you are looking for*/)
{
// do what you want
}
}