我正在尝试评估手机连接到我的wifi网络的能力。我想知道对我的wifi接入点进行身份验证需要多长时间,获取IP地址需要多长时间等等。
由于这个网站和大量的测试,我取得了很大的进步。然而,我永远不能缝合能够让我的广播接收器触发某些连接状态。我真的想以某种方式检测身份验证发生的时间。
现在我知道Android文档的内容,并且身份验证状态应该是一个容易访问的内容。但是在使用多个设备的实践中,永远不会达到该状态。我甚至尝试使用以下代码进行轮询。
public void start(long delayMillsec) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
long timeout = System.currentTimeMillis() + 5000;
running = true;
while(true)
{
int oldState = state;
showWifiStatus();
int newState = state;
if(newState != oldState)
{
Log.e("STATE", stateString + ": " + System.nanoTime());
}
}
} }
, delayInMillsec);
}
public void showWifiStatus() {
boolean connected = false;
boolean associated = false;
ConnectivityManager connManager = (ConnectivityManager) thisContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
DetailedState val = mWifi.getDetailedState();
if (val == NetworkInfo.DetailedState.SCANNING) {
//Log.e("Detailed Wifi Info"," wifi state is SCANNING ");
state = 0;
stateString = "SCANNING";
}
if (val == NetworkInfo.DetailedState.CONNECTING) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTING ");
associated = true;
state = 1;
stateString = "CONNECTING";
}
if (val == NetworkInfo.DetailedState.AUTHENTICATING) {
//Log.e("Detailed Wifi Info"," 3wifi state is AUTHENTICATING");
associated = true;
state = 2;
stateString = "AUTHENTICATING";
}
if (val == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
//Log.e("Detailed Wifi Info"," wifi state is OBTAINING_IPADDR");
associated = true;
state = 3;
stateString = "OBTAINING_IPADDR";
}
if (val == NetworkInfo.DetailedState.CONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTED ");
connected = true;
associated = true;
state = 4;
stateString = "CONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is DISCONNECTED ");
state = 5;
stateString = "DISCONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTING) {
// Log.e("Detailed Wifi Info"," wifi state is DISCONNECTING ");
state = 6;
stateString = "DISCONNECTING";
}
if (val == NetworkInfo.DetailedState.FAILED) {
//Log.e("Detailed Wifi Info"," wifi state is FAILED");
state = 7;
stateString = "FAILED";
}
if (val == NetworkInfo.DetailedState.IDLE) {
//Log.e("Detailed Wifi Info"," wifi state is IDLE");
state = 8;
stateString = "IDLE";
}
if (val == NetworkInfo.DetailedState.SUSPENDED) {
//Log.e("Detailed Wifi Info"," wifi state is SUSPENDED");
state = 9;
stateString = "SUSPENDED";
}
}
我立即从后台服务中调用启动功能,然后我在已知的无线AP之间走来走去,断开了wifi并重新连接等等......
我也尝试过连接接收器,以便尽我所能。
我使用了这个意图过滤器:
IntentFilter ConnectedFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
ConnectedFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
ConnectedFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
ConnectedFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
getApplicationContext().registerReceiver(ConnectedToAPReceiver,
ConnectedFilter);
使用此接收器,输出绝对正在接收的所有内容。并且从来没有任何迹象表明身份验证状态曾被触发过。
private BroadcastReceiver ConnectedToAPReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras != null) {
Log.e("NEW Action", intent.getAction());
for (String key: extras.keySet()) {
Log.e("CONN_ACTION", "key [" + key + "]: " +
extras.get(key));
}
Log.e("NEW THING", "------------");
}
else {
Log.e("CONNACTION", "no extras");
}
}};
我也使用以下权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
是否有任何Android专家可以帮助我解决这个问题?理想情况下,我想获得一些我可以使用的工作代码,但即使是一些关于为什么这种状态永远不会达到的良好信息也会非常有用。这是一个错误吗?
谢谢!
答案 0 :(得分:1)
它对我有用:
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiverWifi, mIntentFilter);
class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
SupplicantState supl_state=((SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
switch(supl_state){
case ASSOCIATED:Log.i("SupplicantState", "ASSOCIATED");
break;
case ASSOCIATING:Log.i("SupplicantState", "ASSOCIATING");
break;
case AUTHENTICATING:Log.i("SupplicantState", "Authenticating...");
break;
case COMPLETED:Log.i("SupplicantState", "Connected");
break;
case DISCONNECTED:Log.i("SupplicantState", "Disconnected");
break;
case DORMANT:Log.i("SupplicantState", "DORMANT");
break;
case FOUR_WAY_HANDSHAKE:Log.i("SupplicantState", "FOUR_WAY_HANDSHAKE");
break;
case GROUP_HANDSHAKE:Log.i("SupplicantState", "GROUP_HANDSHAKE");
break;
case INACTIVE:Log.i("SupplicantState", "INACTIVE");
break;
case INTERFACE_DISABLED:Log.i("SupplicantState", "INTERFACE_DISABLED");
break;
case INVALID:Log.i("SupplicantState", "INVALID");
break;
case SCANNING:Log.i("SupplicantState", "SCANNING");
break;
case UNINITIALIZED:Log.i("SupplicantState", "UNINITIALIZED");
break;
default:Log.i("SupplicantState", "Unknown");
break;
}
int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if(supl_error==WifiManager.ERROR_AUTHENTICATING){
Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}
}
<receiver
android:name=".MyActivity$WifiReceiver"
android:process=":remote" >
</receiver>