如何在选项卡式 activity 的片段中使用 wifimanager.getscanresults()?

How to use wifimanager.getscanresults() in fragment in tabbed activity?

我想在 activity 选项卡中显示 WiFi 扫描的结果,其中包含 2 个选项卡 'OM' 和 'OR'。在 'OM' 选项卡中,我想列出 SSID 以 OM 开头的设备 & 在 'OR' 选项卡中以 SSID OR 开头的设备。 我尝试了以下在 activity public class MainActivity extends AppCompatActivity {} 中运行良好的代码。但是它不会在片段中给出任何结果(returns null)。 请查看以下代码并让我知道我在做什么错误。 提前致谢。

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();
                }
            }
        }
    }
}

我在 Naugat 上试用应用程序,我们需要提供以下权限

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

& 必须打开位置并授予位置权限。代码现在可以运行了。