@Override public void handleMessage(Message msg) { switch (msg.what) { case WifiApConst.ApScanResult: // 扫描Wifi列表 if (isRespond) { getWifiList(); refreshAdapter(mWifiList); } break; case WifiApConst.WiFiConnectSuccess: // 连接热点成功 String str = getString(R.string.wifiap_text_wifi_connected) + WifiUtils.getSSID(); mTvStatusInfo.setText(str); Toast.makeText(getActivity(), str, 0).show(); break; case WifiApConst.WiFiConnectError: // 连接热点错误 Toast.makeText(getActivity(), R.string.wifiap_toast_connectap_error, 0).show(); break; case WifiApConst.NetworkChanged: // Wifi状态变化 if (WifiUtils.isWifiEnabled()) { mTvStatusInfo.setText(getString(R.string.wifiap_text_wifi_1_0)); } else { mTvStatusInfo.setText(getString(R.string.wifiap_text_wifi_0)); Toast.makeText(getActivity(), R.string.wifiap_text_wifi_disconnect, 0).show(); } default: break; } }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ScanResult ap = mWifiList.get(position); if (ap.SSID.startsWith(WifiApConst.WIFI_AP_HEADER)) { mTvStatusInfo.setText(getString(R.string.wifiap_btn_connecting) + ap.SSID); // 连接网络 boolean connFlag = WifiUtils.connectWifi( ap.SSID, WifiApConst.WIFI_AP_PASSWORD, WifiCipherType.WIFICIPHER_WPA); if (!connFlag) { mTvStatusInfo.setText(getString(R.string.wifiap_toast_connectap_error_1)); } } else if (!WifiUtils.isWifiConnect() || !ap.BSSID.equals(WifiUtils.getBSSID())) { showDialogBluetooth(position, view); } }
public void run() { while (!WifiUtils.isWifiApEnabled()) { if (!this.running) return; try { Thread.sleep(2000); // 扫描间隔 } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(WifiApConst.ApScanResult); } }
/** 初始化控件设置 * */ protected void initAction() { if (!WifiUtils.isWifiConnect() && !WifiUtils.isWifiApEnabled()) { // 无开启热点无连接WIFI WifiUtils.OpenWifi(); } if (WifiUtils.isWifiConnect()) { // Wifi已连接 mTvStatusInfo.setText(getString(R.string.wifiap_text_wifi_connected) + WifiUtils.getSSID()); } if (WifiUtils.isWifiEnabled() && !WifiUtils.isWifiConnect()) { // Wifi已开启,未连接 mTvStatusInfo.setText(getString(R.string.wifiap_text_wifi_1_0)); } mSearchWifiThread.start(); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { wifiManager = (WifiManager) getActivity().getSystemService(getActivity().WIFI_SERVICE); initBroadcast(); // 注册广播 View view = inflater.inflate(R.layout.fragment_wifi_main, container, false); initView(view); initEventes(); if (isWiFiActive(getActivity())) { WifiUtils.OpenWifi(); getWifiList(); refreshAdapter(mWifiList); } initAction(); // utilss = new WifiUtils(getActivity()); return view; }
private void getWifiList() { mWifiList.clear(); WifiUtils.startScan(); scanResults = WifiUtils.getScanResults(); mWifiList.addAll(scanResults); }