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
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();
}
}
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.