Voice and Wi-Fi controlled Lamp III

Yelling on your Raspberry is not always the best solution, so let’s take a look how to communicate between two C# applications.

When it comes to peer-to-peer communication between UWP and Xamarin apps, we can use TCP Sockets. Unlike UDP, it establishes a long-lasting and reliable connection. However, please note that this implementation is not secured and it’s aimed to use only on private networks.

UWP Server class

First of all, we need to implement server-side listener, which will listen for any incoming requests. Create a new class with TcpListener property, initialize it, and listen in a loop for any incoming connections. When the connection is established, listen for commands, process them and provide a relevant response back to the client. In this scenario, I’m dealing with strings, however, for bigger projects, it is more reasonable to use serializable classes for commands and responses of your own.

Xamarin Client

Now, we are ready to implement the client in order to reach our server. Because of different Mono Frameworks for mobile platforms, it’s not possible to create a single class client and use it on iOS, Windows, and Android. Fortunately, the code is 99.9% the same, so all we need to do is copy our class to each project and then use DependencyService to access it from PCL (portable class library).

PCL abstract client

Platform-specific client

The platform-specific class inherits from abstract client implemented in PCL project and is marked with assembly attribute.

Accessing platform-specific client from PCL


And that’s pretty much all from Voice and Wi-Fi controlled Lamp tutorial 🙂

We’ve seen how to control simple electronic device, either with your voice or mobile phone. Hope it helps to you!

Leave a Reply