I can not find the problem, there are no mistakes but it does not work for me.
public class ConfigWifiBT extends Activity implements View.OnClickListener {
WifiManager wifi;
ListView lv;
Button buttonScan;
int size = 0;
List<ScanResult> results;
ListView IdLista;
private ArrayAdapter mPairedDevicesArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispositivos_bt);
buttonScan = (Button) findViewById(R.id.scan);
buttonScan.setOnClickListener(this);
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);
}
mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.activity_dispositivos_bt);
IdLista = (ListView) findViewById(R.id.wifilist);
IdLista.setAdapter(mPairedDevicesArrayAdapter);
IdLista.setOnItemClickListener(mDeviceClickListener);
scanWifiNetworks();
}
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView av, View v, int arg2, long arg3) {
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
Log.d("Wifi info:",info+" - "+address);
}
};
public void onClick(View view)
{
scanWifiNetworks();
}
private void scanWifiNetworks(){
registerReceiver(wifi_receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
Log.d("WifScanner1", "scanWifiNetworks");
Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver wifi_receiver= new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
Log.d("WifScanner2", "onReceive");
results = wifi.getScanResults();
size = results.size();
unregisterReceiver(this);
try
{
while (size >= 0)
{
size--;
mPairedDevicesArrayAdapter.add(results.get(size).SSID);
}
}
catch (Exception e)
{
Log.w("WifScanner3", "Exception: "+e);
}
}
};
}
Layout, ignore the name of the same, activity_bug_devices:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/wifilist"
android:layout_width="match_parent"
android:layout_height="312dp"
android:layout_weight="0.97" />
<Button
android:id="@+id/scan"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_margin="15dp"
android:background="@android:color/holo_green_light"
android:text="Scan Again" />
</LinearLayout>
The log that appears in Logcat is: W / WifScanner3: Exception: java.lang.ArrayIndexOutOfBoundsException: length = 0; index = -1