Wi-Fi Direct Android

痞子三分冷 提交于 2019-11-30 07:42:35

You can delete all groups through reflection but, it's bit of a hack and class members might change later

 private void deletePersistentInfo() {
    try {

        Class persistentInterface = null;

        //Iterate and get class PersistentGroupInfoListener
        for (Class<?> classR : WifiP2pManager.class.getDeclaredClasses()) {
            if (classR.getName().contains("PersistentGroupInfoListener")) {
                persistentInterface = classR;
                break;
            }

        }

        final Method deletePersistentGroupMethod = WifiP2pManager.class.getDeclaredMethod("deletePersistentGroup", new Class[]{Channel.class, int.class, ActionListener.class});




        //anonymous class to implement PersistentGroupInfoListener which has a method, onPersistentGroupInfoAvailable
        Object persitentInterfaceObject =
                java.lang.reflect.Proxy.newProxyInstance(persistentInterface.getClassLoader(),
                        new java.lang.Class[]{persistentInterface},
                        new java.lang.reflect.InvocationHandler() {
                            @Override
                            public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
                                String method_name = method.getName();

                                if (method_name.equals("onPersistentGroupInfoAvailable")) {
                                    Class wifiP2pGroupListClass =  Class.forName("android.net.wifi.p2p.WifiP2pGroupList");
                                    Object wifiP2pGroupListObject = wifiP2pGroupListClass.cast(args[0]);

                                    Collection<WifiP2pGroup> wifiP2pGroupList = (Collection<WifiP2pGroup>) wifiP2pGroupListClass.getMethod("getGroupList", null).invoke(wifiP2pGroupListObject, null);
                                    for (WifiP2pGroup group : wifiP2pGroupList) {
                                        deletePersistentGroupMethod.invoke(wifiP2pManager, channel, (Integer) WifiP2pGroup.class.getMethod("getNetworkId").invoke(group, null), new ActionListener() {
                                            @Override
                                            public void onSuccess() {
                                                //All groups deleted
                                            }

                                            @Override
                                            public void onFailure(int i) {

                                            }
                                        });
                                    }
                                }

                                return null;
                            }
                        });

        Method requestPersistentGroupMethod =
                WifiP2pManager.class.getDeclaredMethod("requestPersistentGroupInfo", new Class[]{Channel.class, persistentInterface});

        requestPersistentGroupMethod.invoke(wifiP2pManager, channel, persitentInterfaceObject);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Chait

To send data you need to know the IP address (not the device address) of the receiver. For the P2P client, the IP address of group_owner is available in the WifiP2pInfo variable, so it can use this to send data to the group owner. If the group owner knows the IP address of the P2P client to which it wants to send data, then it can also send files. This can be achieved in two ways.

  1. Group owner assigns the IP addresses to the clients and stores the information about it.
  2. Every newly added client sends its IP address to the group owner at the time of joining the group.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!