This slide will teach you how to enable your cross platform mobile app communicate with IoT Devices via Network Protocol, as such, UDP.
#xamarin #dotnet #iot #mobileapp #app #development
Click here for viewing full tutorial @ www.techconnect.io
Twitter: @hmheng
Blog: www.techconnect.io
App Development: How Mobile App Can Communicate With IoT Device via Network
1. How Mobile App
Can Communicate
with IoT Devices
via Network
Marvin Heng
Twitter : @hmheng
Blog : http://hmheng.azurewebsites.net
Github: https://github.com/hmheng
2. Pre-requisite
• Installed Visual Studio 2017 for Windows with Xamarin
Cross-Platform component & all necessary platform-
specific SDK. (Download a FREE Community Version here)
• Create a cross platform mobile app with .NETStandard 2.0
(Learn to how to create one here)
3. Communicate between App & IoT Devices
1. Launch your Visual Studio 2017 (VS2017), follow this
tutorial to create a .NET Standard Cross Platform Mobile App.
4. 2. Let’s delete the “Welcome to Xamarin.Forms!” label.
3. Add in these lines to MainPage.xaml.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:HmhengXamNet"
x:Class="HmhengXamNet.MainPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Entry x:Name="txtIPAddress" Text="192.168.0.103" />
<Entry x:Name="txtPort" Text="7000" />
<Entry x:Name="txtMessage" Text="Message" />
<Button x:Name="btnConnect" Text=“Send" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Communicate between App & IoT Devices
3
2
5. 4. Now, let’s create a SendUDP function that’ll eventually read
the entered IP, Port Number & Message and perform a send.
public void ConnectUDP(object sender, EventArgs e)
{
UdpClient ucpClient = new UdpClient();
IPAddress ipAddress = IPAddress.Parse(txtIPAddress.Text);
IPEndPoint endPoint = new IPEndPoint(ipAddress,
Int32.Parse(txtPort.Text));
Byte[] data = Encoding.ASCII.GetBytes(txtMessage.Text);
ucpClient.Send(data, data.Length, endPoint);
}
Communicate between App & IoT Devices
4
6. 5. You may notice red underlined errors that can be resolved
by adding reference to System.Net & System.Net.Sockets.
…
using System.Net;
using System.Net.Sockets;
…
Communicate between App & IoT Devices
5
7. 6. Last, but not least, we need to bind the btnConnect to the
function of SendUDP.
public MainPage()
{
InitializeComponent();
btnConnect.Clicked += SendUDP;
}
Communicate between App & IoT Devices
6
8. 7. Let’s compile, run and send some messages through
network to an App that’s listening to same specified UDP port.
Communicate between App & IoT Devices
10. How Mobile App
Can Communicate
with IoT Devices
via Network
Marvin Heng
Twitter : @hmheng
Blog : http://hmheng.azurewebsites.net
Github: https://github.com/hmheng