Created
November 19, 2020 20:40
-
-
Save IanMercer/5f354733891bea4e0e88560801eb99ab to your computer and use it in GitHub Desktop.
Sample UDP Receiver to listen for decoded Bluetooth messages from PI-SNIFFER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
namespace UDPSample | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
_ = new UDPListener().Get(); | |
Console.WriteLine("Press any key to quit"); | |
Console.ReadKey(); | |
} | |
} | |
public class UDPListener | |
{ | |
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); | |
private int listenPort = 7779; | |
public async Task Get() | |
{ | |
var cancellationToken = cancellationTokenSource.Token; | |
using (UdpClient listener = new UdpClient(listenPort)) | |
{ | |
IPEndPoint listenEndPoint = new IPEndPoint(IPAddress.Broadcast, listenPort); | |
while (!cancellationToken.IsCancellationRequested) | |
{ | |
var result = await listener.ReceiveAsync(); | |
//Console.WriteLine($"Received broadcast message from client {result.RemoteEndPoint.Address}"); | |
string mess = Encoding.ASCII.GetString(result.Buffer); | |
//Console.WriteLine($"Decoded data is: {mess}"); | |
var m = JsonConvert.DeserializeObject<Message>(mess); | |
Console.WriteLine($"{m.mac} {m.name} a {m.category} {m.distance:0.0}m from {m.from}"); | |
} | |
} | |
} | |
} | |
public class Message | |
{ | |
public string from { get; set; } | |
public string description { get; set; } | |
public string platform { get; set; } | |
public int rssi_one_meter { get; set; } | |
public float rssi_factor { get; set; } | |
public int people_distance { get; set; } | |
public int people_closest_count { get; set; } | |
public float people_in_range_count { get; set; } | |
public int seq { get; set; } | |
public string mac { get; set; } | |
public string name { get; set; } | |
public string supersededby { get; set; } | |
public string alias { get; set; } | |
public int addressType { get; set; } | |
public string category { get; set; } | |
public bool paired { get; set; } | |
public bool connected { get; set; } | |
public bool trusted { get; set; } | |
public int deviceclass { get; set; } | |
public int appearance { get; set; } | |
public int manufacturer_data_hash { get; set; } | |
public int service_data_hash { get; set; } | |
public int uuids_length { get; set; } | |
public int uuid_hash { get; set; } | |
public int txpower { get; set; } | |
public int last_sent { get; set; } | |
public float distance { get; set; } | |
public int earliest { get; set; } | |
public int latest { get; set; } | |
public int count { get; set; } | |
public int column { get; set; } | |
public int try_connect_state { get; set; } | |
public int nt { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment