Callback on device connecting to Wifi Hotspot

2019-06-20 13:53发布

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条回答
疯言疯语
2楼-- · 2019-06-20 14:57

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

查看更多
登录 后发表回答