Wake on lan demo added

This commit is contained in:
Maurice 2024-06-19 16:16:38 +02:00
parent 41d2061d38
commit 9d6aaccc0c
5 changed files with 94 additions and 4 deletions

View File

@ -0,0 +1,53 @@
using System.Net;
using System.Net.Sockets;
namespace DemoAndroid;
[Activity(Label = "WakeOnLanActivity")]
public class WakeOnLanActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.activity_wakeonlan);
var button = FindViewById<Button>(Resource.Id.btnWakeOnLan);
button.Click += (sender, args) =>
{
WakePc();
};
}
private void WakePc()
{
byte[] macAddress = { 0x2c, 0x4d, 0x54, 0x4d, 0x10, 0x0f };
byte[] magicPacket = new byte[102];
// Fill first 6 bytes with 0xFF
for (int i = 0; i < 6; i++)
{
magicPacket[i] = 0xFF;
}
// Repeat MAC address 16 times
for (int i = 1; i <= 16; i++)
{
for (int j = 0; j < 6; j++)
{
magicPacket[i * 6 + j] = macAddress[j];
}
}
using var client = new UdpClient();
client.EnableBroadcast = true;
var endpoint = new IPEndPoint(IPAddress.Broadcast, 9); // 255.255.255.255 port 9
for (int i = 0; i < 5; i++)
{
// Try 5 times
client.Send(magicPacket, magicPacket.Length, endpoint);
}
}
}

View File

@ -10,9 +10,13 @@
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<!-- With AOT, our app is crashing in Release mode -->
<AotAssemblies>False</AotAssemblies>
<RunAOTCompilation>False</RunAOTCompilation>
<PublishAot>False</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QRCoder-ImageSharp" Version="0.10.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Activities\" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,6 @@
using Android.Content;
using Android.Graphics;
using QRCoder;
using Exception = Java.Lang.Exception; // QRCoder-ImageSharp else it crashes
using QRCoder; // QRCoder-ImageSharp else it crashes
namespace DemoAndroid;
@ -18,7 +18,7 @@ public class MainActivity : Activity
SetContentView(Resource.Layout.activity_main);
var etInput = FindViewById<EditText>(Resource.Id.etInput) ?? throw new Exception("EditText #etInput not found");
_ivQr = FindViewById<ImageView>(Resource.Id.ivQR) ?? throw new Exception("ImageView #ivQR not found");;
_ivQr = FindViewById<ImageView>(Resource.Id.ivQR) ?? throw new Exception("ImageView #ivQR not found");
etInput.TextChanged += (sender, args) =>
{
@ -29,6 +29,12 @@ public class MainActivity : Activity
CreateQr();
}
};
var btnWol = FindViewById<Button>(Resource.Id.btnWolPage) ?? throw new Exception("Button #btnWol not found");
btnWol.Click += (_, _) =>
{
StartActivity(new Intent(this, typeof(WakeOnLanActivity)));
};
}
private void CreateQr()

View File

@ -12,4 +12,12 @@
<ImageView android:id="@+id/ivQR"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/btnWolPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to wake on lan page"
android:background="@color/blue"
android:textColor="@color/white"
android:height="15pt"/>
</LinearLayout>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start PC by tapping button below" />
<Button android:id="@+id/btnWakeOnLan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start PC"
android:textColor="@color/white"
android:background="@color/blue"
android:height="15pt"
android:layout_margin="10pt"/>
</LinearLayout>