Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by balt · May 10, 2012 at 06:10 AM · androidnetworkingudp

Android build is not receiving UDP broadcasts

I'm using UdpClient from System.Net.Sockets to listen to UDP broadcasts. This seems to work fine on OS X and Windows, however, on Android, the machine only sees itself. The HTC I'm testing this on is in the same wireless LAN as all the other machines and the other machines see the broadcast of the Android. It's just the Android that doesn't see anybody else.

This is the code that listens. It obviously works for itself (i.e. it receives its own broadcasts) but not anybody elses. Am I missing some peculiar socket options that need to be set on mobile devices to allow broadcast reception? Searched for that, but couldn't find anything.

 public void StartListeningForUDPBroadcast () {
     UdpClient udpClient = new UdpClient(portnum);
     udpClient.BeginReceive(new AsyncCallback(ReceiveDataCallback), udpClient);
 }
 
 // ReceiveDataCallback
 private void ReceiveDataCallback(IAsyncResult ar) {
     string receiveBytes = "";
     UdpClient udp = (UdpClient)ar.AsyncState;
     IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, portnum);
     
     if (udp != null) {
         byte[] receive = udp.EndReceive(ar, ref ipEndPoint);
         receiveBytes = Encoding.ASCII.GetString(receive);
     }
     
     Debug.Log("Received this: " + receivedBytes;
     // restart listener
 udp.BeginReceive(new AsyncCallback(ReceiveDataCallback), udp);

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image QuantumRyan · Nov 07, 2012 at 09:11 PM 0
Share

What device? I'm having problems with a Transformer Pad Infinity, but Nexus 7's work without fail.

avatar image Qertyfantus · Dec 23, 2014 at 04:42 AM 0
Share

do you solve your problem. my android device has a problem like yours. it could send broadcast message,but it could not receive broadcast message.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by balt · May 12, 2012 at 01:57 AM

The reason this doesn't work is that Android by default doesn't listen to broadcasts unless they're directed at the device address itself. I'm not sure what part of that anyone then would consider broadcast, but anyhow... here's how to get around this:

In a piece of Java code in the android SDK, acquire a multicast lock, and disable it when your app is done:

 WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
 WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
 multicastLock.acquire();

Also add this manifest to your Plugins/Android/AndroidManifest.xml file:

 <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

That's the theory anyway. After putting the manifest there I'm now getting a build errror:

Error building Player: XmlException: 'android' is undeclared namespace. file:///...../Unity3D/Multiplayer Framework/Network/broadcast/Temp/StagingArea/AndroidManifest.xml Line 1, position 81.

And I've only just started to look into how to write a java plugin. Perhaps it could be done by accessing the Java classes directly from Unity?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iiaddr · Mar 19, 2015 at 01:44 AM 0
Share

How do you add the code above to the Android SD$$anonymous$$?

avatar image
0

Answer by Ruud3DV · Apr 03, 2018 at 09:25 AM

I'm having the same problem with a Google Pixel 2 XL. I've added acquiring a multicastLock in Unity through Java calls:

 public bool getMulticastLock(string lockTag)
 {
     using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
     {
         try
         {
             using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
             {
                 multicastLock = wifiManager.Call<AndroidJavaObject>("createMulticastLock", lockTag);
                 multicastLock.Call("acquire");
                 bool isHeld = multicastLock.Call<bool>("isHeld");
                 return isHeld;
             }
         }
         catch (Exception err)
         {
             Debug.Log(err.ToString ());
         }
     }
     return false;
 }

And added the necessary permission in the manifest:

 <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>


But this doesn't seem to make a difference. Other Android devices receive the UDP broadcast packets fine, but the Pixel 2 XL doesn't, only very occasionally.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bierrobot · Apr 16, 2018 at 11:28 PM 0
Share

I am having the same issues for both Google Pixel 2, and Google Pixel 2 XL. Have you figured it out @Ruud3DV ?

avatar image
0

Answer by Bierrobot · Apr 19, 2018 at 10:32 PM

@Ruud3DV, thanks for the tip, your code worked great. The one thing that I realized though after looking at the Logcat is that the Multicast Lock was automatically released after about 1.5 seconds after it was acquired through code. I then just put the code in a coroutine that starts again every second.

 public class MultiCastLockPixel : MonoBehaviour {
 
     public float multicastDelay; // Set it to 1 or less in the editor
     [HideInInspector]
     public bool stopAcquiringLock = false;
 
     public void Start()
     {
         StartCoroutine(acquireMultiCastPeriodically());
     }
 
     private IEnumerator acquireMultiCastPeriodically()
     {
         while (!stopAcquiringLock)
         {
             getMulticastLock("debugMulticast");
             yield return new WaitForSeconds(multicastDelay);
         }
     }
 
     public bool getMulticastLock(string lockTag)
     {
         try
         {
             using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
             {
             
                 using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
                 {
                     AndroidJavaObject multicastLock = wifiManager.Call<AndroidJavaObject>("createMulticastLock", lockTag);
                     multicastLock.Call("acquire");
                     bool isHeld = multicastLock.Call<bool>("isHeld");
                     return isHeld;
                 }
             
             }
         }
         catch (Exception err)
         {
             Debug.Log(err.ToString());
         }
         return false;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by oLDo · Nov 29, 2018 at 01:00 PM

I spent some time with solving same problem. Most important what I found, android is not supporting async receive. I don't know why. I ended up with separated background Thread, where I call standart Receive(). It works well. Also I used multicast instead of broadcast. Because I had more network adapters in PC.

You can take a look on it here MulticastDiscovery

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Networking: UDP Packets dont arrive on Android 2 Answers

Best way to create a UDP network for Android 0 Answers

Multiplayer Networking for Android 0 Answers

UNET can't join android server 0 Answers

Can't receive TUIO messages on Release mode 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges