/
BLEUDPListener.cs
48 lines (41 loc) · 1.14 KB
/
BLEUDPListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace BLELocator
{
public class BLEUDPListener
{
private const int listenPort = 11000;
public BLEUDPListener()
{
StartListener();
}
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (!done)
{
Thread.Sleep(100);
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive(ref groupEP);
Console.Write(
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
}
}