Skip to content

Commit

Permalink
Android demo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
buldo committed Nov 12, 2023
1 parent 47c3cd1 commit 9b63594
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 213 deletions.
10 changes: 4 additions & 6 deletions src/Rtl8812auNet.AndroidDemo/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
HorizontalOptions="Center" />

<Label
Text="Hello, World!"
Text="OHD receiver"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />

<Label
Text="Welcome to .NET Multi-platform App UI"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
x:Name="StatusLabel"
Text="Push the button!"
FontSize="18"
HorizontalOptions="Center" />

<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Text="Start"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />

Expand Down
95 changes: 72 additions & 23 deletions src/Rtl8812auNet.AndroidDemo/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,88 @@
using Android.App;
using System.Net;

using Android.App;
using Android.Content;
using Android.Hardware.Usb;
using LibUsbDotNet.Main;
using Rtl8812auNet.AndroidDemo.Platforms.Android;
using Rtl8812auNet.AndroidDemo.RtlUsb;
using Rtl8812auNet.Rtl8812au;
using Application = Android.App.Application;

namespace Rtl8812auNet.AndroidDemo
namespace Rtl8812auNet.AndroidDemo;

public partial class MainPage : ContentPage
{
public partial class MainPage : ContentPage
private static string ACTION_USB_PERMISSION = "zone.bld.receiverapp.USB_PERMISSION";
private readonly MyBroadcastReceiver _broadcastReceiver;

public MainPage()
{
InitializeComponent();
_broadcastReceiver = new MyBroadcastReceiver(this);
}

private void OnCounterClicked(object sender, EventArgs e)
{
var context = Android.App.Application.Context;
var usbManager = (UsbManager)context.GetSystemService(Android.Content.Context.UsbService);

var (_, device) = usbManager.DeviceList.FirstOrDefault(pair => pair.Value.ManufacturerName == "Realtek");
if (device != null)
{
if (usbManager.HasPermission(device))
{
StatusLabel.Text = $"Device found:{device.DeviceName}" + System.Environment.NewLine + "Starting...";
StartService(device);
}
else
{
var pi = PendingIntent.GetBroadcast(
Android.App.Application.Context,
0,
new Intent(ACTION_USB_PERMISSION),
PendingIntentFlags.Immutable);
_broadcastReceiver.Dev = device;
var filter = new IntentFilter(ACTION_USB_PERMISSION);
context.RegisterReceiver(_broadcastReceiver, filter);

StatusLabel.Text = $"Device found:{device.DeviceName}" + System.Environment.NewLine + "Requesting permissions...";
usbManager.RequestPermission(device, pi);
}
}
else
{
StatusLabel.Text = "No RTL8812AU device found";
}

Console.WriteLine("READY");
}

private void StartService(UsbDevice device)
{
int count = 0;
StatusLabel.Text = $"Device found:{device.DeviceName}" + System.Environment.NewLine + "Starting...";
AndroidServiceManager.Device = device;
var context = Android.App.Application.Context;
var usbManager = (UsbManager)context.GetSystemService(Android.Content.Context.UsbService);
AndroidServiceManager.Connection = usbManager.OpenDevice(device);
AndroidServiceManager.StartWfbService();
}

public MainPage()
public class MyBroadcastReceiver : BroadcastReceiver
{
private readonly MainPage _parent;

public MyBroadcastReceiver(MainPage parent)
{
InitializeComponent();
_parent = parent;
}

private void OnCounterClicked(object sender, EventArgs e)
public override void OnReceive(Context context, Intent intent)
{
var context = Android.App.Application.Context;
var usbManager = (UsbManager)context.GetSystemService(Android.Content.Context.UsbService);
var dev = usbManager.DeviceList.Single(pair => pair.Value.ManufacturerName == "Realtek").Value;
var pi = PendingIntent.GetBroadcast(
Application.Context,
0,
new Intent(Context.UsbService),
PendingIntentFlags.Immutable);
usbManager.RequestPermission(dev, pi);

var conn = usbManager.OpenDevice(dev);
var rtlUsbDevice = new RtlUsbDevice(dev, conn);
var rtl = new Rtl8812aDevice(rtlUsbDevice);
rtl.Init();

Console.WriteLine("READY");
_parent.StatusLabel.Text = $"Device found:{Dev.DeviceName}" + System.Environment.NewLine + "Starting...";
_parent.StartService(Dev);
}

public UsbDevice Dev { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Rtl8812auNet.AndroidDemo/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static MauiApp CreateMauiApp()
});

#if DEBUG
builder.Logging.AddDebug();
builder.Logging.AddDebug().SetMinimumLevel(LogLevel.Trace);
#endif

return builder.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Android.Hardware.Usb;

namespace Rtl8812auNet.AndroidDemo.Platforms.Android;

public static class AndroidServiceManager
{
public static MainActivity MainActivity { get; set; }

public static bool IsRunning { get; set; }
public static UsbDevice Device { get; set; }
public static UsbDeviceConnection Connection { get; set; }

public static void StartWfbService()
{
if (MainActivity == null)
{
return;
}

MainActivity.StartService();
}

public static void StopWfbService()
{
if (MainActivity == null)
{
return;
}
MainActivity.StopService();
IsRunning = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Android.App;
using Android.Content;
using Android.OS;
using Bld.WlanUtils;
using Java.Util.Logging;

using LibUsbDotNet.LibUsb;
using Microsoft.Extensions.Logging;
using Rtl8812auNet.AndroidDemo.RtlUsb;
using Rtl8812auNet.Rtl8812au;
using Rtl8812auNet.Rtl8812au.Models;
using ChannelWidth = Rtl8812auNet.Rtl8812au.Enumerations.ChannelWidth;

namespace Rtl8812auNet.AndroidDemo.Platforms.Android;

[Service]
public class DriverBackgroundService : Service
{
private WiFiDriver _driver;
private Rtl8812aDevice _device;
private ILogger<DriverBackgroundService> _logger;
private WlanChannel _channel = Channels.Ch036;


public override IBinder OnBind(Intent intent)
{
return null;
}

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
var loggerFactory = IPlatformApplication.Current.Services.GetRequiredService<ILoggerFactory>();
_logger = loggerFactory.CreateLogger<DriverBackgroundService>();
_driver = new WiFiDriver(loggerFactory, false);
_device = _driver.CreateRtlDevice(new RtlUsbDevice(AndroidServiceManager.Device, AndroidServiceManager.Connection, loggerFactory.CreateLogger<RtlUsbDevice>()));
_device.Init(PacketProcessor, CreateCurrentChannel());
_device.SetMonitorChannel(CreateCurrentChannel());

return StartCommandResult.Sticky;
}

private Task PacketProcessor(ParsedRadioPacket arg)
{
_logger.LogDebug("Received");
return Task.CompletedTask;
}

private SelectedChannel CreateCurrentChannel()
{
return new SelectedChannel
{
Channel = (byte)_channel.ChannelNumber,
ChannelOffset = 0,
ChannelWidth = ChannelWidth.CHANNEL_WIDTH_20
};
}
}
19 changes: 19 additions & 0 deletions src/Rtl8812auNet.AndroidDemo/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Rtl8812auNet.AndroidDemo.Platforms.Android;

namespace Rtl8812auNet.AndroidDemo
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
public MainActivity()
{
AndroidServiceManager.MainActivity = this;
}

public void StartService()
{
var serviceIntent = new Intent(this, typeof(DriverBackgroundService));
serviceIntent.PutExtra("inputExtra", "Background Service");
StartService(serviceIntent);
}

public void StopService()
{
var serviceIntent = new Intent(this, typeof(DriverBackgroundService));
StopService(serviceIntent);
}
}
}
10 changes: 0 additions & 10 deletions src/Rtl8812auNet.AndroidDemo/Platforms/MacCatalyst/AppDelegate.cs

This file was deleted.

30 changes: 0 additions & 30 deletions src/Rtl8812auNet.AndroidDemo/Platforms/MacCatalyst/Info.plist

This file was deleted.

17 changes: 0 additions & 17 deletions src/Rtl8812auNet.AndroidDemo/Platforms/MacCatalyst/Program.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/Rtl8812auNet.AndroidDemo/Platforms/Tizen/Main.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Rtl8812auNet.AndroidDemo/Platforms/Tizen/tizen-manifest.xml

This file was deleted.

10 changes: 0 additions & 10 deletions src/Rtl8812auNet.AndroidDemo/Platforms/iOS/AppDelegate.cs

This file was deleted.

Loading

0 comments on commit 9b63594

Please sign in to comment.