Android WIFI 详解
event, String remainder) {
switch (event) {
case DISCONNECTED:
handleNetworkStateChange(NetworkInfo.DetailedState.DISCONNECTED,remainder);
break;
case CONNECTED:
handleNetworkStateChange(NetworkInfo.DetailedState.CONNECTED,remainder);
break;
……
}
private voidhandleNetworkStateChange(NetworkInfo.DetailedState newState, String data) {
StringBSSID = null;
intnetworkId = -1;
if(newState == NetworkInfo.DetailedState.CONNECTED) {
Matcher match = mConnectedEventPattern.matcher(data);
if(!match.find()) {
if (Config.LOGD) Log.d(TAG, Could not find BSSID in CONNECTEDevent string);
}else {
BSSID = match.group(1);
try {
networkId = Integer.parseInt(match.group(2));
} catch (NumberFormatException e) {
networkId = -1;
}
}
}
mWifiStateTracker.notifyStateChange(newState,BSSID, networkId);
}
void notifyStateChange(DetailedState newState, StringBSSID, int networkId) {
Messagemsg = Message.obtain(
this, EVENT_NETWORK_STATE_CHANGED,
newNetworkStateChangeResult(newState, BSSID, networkId));
msg.sendToTarget();
}
caseEVENT_NETWORK_STATE_CHANGED:
……
configureInterface();
……
private void configureInterface() {
checkPollTimer();
mLastSignalLevel = -1;
if(!mUseStaticIp) { //使用DHCP线程动态IP
if(!mHaveIpAddress !mObtainingIpAddress) {
mObtainingIpAddress = true;
//发送启动DHCP线程获取IP
mDhcpTarget.sendEmptyMessage(EVENT_DHCP_START);
}
} else { //使用静态IP,IP信息从mDhcpInfo中获取
intevent;
if(NetworkUtils.configureInterface(mInterfaceName, mDhcpInfo)) {
mHaveIpAddress = true;
event = EVENT_INTERFACE_CONFIGURATION_SUCCEEDED;
if (LOCAL_LOGD) Log.v(TAG, Static IP configurationsucceeded);
}else {
mHaveIpAddress = false;
event = EVENT_INTERFACE_CONFIGURATION_FAILED;
if (LOCAL_LOGD) Log.v(TAG, Static IP configuration failed);
}
sendEmptyMessage(event); //发送IP获得成功消息事件
}
}
DhcpThread获取EVENT_DHCP_START消息事件后,调用handleMessage()函数,启动DHCP获取IP地址的服务。
public void handleMessage(Message msg) {
intevent;
switch (msg.what) {
case EVENT_DHCP_START:
……
Log.d(TAG, DhcpHandler: DHCP requeststarted);
//启动一个DHCPclient的精灵进程,为mInterfaceName请求分配一个IP地//址
if (NetworkUtils.runDhcp(mInterfaceName, mDhcpInfo)) {
event= EVENT_INTERFACE_CONFIGURATION_SUCCEEDED;
if(LOCAL_LOGD) Log.v(TAG, DhcpHandler: DHCP request succeeded);
} else {
event= EVENT_INTERFACE_CONFIGURATION_FAILED;
Log.i(TAG,DhcpHandler: DHCP request failed: +
NetworkUtils.getDhcpError());
}
……
}
这 里调用了一个NetworkUtils.runDhcp()函数,NetworkUtils类是一个网络服务的辅助类,它主要定义了一些本地接口,这些接 口会通过他们的JNI层android_net_NetUtils.cpp文件和DHCP client通信,并获取IP地址。
至此,IP地址获取完毕,Wifi启动流程结束。
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)