Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Broadcast receiver in Unity

Discussion in 'Scripting' started by sebhah, Oct 26, 2022.

  1. sebhah

    sebhah

    Joined:
    Jan 6, 2019
    Posts:
    26
    Hello,

    I have a question; I am busy building an app for PDA (Scanner/terminal) devices. There is only one problem. They use a broadcast to receive some input.

    But how can I receive the broadcast value from the Android PDA in Unity?

    Kind regards,

    Seb
     
  2. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    Hello @sebhah. If I understand your question you want to write some c# code to receive UDP datagrams from anywhere on your subnet. It's pretty easy to do using System.Net and System.Net.Sockets. You will need to figure out your subnet and the port the sender is sending to. Once you have that information you can instantiate a UdpClient and IPEndPoint. Then call receive from the client on the end point inside a loop until you are ready to shutdown your client. Here is an example UDPListener that should do what you want.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    If I understand the problem correctly, you need an Android broadcast receiver and forward the data from received intent to Unity code?
    If so, the receiver would have to implemented in Java. For calling C# code from Java, have a look at AndroidJavaProxy in our scripting api documentation.