gpt4 book ai didi

java - android中更改wifi时如何获取连接的wifi ssid

转载 作者:行者123 更新时间:2023-12-01 19:55:39 25 4
gpt4 key购买 nike

我需要在android中获取连接的wifi的SSID。当wifi网络改变时我需要获取SSID。

我搜索了很多关于这个的东西,但我没有得到正确的答案。

我了解到,此任务需要一个广播接收器。但我不知道如何使用广播接收器来实现这一点。任何人都可以帮助我。

我需要采取哪些后续步骤?

最佳答案

以下方法是获取已连接Wifi的SSID。

    public String getConnectedSSID() {
WifiInfo wifiInfo = getWifiInfo();
if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
Log.i(TAG, "getConnectedSSID" + wifiInfo.getSSID());
return wifiInfo.getSSID().replace("\"", "");
} else {
if (mWifiConnectionListener != null) {
mWifiConnectionListener.onError(!mWifiManager.isWifiEnabled() ? WifiConnectionListener.WIFI_ERROR.WIFI_DISABLED : WifiConnectionListener.WIFI_ERROR.WIFI_NOT_CONNECTED);
}
return null;
}
}

您必须在下面注册广播接收器。 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); mContext.registerReceiver(mWifiConnectivityState,intentFilter);

private final BroadcastReceiver mWifiConnectivityState = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();

switch (action) {

case ConnectivityManager.CONNECTIVITY_ACTION:
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
switch (networkInfo.getState()) {
case CONNECTED:
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
NetworkInfo networkInfo = ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
boolean isConnected = networkInfo != null && networkInfo.isConnected();
if (isConnected)
mConnectedSSID = info.getSSID();
//register callback and pass SSID as parameter.
break;
case CONNECTING:
break;
case DISCONNECTED:
break;
case DISCONNECTING:
break;
}
}
break;
}
}
};

关于java - android中更改wifi时如何获取连接的wifi ssid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59044313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com