Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this code</p> <pre><code>public class WiFiDemo extends Activity implements OnClickListener { WifiManager wifi; ListView lv; TextView textStatus; Button buttonScan; int size = 0; List&lt;ScanResult&gt; results; String ITEM_KEY = "key"; ArrayList&lt;HashMap&lt;String, String&gt;&gt; arraylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); SimpleAdapter adapter; /* Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textStatus = (TextView) findViewById(R.id.textStatus); buttonScan = (Button) findViewById(R.id.buttonScan); buttonScan.setOnClickListener(this); lv = (ListView)findViewById(R.id.list); wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifi.isWifiEnabled() == false) { Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show(); wifi.setWifiEnabled(true); } this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }); lv.setAdapter(this.adapter); registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { results = wifi.getScanResults(); size = results.size(); } }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } public void onClick(View view) { arraylist.clear(); wifi.startScan(); Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show(); try { size = size - 1; while (size &gt;= 0) { HashMap&lt;String, String&gt; item = new HashMap&lt;String, String&gt;(); item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities); arraylist.add(item); size--; adapter.notifyDataSetChanged(); } } catch (Exception e) { } } } </code></pre> <p><strong>WiFiDemo.xml</strong> :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:orientation="vertical"&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"&gt; &lt;TextView android:id="@+id/textStatus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Status" /&gt; &lt;Button android:id="@+id/buttonScan" android:layout_width="wrap_content" android:layout_height="40dp" android:text="Scan" /&gt; &lt;/LinearLayout&gt; &lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20dp"&gt;&lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>For ListView- <strong>row.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"&gt; &lt;TextView android:id="@+id/list_value" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14dp" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Add these permission in <strong>AndroidManifest.xml</strong></p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt; &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload