问题
I am creating WiFi AP programmatically in my application. Do i get any broadcast when new devices connect to my AP.
I know we can get the list of the connected devices from the /proc/net/arp but i need a callback when there is a new connection.
Any help is appreciated.
回答1:
If you don't need to use the AP to connect to the internet but just to communicate in a LAN, you can create a P2P group with WifiP2pManager instance createGroup and listen to WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION with a broadcast receiver.
Like this:
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
Manager.requestConnectionInfo(mChannel, new WifiP2pManager.ConnectionInfoListener(){
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
if (info.isGroupOwner) {
mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() {
@Override
public void onGroupInfoAvailable(WifiP2pGroup group) {
//This is the size you want
group.getClientList().size();
}
});
}
}
});
}
}
For more detail look at: http://developer.android.com/guide/topics/connectivity/wifip2p.html
来源:https://stackoverflow.com/questions/18378143/callback-on-device-connecting-to-wifi-hotspot