Skip to content

Commit

Permalink
Acquire MulticastLock for local discovery to work on Android 11 (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Catfriend1 committed May 16, 2021
1 parent 6ba250c commit ee2642b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<uses-permission android:name="android.permission.CAMERA" />
<!-- MANAGE_EXTERNAL_STORAGE is required on Android 11 "R" -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<!-- CHANGE_WIFI_MULTICAST_STATE is required for local discovery to work on Android 11 "R" -->
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

<application
android:allowBackup="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
Expand Down Expand Up @@ -158,12 +160,19 @@ public String run(boolean returnStdOut) throws ExecutableNotFoundException {
);
}

MulticastLock multicastLock = null;
Process process = null;
try {
if (wakeLock != null) {
wakeLock.acquire();
}

// See issue #735: Android 11 blocks local discovery if we did not acquire MulticastLock.
WifiManager wifi = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

/**
* Setup and run a new syncthing instance
*/
Expand Down Expand Up @@ -250,6 +259,10 @@ public String run(boolean returnStdOut) throws ExecutableNotFoundException {
if (wakeLock != null) {
wakeLock.release();
}
if (multicastLock != null) {
multicastLock.release();
multicastLock = null;
}
if (process != null) {
process.destroy();
}
Expand Down

0 comments on commit ee2642b

Please sign in to comment.