Skip to content

Commit

Permalink
Check for SDK Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AskBojesen committed May 16, 2024
1 parent c894e46 commit e423738
Show file tree
Hide file tree
Showing 9 changed files with 816 additions and 10 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Source/BLE.Client/BLE.Client.WinConsole/BleAddressSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BLE.Client.WinConsole
{
public static class BleAddressSelector
{
static string bleaddressTxtPath = "bleaddress.txt";
static string bleaddressTxtPath = Path.Combine(Path.GetTempPath(), "bleaddress.txt");
static string? bleaddress = null;

public static bool DoesBleAddressExists()
Expand All @@ -23,7 +23,7 @@ public static bool DoesBleAddressExists()
}

public static string GetBleAddress()
{
{
if (bleaddress is null)
{
if (File.Exists(bleaddressTxtPath))
Expand Down Expand Up @@ -55,8 +55,8 @@ public static void SetBleAddress(string? bleaddressIn)

public static Task NewBleAddress()
{
Console.Write("Enter BLE Address (12 hex chars): ");
SetBleAddress(Console.ReadLine());
Console.Write("Enter BLE Address (12 hex chars): ");
SetBleAddress(Console.ReadLine());
return Task.CompletedTask;
}
}
Expand Down
20 changes: 20 additions & 0 deletions Source/BLE.Client/BLE.Client.WinConsole/PluginDemos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ private void Write(string format, params object[] args)
writer?.Invoke(format, args);
}

public async Task TurnBluetoothOn()
{
await bluetoothLE.TrySetStateAsync(true);
}

public async Task TurnBluetoothOff()
{
await bluetoothLE.TrySetStateAsync(false);
}

public IDevice ConnectToKnown(Guid id)
{
IDevice dev = Adapter.ConnectToKnownDeviceAsync(id).Result;
Expand Down Expand Up @@ -338,6 +348,11 @@ public async Task DoTheScanning(ScanMode scanMode = ScanMode.LowPower, int time_

internal async Task DiscoverAndSelect()
{
if (!bluetoothLE.IsOn)
{
Console.WriteLine("Bluetooth is off - cannot discover");
return;
}
await DoTheScanning();
int index = 1;
await Task.Delay(200);
Expand All @@ -346,6 +361,11 @@ internal async Task DiscoverAndSelect()
{
Console.WriteLine($"{index++}: {dev.Id.ToHexBleAddress()} with Name = {dev.Name}");
}
if (discoveredDevices.Count == 0)
{
Console.Write("NO BLE Devices discovered");
return;
}
Console.WriteLine();
Console.Write($"Select BLE address index with value {1} to {discoveredDevices.Count}: ");
if (int.TryParse(Console.ReadLine(), out int selectedIndex))
Expand Down
6 changes: 5 additions & 1 deletion Source/BLE.Client/BLE.Client.WinConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Windows.Media.Capture;

Console.WriteLine("Hello, BLE World!");
Console.WriteLine($"BleImplementation.OsRuntimeBuildNumber: {BleImplementation.OsRuntimeBuildNumber}");
using (var ct = new ConsoleTracer())
{

Expand All @@ -16,6 +17,8 @@
var demoDict = new Dictionary<ConsoleKey, Demo>
{

{ConsoleKey.B, new Demo("Turn Bluetooth ON", ppemos.TurnBluetoothOn) },
{ConsoleKey.N, new Demo("Turn Bluetooth OFF", ppemos.TurnBluetoothOff) },
{ConsoleKey.D1, new Demo("Discover and set the BleAddress", ppemos.DiscoverAndSelect) },
{ConsoleKey.D2, new Demo("Set the BleAddress", BleAddressSelector.NewBleAddress) },
{ConsoleKey.D3, new Demo("Connect -> Disconnect", ppemos.Connect_Disconnect) },
Expand Down Expand Up @@ -62,6 +65,7 @@
{
Console.WriteLine();
Console.WriteLine($"Running: {chosendemo.Description}");
Console.WriteLine("-------------------------------------------------------");
if (chosendemo is null)
{
throw new Exception("No such demo!");
Expand All @@ -73,7 +77,7 @@
Console.WriteLine($"{key} -> No such test. Remember {ConsoleKey.Escape} -> Quit!");
}
await Task.Delay(200);
Console.WriteLine("---------------------------------------------");
Console.WriteLine("-------------------------------------------------------");
}
}

Expand Down
31 changes: 31 additions & 0 deletions Source/BLE.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BLE.Client.Maui", "BLE.Clie
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BLE.Client.WinConsole", "BLE.Client\BLE.Client.WinConsole\BLE.Client.WinConsole.csproj", "{39287F60-65DE-4077-90F2-57CF725E92B6}"
EndProject
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "BLE.Client.WinConsole.Installer", "BLE.Client\BLE.Client.WinConsole.Installer\BLE.Client.WinConsole.Installer.vdproj", "{14D4A306-E53A-40A5-9427-5CA068B1CB5F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
Expand Down Expand Up @@ -666,6 +668,34 @@ Global
{39287F60-65DE-4077-90F2-57CF725E92B6}.Release|x64.Build.0 = Release|Any CPU
{39287F60-65DE-4077-90F2-57CF725E92B6}.Release|x86.ActiveCfg = Release|Any CPU
{39287F60-65DE-4077-90F2-57CF725E92B6}.Release|x86.Build.0 = Release|Any CPU
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|Any CPU.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|ARM.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|ARM64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|iPhone.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|x64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Ad-Hoc|x86.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|Any CPU.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|ARM.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|ARM64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|iPhone.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|iPhoneSimulator.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|x64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.AppStore|x86.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|Any CPU.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|ARM.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|ARM64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|iPhone.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|iPhoneSimulator.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|x64.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Debug|x86.ActiveCfg = Debug
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|Any CPU.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|ARM.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|ARM64.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|iPhone.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|iPhoneSimulator.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|x64.ActiveCfg = Release
{14D4A306-E53A-40A5-9427-5CA068B1CB5F}.Release|x86.ActiveCfg = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -678,6 +708,7 @@ Global
{25E04E05-F867-4F64-813D-AAFE0BA171B0} = {A06042F5-F69D-4191-875E-5ADE9CF42075}
{D04F2D04-A33F-4E09-9EE2-29D7B2A6CD4E} = {A06042F5-F69D-4191-875E-5ADE9CF42075}
{39287F60-65DE-4077-90F2-57CF725E92B6} = {A06042F5-F69D-4191-875E-5ADE9CF42075}
{14D4A306-E53A-40A5-9427-5CA068B1CB5F} = {A06042F5-F69D-4191-875E-5ADE9CF42075}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B5D5105C-EA52-467B-8CCC-6777900C3B95}
Expand Down
11 changes: 7 additions & 4 deletions Source/Plugin.BLE/Windows/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ private void Device_ConnectionStatusChanged(BluetoothLEDevice nativeDevice, obje
&& ConnectedDeviceRegistry.TryGetValue(id, out var connectedDevice))
{
#if WINDOWS10_0_22000_0_OR_GREATER
var conpar = nativeDevice.GetConnectionParameters();
Trace.Message(
$"Connected with Latency = {conpar.ConnectionLatency}, "
+ $"Interval = {conpar.ConnectionInterval}, Timeout = {conpar.LinkTimeout}");
if (BleImplementation.OsRuntimeBuildNumber >= 22000)
{
var conpar = nativeDevice.GetConnectionParameters();
Trace.Message(
$"Connected with Latency = {conpar.ConnectionLatency}, "
+ $"Interval = {conpar.ConnectionInterval}, Timeout = {conpar.LinkTimeout}");
}
#endif
HandleConnectedDevice(connectedDevice);
return;
Expand Down
20 changes: 20 additions & 0 deletions Source/Plugin.BLE/Windows/BleImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ public class BleImplementation : BleImplementationBase
public static BluetoothCacheMode CacheModeGetCharacteristics { get; set; } = BluetoothCacheMode.Cached;
public static BluetoothCacheMode CacheModeGetServices { get; set; } = BluetoothCacheMode.Cached;

private static int? runtimeBuildNumber = null;

/// <summary>
/// Get the Windows runtime build number
/// Eg running on Windows 10 gives 19041 even though it is executing an application compiled as eg net8.0-windows10.0.22621.0
/// ref https://learn.microsoft.com/en-us/windows/release-health/release-information
/// ref https://learn.microsoft.com/en-us/windows/release-health/windows11-release-information
/// </summary>
public static int OsRuntimeBuildNumber
{
get
{
if (runtimeBuildNumber is null)
{
runtimeBuildNumber = Environment.OSVersion.Version.Build;
}
return runtimeBuildNumber.Value;
}
}

protected override IAdapter CreateNativeAdapter()
{
return new Adapter(btAdapter);
Expand Down
4 changes: 4 additions & 0 deletions Source/Plugin.BLE/Windows/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ protected override bool UpdateConnectionIntervalNative(ConnectionInterval interv
static bool MaybeRequestPreferredConnectionParameters(BluetoothLEDevice device, ConnectParameters connectParameters)
{
#if WINDOWS10_0_22000_0_OR_GREATER
if (BleImplementation.OsRuntimeBuildNumber < 22000)
{
return false;
}
BluetoothLEPreferredConnectionParameters parameters = null;
switch(connectParameters.ConnectionParameterSet)
{
Expand Down

0 comments on commit e423738

Please sign in to comment.