ahmed-BH / android_wifi_signal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
/* —————————————————————————————————————————— |
* SIGNAL MEANING |
* -30 dBm Maximum signal strength, you are probably standing right next to the access point. |
* -50 dBm Anything down to this level can be considered excellent signal strength. |
* -60 dBm Good, reliable signal strength. |
* -67 dBm Reliable signal strength. The minimum for any service depending on a reliable connection and signal strength, such as voice over Wi-Fi and non-HD video streaming. |
* -70 dBm Not a strong signal. Light browsing and email. |
* -80 dBm Unreliable signal strength, will not suffice for most services. Connecting to the network. |
* -90 dBm The chances of even connecting are very low at this level. |
*/ —————————————————————————————————————————— |
/* |
* Needed permission |
* |
* |
* |
*/ |
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver () |
@ Override |
public void onReceive ( Context c , Intent intent ) |
if ( intent . getAction (). equals ( WifiManager . SCAN_RESULTS_AVAILABLE_ACTION )) |
List < ScanResult >mScanResults = mWifiManager . getScanResults (); |
// add your logic here |
> |
> |
> |
@ Override |
protected void onCreate ( Bundle savedInstanceState ) |
mWifiManager = ( WifiManager ) getSystemService ( Context . WIFI_SERVICE ); |
registerReceiver ( mWifiScanReceiver , |
new IntentFilter ( WifiManager . SCAN_RESULTS_AVAILABLE_ACTION )); |
mWifiManager . startScan (); |
// Turn Wifi ON/OFF |
// |
wifiManager . setWifiEnabled ( true ); |
wifiManager . setWifiEnabled ( false ); |
> |
// Asking permission at Runtime |
@ Override |
public void onResume () |
super . onResume (); |
if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . M ) |
if ( checkSelfPermission ( Manifest . permission . ACCESS_COARSE_LOCATION ) != PackageManager . PERMISSION_GRANTED ) |
requestPermissions ( new String []< Manifest . permission . ACCESS_COARSE_LOCATION >, 87 ); |
> |
> |
> |
How to get available wifi networks and display them in a list in android
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Step 3 − Add the following code to src/MainActivity.java
package com.example.myapplication; import android.Manifest; import android.content.Context; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.net.wifi.WifiManager; import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity < private ListView wifiList; private WifiManager wifiManager; private final int MY_PERMISSIONS_ACCESS_COARSE_LOCATION = 1; WifiReceiver receiverWifi; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wifiList = findViewById(R.id.wifiList); Button buttonScan = findViewById(R.id.scanBtn); wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (!wifiManager.isWifiEnabled()) < Toast.makeText(getApplicationContext(), "Turning WiFi ON. ", Toast.LENGTH_LONG).show(); wifiManager.setWifiEnabled(true); >buttonScan.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) ! = PackageManager.PERMISSION_GRANTED) < ActivityCompat.requestPermissions( MainActivity.this, new String[], MY_PERMISSIONS_ACCESS_COARSE_LOCATION); > else < wifiManager.startScan(); >> >); > @Override protected void onPostResume() < super.onPostResume(); receiverWifi = new WifiReceiver(wifiManager, wifiList); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); registerReceiver(receiverWifi, intentFilter); getWifi(); >private void getWifi() < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < Toast.makeText(MainActivity.this, "version>= marshmallow", Toast.LENGTH_SHORT).show(); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) ! = PackageManager.PERMISSION_GRANTED) < Toast.makeText(MainActivity.this, "location turned off", Toast.LENGTH_SHORT).show(); ActivityCompat.requestPermissions(MainActivity.this, new String[], MY_PERMISSIONS_ACCESS_COARSE_LOCATION); > else < Toast.makeText(MainActivity.this, "location turned on", Toast.LENGTH_SHORT).show(); wifiManager.startScan(); >> else < Toast.makeText(MainActivity.this, "scanning", Toast.LENGTH_SHORT).show(); wifiManager.startScan(); >> @Override protected void onPause() < super.onPause(); unregisterReceiver(receiverWifi); >@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) < super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) < case MY_PERMISSIONS_ACCESS_COARSE_LOCATION: if (grantResults.length >0 && grantResults[0] = = PackageManager.PERMISSION_GRANTED) < Toast.makeText(MainActivity.this, "permission granted", Toast.LENGTH_SHORT).show(); wifiManager.startScan(); >else < Toast.makeText(MainActivity.this, "permission not granted", Toast.LENGTH_SHORT).show(); return; >break; > > >
Step 4 − Add the following code to src/WifiReceiver
package com.example.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; class WifiReceiver extends BroadcastReceiver < WifiManager wifiManager; StringBuilder sb; ListView wifiDeviceList; public WifiReceiver(WifiManager wifiManager, ListView wifiDeviceList) < this.wifiManager = wifiManager; this.wifiDeviceList = wifiDeviceList; >public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) < sb = new StringBuilder(); ListwifiList = wifiManager.getScanResults(); ArrayList deviceList = new ArrayList<>(); for (ScanResult scanResult : wifiList) < sb.append("
").append(scanResult.SSID).append(" - ").append(scanResult.capabilities); deviceList.add(scanResult.SSID + " - " + scanResult.capabilities); > Toast.makeText(context, sb, Toast.LENGTH_SHORT).show(); ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, deviceList.toArray()); wifiDeviceList.setAdapter(arrayAdapter); > > >
Step 5 − Add the following code to androidManifest.xml
Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code
Android: Scanning Wifi Network + Selectable list
I’m trying to creat an application that let you scan the available wifi networks, and then display them in a selectable list. To do that I tried with this code:
package android.nacho.WifiScan; import java.util.List; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class WifiScan extends Activity < TextView mainText; WifiManager mainWifi; WifiReceiver receiverWifi; ListwifiList; StringBuilder sb = new StringBuilder(); ListView listView = new ListView(this); ArrayAdapter adapter; Context context = this; static String[] NETWORKS; public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_scan); mainText = (TextView) findViewById(R.id.text); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); receiverWifi = new WifiReceiver(); registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); mainWifi.startScan(); mainText.setText("\\nStarting Scan. \\n"); >public boolean onCreateOptionsMenu(Menu menu) < menu.add(0, 0, 0, "Refresh"); return super.onCreateOptionsMenu(menu); >public boolean onMenuItemSelected(int featureId, MenuItem item) < mainWifi.startScan(); mainText.setText("Starting Scan"); return super.onMenuItemSelected(featureId, item); >protected void onPause() < unregisterReceiver(receiverWifi); super.onPause(); >protected void onResume() < registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); super.onResume(); >class WifiReceiver extends BroadcastReceiver < public void onReceive(Context c, Intent intent) < wifiList = mainWifi.getScanResults(); NETWORKS = new String[wifiList.size()]; for(int i = 0; i < wifiList.size(); i++)< NETWORKS[i]=i+"-"+(wifiList.get(i)).toString(); >System.out.println("debería imprimir: "+NETWORKS[2]); adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_multiple_choice, NETWORKS); // adapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice); listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setOnItemClickListener(new OnItemClickListener() < public void onItemClick(AdapterViewarg0, View arg1, int arg2,long arg3) < >>); setContentView(listView); > > >
06-18 12:36:32.670: W/dalvikvm(15499): threadid=1: thread exiting with uncaught exception (group=0x40dfb930) 06-18 12:36:32.670: E/AndroidRuntime(15499): FATAL EXCEPTION: main 06-18 12:36:32.670: E/AndroidRuntime(15499): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.NullPointerException 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-18 12:36:32.670: D/AndroidRuntime(15499): Shutting down VM 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.os.Looper.loop(Looper.java:137) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.ActivityThread.main(ActivityThread.java:5039) 06-18 12:36:32.670: E/AndroidRuntime(15499): at java.lang.reflect.Method.invokeNative(Native Method) 06-18 12:36:32.670: E/AndroidRuntime(15499): at java.lang.reflect.Method.invoke(Method.java:511) 06-18 12:36:32.670: E/AndroidRuntime(15499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-18 12:36:32.670: E/AndroidRuntime(15499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-18 12:36:32.670: E/AndroidRuntime(15499): at dalvik.system.NativeStart.main(Native Method) 06-18 12:36:32.670: E/AndroidRuntime(15499): Caused by: java.lang.NullPointerException 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.view.View.(View.java:3224) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.view.View.(View.java:3279) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.view.ViewGroup.(ViewGroup.java:431) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.widget.AdapterView.(AdapterView.java:235) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.widget.AbsListView.(AbsListView.java:766) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.widget.ListView.(ListView.java:143) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.widget.ListView.(ListView.java:139) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.widget.ListView.(ListView.java:135) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.nacho.WifiScan.WifiScan.(WifiScan.java:27) 06-18 12:36:32.670: E/AndroidRuntime(15499): at java.lang.Class.newInstanceImpl(Native Method) 06-18 12:36:32.670: E/AndroidRuntime(15499): at java.lang.Class.newInstance(Class.java:1319) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 06-18 12:36:32.670: E/AndroidRuntime(15499): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 06-18 12:36:32.670: E/AndroidRuntime(15499): . 11 more