Callback on device connecting to Wifi Hotspot

空扰寡人 提交于 2019-12-10 15:04:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!