Developing applications for the Windows 10 IoT Core on Raspberry Pi, you soon get familiar with the "Windows IoT Core Watcher" that installs on your development machine together with the ISO for the Raspberry Pi image:
I was thinking that it would be nice to have this functionality in my own code, so I used Wireshark to try to find out the magic behind the scene.
It turned out that the Raspberry Pi (or rather the Windows 10 IoT Core) broadcasted a 150 bytes big UDP package (around) every fifth second, carrying the information presented by the watcher application.
This is the content of the byte array my device was sending (where the middle part of the MAC address bytes are blanked out with XX):
It wasn't hard to realize that the bytes were a UTF-16 text string, which means that the package effectively contained 75 Unicode characters.
Since only ASCII characters (actually only English letters, regular digits and a couple of punctuation symbols) are present, every second byte is actually unused. (They will only be utilized if you manage to give your device a name of non-English characters.)
Decoding the bytes as UTF-16 characters you will get this:
Note that the empty cells with lighter backgrounds above contain binary zero and are therefore completely empty (i.e. not even space characters -- a total blank, as you can tell from the byte array).
Anyway, I wrapped this into a library in C#, for simple integration in other projects.
As an example, here is the Main
method of a regular Windows console application, that listens for devices found on the network:
using System;
using Laserbrain.WinIoTCoreListener.Lib;
namespace Laserbrain.WinIoTCoreListener.ConsoleExample
{
internal static class Program
{
private static void Main()
{
using (IWinIotCoreListener winIotCoreListener = WinIotCoreListenerFactory
.Create())
{
winIotCoreListener.OnDeviceInfoUpdated += (s, e) =>
{
Console.WriteLine(e.UpdateStatus + ": " + e.DeviceInfo);
};
Console.WriteLine("Up'n'running.");
Console.WriteLine("Press SPACE to list devices!");
Console.WriteLine("Press ESCAPE to exit!");
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Spacebar:
Console.WriteLine("-------------------------");
foreach (DeviceInfo deviceInfo in winIotCoreListener.DeviceInfos)
{
Console.WriteLine("* " + deviceInfo);
}
Console.WriteLine("-------------------------");
break;
case ConsoleKey.Escape:
return;
}
}
}
}
}
}
How it worksGet an IWinIotCoreListener
by calling the Create
method of the static WinIotCoreListenerFactory
. As long as you don't dispose the listener given to you, it will continue to fire the OnDeviceInfoUpdated
event. This event is fired each time a new device is found, an existing device changes a property or when a device stops broadcasting its data package. The UpdateStatus
property of the event args tells you the kind of change (an enum saying Found
, Updated
or Lost
). The DeviceInfo
property of the event args holds all the properties received in the broadcast package: MachineName
, IpAddress
and the MAC address -- both in string format (MacAddressString
) and as a byte array (MacAddressBytes
).
You can also -- at any time -- get the current list of devices from the DeviceInfos
property of the listener interface.
Calling the Dispose
method on the listener will make it stop receiving broadcasts and free all its resources.
You can get the library by downloading this NuGet package, or, if you prefer, you can get the full source code from GitHub.
Enjoy! :-)
Comments
Please log in or sign up to comment.