Wi-Fi Direct Android

前端 未结 2 1118
囚心锁ツ
囚心锁ツ 2020-12-30 16:57

I want to transfer files between 2 devices via Wi-Fi Direct.

I wanted to do the same thing as in WifiDirectDemo, but I can\'t transfer data from the group owner to

相关标签:
2条回答
  • 2020-12-30 17:39

    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();
        }
    }
    
    0 讨论(0)
  • 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.
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题