Wi-Fi Direct Android

前端 未结 2 1210
囚心锁ツ
囚心锁ツ 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 wifiP2pGroupList = (Collection) 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();
        }
    }
    

提交回复
热议问题