mvvm pattern

This commit is contained in:
2021-11-12 13:03:12 +00:00
parent ad0779c454
commit 8fb0b6238e
9 changed files with 232 additions and 59 deletions

View File

@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Enumeration;
namespace BlueControl.uwp.Services
{
public class BluetoothService : IBluetoothService
{
private bool control = false;
public BluetoothService()
{
}
private BluetoothLEAdvertisementWatcher bleWatcher = null;
public async Task<bool> IsConnectedAsync(string deviceName)
{
return (from x in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)) where x.Name == deviceName select x).Any();
}
public async Task<bool> IsPairedAsync(string deviceName)
{
return (from x in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true)) where x.Name == deviceName select x).Any();
}
public async Task UnpairAsync(string deviceName)
{
var device = (from x in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true)) where x.Name == deviceName select x).FirstOrDefault();
if (device != null)
await device.Pairing.UnpairAsync();
}
private Action<string> pinCreatedCallBack = null;
private void Custom_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
{
if (this.pinCreatedCallBack != null) pinCreatedCallBack(args.Pin);
args.Accept(args.Pin);
}
public async Task StartAutoPairingAsync(string deviceName, Action onFoundTryingToPair, Action<string> onPinCreated, Action onSuccess, Action onError)
{
pinCreatedCallBack = onPinCreated;
if (this.bleWatcher == null)
{
this.bleWatcher = new BluetoothLEAdvertisementWatcher();
this.bleWatcher.Received += async (sender, args) =>
{
try
{
if (!args.IsConnectable || control == true) return;
control = true;
var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
if (dev.Name == deviceName && dev.DeviceInformation.Pairing.CanPair)
{
if (onFoundTryingToPair != null) onFoundTryingToPair();
System.Threading.Thread.Sleep(3000);
dev.DeviceInformation.Pairing.Custom.PairingRequested += Custom_PairingRequested;
var t = await dev.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.DisplayPin, DevicePairingProtectionLevel.Encryption);
if (t.Status == DevicePairingResultStatus.Paired)
{
this.bleWatcher.Stop();
if (onSuccess != null) onSuccess();
}
else
if (onError != null) onError();
}
}
catch { }
control = false;
};
}
this.bleWatcher.Start();
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlueControl.uwp.Services
{
public interface IBluetoothService
{
Task<bool> IsConnectedAsync(string deviceName);
Task<bool> IsPairedAsync(string deviceName);
Task UnpairAsync(string deviceName);
Task StartAutoPairingAsync(string deviceName, Action onFoundTryingToPair, Action<string> onPinCreated, Action onSuccess, Action onError);
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlueControl.uwp.Services
{
public interface IShellService
{
Task UiStaRunAsync(Action action);
void CloseApplication();
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlueControl.uwp.Services
{
public class UiShellService : IShellService
{
public async Task UiStaRunAsync(Action action)
{
if (StaDispatcher != null)
await StaDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => action());
}
public Windows.UI.Core.CoreDispatcher StaDispatcher { get; set; }
public UiShellService(Windows.UI.Core.CoreDispatcher dispacher)
{
this.StaDispatcher = dispacher;
}
public void CloseApplication()
{
App.Current.Exit();
}
}
}