如何获得wifi热点状态[英] How to get wifi hotspot state

本文是小编为大家收集整理的关于如何获得wifi热点状态的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在手机中创建 wifi hotspot ,我想在我打开或关闭热点时得到状态更改事件.

推荐答案

请查看以下代码.这将有助于您

public class WifiApManager {
    private final WifiManager mWifiManager;

    public WifiApManager(Context context) {
        mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    /*the following method is for getting the wifi hotspot state*/

    public WIFI_AP_STATE getWifiApState() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApState");

            int tmp = ((Integer) method.invoke(mWifiManager));

            // Fix for Android 4
            if (tmp > 10) {
                tmp = tmp - 10;
            }

            return WIFI_AP_STATE.class.getEnumConstants()[tmp];
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
        }
    }

    /**
     * Return whether Wi-Fi Hotspot is enabled or disabled.
     * 
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

}

wifi_ap_state是一个如下所示的枚举

  public enum WIFI_AP_STATE {
      WIFI_AP_STATE_DISABLING, 
      WIFI_AP_STATE_DISABLED, 
      WIFI_AP_STATE_ENABLING, 
      WIFI_AP_STATE_ENABLED, 
      WIFI_AP_STATE_FAILED
  }

其他推荐答案

获取热点AP的当前状态,我使用:

final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final int apState = (Integer) wifiManager.getClass().getMethod("getWifiApState").invoke(wifiManager);

if (apState == 13) {
    // Ap Enabled
}

并在Hotspot AP启用/禁用时获取更新,在BroadcastReceiver中接收"android.net.wifi.wifi_ap_state_changed"Intent:

public class WifiAPReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == "android.net.wifi.WIFI_AP_STATE_CHANGED") {
            int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            if (apState == 13) {
                // Hotspot AP is enabled
            } else {
                // Hotspot AP is disabled/not ready
            }
        }
    }
}

也,不要忘记清单中的声明和权限:

<receiver android:name=".WifiAPReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
    </intent-filter>
</receiver>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

其他推荐答案

您可以使用Broadcasteciver监视WiFi状态.这是如何实现它的例子: http://silverballsoftware.com/android-onitor-wifi-state-with-broadcastreceiver

本文地址:https://www.itbaoku.cn/post/102318.html

问题描述

I am creating wifi hotspot in my phone and I want get its state change event when I switched on or off hotspot.

推荐答案

Please look at the following code. This will help you

public class WifiApManager {
    private final WifiManager mWifiManager;

    public WifiApManager(Context context) {
        mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    /*the following method is for getting the wifi hotspot state*/

    public WIFI_AP_STATE getWifiApState() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApState");

            int tmp = ((Integer) method.invoke(mWifiManager));

            // Fix for Android 4
            if (tmp > 10) {
                tmp = tmp - 10;
            }

            return WIFI_AP_STATE.class.getEnumConstants()[tmp];
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
        }
    }

    /**
     * Return whether Wi-Fi Hotspot is enabled or disabled.
     * 
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

}

Where WIFI_AP_STATE is an enum which is as follows

  public enum WIFI_AP_STATE {
      WIFI_AP_STATE_DISABLING, 
      WIFI_AP_STATE_DISABLED, 
      WIFI_AP_STATE_ENABLING, 
      WIFI_AP_STATE_ENABLED, 
      WIFI_AP_STATE_FAILED
  }

其他推荐答案

To get the current state of the hotspot AP, I use:

final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final int apState = (Integer) wifiManager.getClass().getMethod("getWifiApState").invoke(wifiManager);

if (apState == 13) {
    // Ap Enabled
}

And to get updates when the hotspot AP gets enabled/disabled, receive the "android.net.wifi.WIFI_AP_STATE_CHANGED" intent in a BroadcastReceiver:

public class WifiAPReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == "android.net.wifi.WIFI_AP_STATE_CHANGED") {
            int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            if (apState == 13) {
                // Hotspot AP is enabled
            } else {
                // Hotspot AP is disabled/not ready
            }
        }
    }
}

Also, don't forget your declaration and permissions in the Manifest:

<receiver android:name=".WifiAPReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
    </intent-filter>
</receiver>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

其他推荐答案

You can monitor the wifi state with a BroadcastReciver. Here's an example of how to implement it: http://silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver