mvvm pattern
This commit is contained in:
89
src/BlueControl.uwp/Services/BluetoothService.cs
Normal file
89
src/BlueControl.uwp/Services/BluetoothService.cs
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
18
src/BlueControl.uwp/Services/IBluetoothService.cs
Normal file
18
src/BlueControl.uwp/Services/IBluetoothService.cs
Normal 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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
14
src/BlueControl.uwp/Services/IShellService.cs
Normal file
14
src/BlueControl.uwp/Services/IShellService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
30
src/BlueControl.uwp/Services/UiShellService.cs
Normal file
30
src/BlueControl.uwp/Services/UiShellService.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user