Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-73138

QUdpSocket::bind() does not acquire WifiManager.MulticastLock on Android

    XMLWordPrintable

Details

    • Android

    Description

      Some devices (like the Pixel 3) require you to acquire a "Multi cast lock". This lock enables apps on the  device to receive broadcasts while acquired.

      Not acquiring this lock is often not harm full since the locking takes effect system wide. Therefore, a single app acquiring the lock is enough for all apps to receive broadcasts. But you are screwed if no other app does this for you.

      See https://developer.android.com/reference/android/Manifest.permission

      android.permission.CHANGE_WIFI_MULTICAST_STATE

      See also https://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock

       Allows an application to receive Wifi Multicast packets. Normally the Wifi stack filters out packets not explicitly addressed to this device. Acquring a MulticastLock will cause the stack to receive packets addressed to multicast addresses. Processing these extra packets can cause a noticeable battery drain and should be disabled when not needed. 

      Probably a related issue: https://bugreports.qt.io/browse/QTBUG-64233  "UDP broadcast packets not received on Android 8.0"

       

      We deployed the following workaround:

       

      /*
       * \copyright Copyright (c) 2019 Governikus GmbH & Co. KG, Germany
       */
      
      package com.governikus.ausweisapp2;
      
      import android.content.Context;
      import android.net.wifi.WifiManager;
      import android.util.Log;
      
      
      public final class MulticastLockJniBridgeUtil
      {
      	private static final String LOG_TAG = AusweisApp2Service.LOG_TAG;
      
      	private static WifiManager.MulticastLock cLock;
      
      
      	private MulticastLockJniBridgeUtil()
      	{
      	}
      
      
      	public static synchronized void acquire(Context pContext)
      	{
      		if (cLock == null)
      		{
      			WifiManager wifi = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);
      			cLock = wifi.createMulticastLock("AusweisApp2");
      			cLock.setReferenceCounted(true);
      		}
      
      		cLock.acquire();
      		Log.d(LOG_TAG, "Multicast lock: " + cLock.toString());
      	}
      
      
      	public static synchronized void release(Context pContext)
      	{
      		cLock.release();
      		Log.d(LOG_TAG, "Multicast lock released.");
      	}
      
      
      }

       

      Attachments

        Issue Links

          Activity

            People

              qtandroidteam Qt Android Team
              llsag85dywftnhuo Jan Murawski
              Votes:
              5 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated: