Android softap連線斷開訊息通知機制(Android O)
版權宣告:本文為博主原創文章,部落格地址:https://blog.csdn.net/h784707460/article/details/79788344,未經博主允許不得轉載。
基於使用者需求、功耗優化等,Softap功能常常會在原生Android基礎上做一些定製,比如:STA的接入/斷開通知;獲取當前接入的STA個數和列表;softap的休眠機制等。這些定製都離不開一個基礎,就是Android softap中STA接入斷開的訊息傳遞機制。掌握了訊息傳遞機制,這些定製只是在該機制中加入一些邊邊角角,so easy~
我們從softap的管理核心SoftApManager開始看分析,然後分別分析向上層的訊息傳遞,下層上來的通知等。
1. Softap管理核心SoftApManager.java
private static class InterfaceEventHandler extends IInterfaceEventCallback.Stub
public void OnSoftApClientEvent(byte[] mac_address, boolean connect_status)
// 監聽到底層有sta連線狀態變化,送Softap狀態機處理
mSoftApStateMachine.sendMessage(SoftApStateMachine.CMD_SOFTAP_CLIENT_CONNECT_STATUS_CHANGED, connect_status ? 1 : 0, 0, msg.obj);
Softap狀態機只有兩種狀態:IdleState,StartedState
// startedState中對改訊息進行處理,訊息傳送到上層WifiSoftApNotificationMAnager處理。
private class StartedState extends State
case CMD_SOFTAP_CLIENT_CONNECT_STATUS_CHANGED:
mWifiSoftApNotificationManager.connectionStatusChange(message);
2. 上層訊息處理WifiSoftApNotificationManager.java
public void connectionStatusChange(Message message)
interfaceMessageRecevied((String) message.obj,isConnected);
//這裡可以實現一些對使用者的通知,介面更新等,更新接入列表和個數等softap擴充套件特性
private void interfaceMessageRecevied(String message , boolean isConnected)
// if softap extension feature not enabled, do nothing
3. SoftApManager是如何知道sta接入狀態變化的呢?--wificond
3.1 Android aidl通訊方式
system/connectivity/wificond/aidl/android/net/wifi/IInterfaceEventCallback.aidl
//aidl interface call to send sta mac and status to framework
oneway void OnSoftApClientEvent(in byte[] client_mac_addr, boolean connect_status);
3.2 wificond server廣播接入斷開訊息
system/connectivity/wificond/server.cpp
void Server::BroadcastSoftApClientConnectStatus(const vector<uint8_t>& mac_address, bool connect_status)
for (auto& it : interface_event_callbacks_)
it->OnSoftApClientEvent(mac_address, connect_status);
3.3 收到NEW_STATION/DEL_STATION訊息後,處理接入個數統計及向上傳送廣播訊息
system/connectivity/wificond/ap_interface_impl.cpp
void ApInterfaceImpl::OnStationEvent(StationEvent event, const vector<uint8_t>& mac_address)
if (event == NEW_STATION) {
number_of_associated_stations_++;
client_conn_status = true;
} else if (event == DEL_STATION) {
number_of_associated_stations_--;
}
server_->BroadcastSoftApClientConnectStatus(mac_address, client_conn_status);
3.4 wificond根據收到底層的NL80211_CMD_NEW_STATION/ NL80211_CMD_DEL_STATION訊息,向上發NEW_STATION/DEL_STATION訊息。
wificond/net/netlink_manager.h
typedef std::function<void(
StationEvent event,
const std::vector<uint8_t>& mac_address)> OnStationEventHandler;
std::map<uint32_t, OnStationEventHandler> on_station_event_handler_;
/wificond/net/netlink_manager.cpp
void NetlinkManager::BroadcastHandler(unique_ptr<const NL80211Packet> packet)
const auto handler = on_station_event_handler_.find(if_index);
if (command == NL80211_CMD_NEW_STATION) {
handler->second(NEW_STATION, mac_address);
} else {
if (command == NL80211_CMD_DEL_STATION) {
handler->second(DEL_STATION, mac_address);}
}
在向下就是WifiHAL、kernel 802.11協議了。
定製修改一般在settings、以及framework。比如1>在settings介面實現一些對使用者的通知,介面更新等,比如通過toast、彈框等通知使用者有裝置接入或斷開;2>在settings介面實現展示接入個數和列表,接入個數可以通過該介面獲取:IApInterface.getNumberOfAssociatedStations();3>Softap的休眠機制,可以通過Android定時任務Timer、AlarmManager等呼叫WiFiMAnager.stopSoftAp()來實現softap的定時關閉。等等。