Wifi-Direct always disconnects after thirty minutes

The following looks like the DHCP lease for the 2 peers is 30 minutes and the renew is failing.

E/DhcpStateMachine: DHCP renew failed on p2p-wlan0-0: Timed out waiting for DHCP Renew to finish
D/WifiP2pService: GroupCreatedState{ what=196613 }
E/WifiP2pService: DHCP failed

The service advertiser and service browser have IP addresses, the browser's issued by DHCP on the advertiser. If the DHCP fails to renew, then the connection is lost and the devices have to reconnect. It looks like the lease renewal is attempted at 29 min 18 sec, and times out at the 30-minute lease time. At this point the browser has to reconnect. If the DHCP server doesn't remember the browser's IP address,or if the framework always issues a new IP, then what you describe will happen. Hope this helps.

Edit: this post seems to validate that the group owner acts as a DHCP server: Get peer device's IP address in wifi-direct p2p connection . I don't know how you could increase the lease time on WiFi-Direct, though.

Edit: this link talks about some problems with DHCP on android: https://www.net.princeton.edu/android/android-stops-renewing-lease-keeps-using-IP-address-11236.html

Edit: Possible solution is contained in https://www.net.princeton.edu/android/android-11236-partial-workaround.html , appended below. The idea is to use settings on the group owner's device.

Procedure

Depending upon the version of Android on your device, there should be a "Wi-Fi sleep policy" setting, a "Wi-Fi disconnect policy" setting, or a "Keep Wi-Fi on during Sleep" setting. (Your device will have only one of these; the name varies.) It should be available at (only) one of the following locations:

      Settings 
         -> Wireless & networks 
            -> Wi-Fi settings 
               -> Menu (button) 
                  -> Advanced 
                     -> Wi-Fi sleep policy

      Settings
         -> Wireless & networks
            -> Wi-Fi settings
               -> Wi-Fi disconnect policy

      Settings
         -> Wireless & networks
            -> Wi-Fi settings
               -> Menu (button)
                  -> Advanced
                     -> Keep Wi-Fi on during Sleep

If the Wi-Fi settings item is greyed-out, you may need to turn on Wi-Fi before you can select this item.
If your device has a "Wi-Fi sleep policy" or " Wi-Fi disconnect policy", it should offer several choices. These choices available vary among different versions of Android. At least one of the choices should be "After 15 mins" or "When screen turns off". Select that choice.

Otherwise, if your device has a "Keep Wi-Fi on during Sleep" setting, it should offer several choices. These choices available may vary among different versions of Android. At least one of the choices should be "Never". Select that choice.

If you have any software installed on your Android device which modifies the way Android turns Wi-Fi on or off, then reconfigure that software so it no longer controls the device's Wi-Fi interface. Alternatively, disable or remove that software.

An example would be an application designed to keep your device's Wi-Fi interface always turned on. Another would be an application configured to turn on your device's Wi-Fi interface based on certain conditions (for example, location, time of day, or battery charge).

These applications are not the cause of the bugs. However, they can prevent this partial workaround from being effective, because these may be configured to override Android's "Wi-Fi Sleep Policy" (or "Wi-Fi disconnect policy"), causing the Wi-Fi interface to remain connected to the wireless network for an extended period while the Android device is asleep.

Every time you upgrade the Android software/firmware in the future, verify that the setting you selected above (the "Wi-Fi sleep policy", "Wi-Fi disconnect policy", or "Keep Wi-Fi on during Sleep" setting) remains configured as described above. Such upgrades sometimes modify existing settings; you must make a point of checking (and if necessary, changing) this setting immediately after upgrading.

Take care that any software you install in the future does not modify the way Android turns on or off Wi-Fi. 

Digging through the sources for p2, the bottom line is that dhcp stuff for p2p is done by ethernetworkfactory, bypassing the native dhcp server (see http://book2s.com/java/src/package/com/android/server/ethernet/ethernetnetworkfactory.html#199b9c81a59238cafe64b4e28b0c71ce), and here's an interesting code fragment:

/* Called by the NetworkFactory on the handler thread. */
public void onRequestNetwork() {
    // TODO: Handle DHCP renew.

so it appears that the dhcp lease will never renew in the p2p framework (see below). Frankly, I'd have to check on that; this is a nontrivial bug if true. If it is true, then the solution of reconnecting every 25 mins as per the above post may be the only way. Note: dhcp renewal happens at 48% of the lease renewal time to run previous to the native dhcp renew (see https://android.googlesource.com/platform/frameworks/base/+/c3a2858/core/java/android/net/DhcpStateMachine.java); the relevant lines are

//Do it a bit earlier than half the lease duration time
//to beat the native DHCP client and avoid extra packets
//48% for one hour lease time = 29 minutes

So in p2pdirect, it looks like there is a layer that avoids the native dhcp renewal but doesn't implement it itself.


you can develop a service. and then reconnect after every 25 minutes programmatically.

or

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {     
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) 
            Log.d("WifiReceiver", "Have Wifi Connection");
        else
            Log.d("WifiReceiver", "Don't have Wifi Connection");    
    }   
};

In order to access the active network info you need to add the following uses-permission to your AndroidManifest.xml:

And the following intent receiver (or you could add this programmatically...)

<receiver android:name=".WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

[reference:] [Wifi Connect-Disconnect Listener1

thus you can solve your problem. :)