I want to display result of WiFi Scan in tabbed activity with 2 tabs 'OM' & 'OR'. In 'OM' tab, I want to list devices whose SSID starts with OM & in 'OR' tab devices starting with SSID OR.
I tried following code which works fine in activity public class MainActivity extends AppCompatActivity {}
. However it doesn't give any result in fragment (returns null).
Please look at the following code & let me know what mistake I am doing.
Thanks in advance.
public static class OmFragment extends Fragment {
ListView listView;
private static WifiManager wifiManager;
private static WifiReceiver wifiReceiver;
private static List<ScanResult> wifiList;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_om_layout, container, false);
listView = (ListView) view.findViewById(R.id.wifiDeviceList);
wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiReceiver();
if (!wifiManager.isWifiEnabled()) {
if (wifiManager.setWifiEnabled(true)) {
Toast.makeText(getActivity(), "WIFI enabled.", Toast.LENGTH_SHORT).show();
getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
} else {
Toast.makeText(getActivity(), "Error in WiFi Initialization!!", Toast.LENGTH_SHORT).show();
}
} else if (wifiManager.isWifiEnabled()) {
getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
}
this.adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(this.adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(getActivity(), "Selected: " + listView.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
});
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
try {
getActivity().unregisterReceiver(wifiReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
Toast.makeText(getActivity(), "SCAN_RESULTS_AVAILABLE", Toast.LENGTH_SHORT).show();
wifiList = wifiManager.getScanResults();
Toast.makeText(getActivity(), "WiFi List: " + wifiList, Toast.LENGTH_SHORT).show();
if (wifiList.size() > 0) {
int TotalWifiDevices = wifiList.size();
Toast.makeText(getActivity(), "Number of Wifi connections : " + TotalWifiDevices, Toast.LENGTH_SHORT).show();
try {
while (TotalWifiDevices >= 0) {
TotalWifiDevices--;
arrayList.add(wifiList.get(TotalWifiDevices).SSID);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.w("WiFi Exception: ", e);
}
} else {
Toast.makeText(getActivity(), "No Wifi found.", Toast.LENGTH_SHORT).show();
}
}
}
}
}