Skip to content

Commit

Permalink
Windows: Check for OS build number version Runtime (dotnet-bluetooth-…
Browse files Browse the repository at this point in the history
…le#853)

* Check for SDK Runtime
* Use Environment.OSVersion.Version.Build directly
  • Loading branch information
AskBojesen authored May 18, 2024
1 parent c894e46 commit 8cc17fc
Show file tree
Hide file tree
Showing 8 changed files with 821 additions and 20 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.2</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
43 changes: 35 additions & 8 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 @@ -88,18 +98,19 @@ public async Task ShowBondState()
dev.Dispose();
}

public async Task Connect_Read_Services_Disconnect_5X()
public async Task Connect_Read_Services_Disconnect_Loop()
{
string bleaddress = BleAddressSelector.GetBleAddress();
var id = bleaddress.ToBleDeviceGuid();
var connectParameters = new ConnectParameters(connectionParameterSet: ConnectionParameterSet.Balanced);

new Task(ConsoleKeyReader).Start();
using (IDevice dev = await Adapter.ConnectToKnownDeviceAsync(id, connectParameters))
{
for (int i = 0; i < 5; i++)
int count = 1;
while(true)
{
await Task.Delay(100);
Write($"---------------- {i} ------------------");
Write($"---------------- {count++} ------- (Esc to stop) ------");
if (dev.State != DeviceState.Connected)
{
Write("Connecting");
Expand All @@ -125,19 +136,25 @@ public async Task Connect_Read_Services_Disconnect_5X()
Write("Disconnecting");
await Adapter.DisconnectDeviceAsync(dev);
Write("Test_Connect_Disconnect done");
if (consoleKey == ConsoleKey.Escape)
{
break;
}
}
}
}

public async Task Connect_Read_Services_Dispose_5X()
public async Task Connect_Read_Services_Dispose_Loop()
{
string bleaddress = BleAddressSelector.GetBleAddress();
var id = bleaddress.ToBleDeviceGuid();
var connectParameters = new ConnectParameters(connectionParameterSet: ConnectionParameterSet.Balanced);
for (int i = 0; i < 5; i++)
new Task(ConsoleKeyReader).Start();
int count = 1;
while (true)
{
await Task.Delay(100);
Write($"---------------- {i} ------------------");
Write($"---------------- {count++} ------- (Esc to stop) ------");
IDevice dev = await Adapter.ConnectToKnownDeviceAsync(id, connectParameters);
Write("Reading services");
var services = await dev.GetServicesAsync();
Expand All @@ -155,7 +172,7 @@ public async Task Connect_Read_Services_Dispose_5X()
charlist.Clear();
Write("Waiting 3 secs");
await Task.Delay(3000);
//await Adapter.DisconnectDeviceAsync(dev);
await Adapter.DisconnectDeviceAsync(dev);
Write("Disposing");
dev.Dispose();
}
Expand Down Expand Up @@ -338,6 +355,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 +368,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
10 changes: 7 additions & 3 deletions 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($"Environment.OSVersion.Version.Build: {Environment.OSVersion.Version.Build}");
using (var ct = new ConsoleTracer())
{

Expand All @@ -16,14 +17,16 @@
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) },
{ConsoleKey.D4, new Demo("Pair -> Connect -> Disconnect", ppemos.Pair_Connect_Disconnect) },
{ConsoleKey.D5, new Demo("Connect -> Change Parameters -> Disconnect", ppemos.Connect_Change_Parameters_Disconnect) },
{ConsoleKey.D6, new Demo("Run GetSystemConnectedOrPairedDevices", ppemos.RunGetSystemConnectedOrPairedDevices) },
{ConsoleKey.D7, new Demo("5X: Connect -> Read services -> Disconnect", ppemos.Connect_Read_Services_Disconnect_5X) },
{ConsoleKey.D8, new Demo("5X: Connect -> Read services -> Dispose", ppemos.Connect_Read_Services_Dispose_5X) },
{ConsoleKey.D7, new Demo("Loop: Connect -> Read services -> Disconnect", ppemos.Connect_Read_Services_Disconnect_Loop) },
{ConsoleKey.D8, new Demo("Loop: Connect -> Read services -> Dispose", ppemos.Connect_Read_Services_Dispose_Loop) },
{ConsoleKey.D9, new Demo("Connect -> Loop: ConnectionLost -> Connect", ppemos.Connect_ConnectionLost_Reconnect) },
{ConsoleKey.Q, new Demo("Adapter.BondAsync", ppemos.BondAsync) },
{ConsoleKey.W, new Demo("Adapter.BondedDevices", ppemos.GetBondedDevices) },
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 (Environment.OSVersion.Version.Build >= 22000)
{
var conpar = nativeDevice.GetConnectionParameters();
Trace.Message(
$"Connected with Latency = {conpar.ConnectionLatency}, "
+ $"Interval = {conpar.ConnectionInterval}, Timeout = {conpar.LinkTimeout}");
}
#endif
HandleConnectedDevice(connectedDevice);
return;
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 (Environment.OSVersion.Version.Build < 22000)
{
return false;
}
BluetoothLEPreferredConnectionParameters parameters = null;
switch(connectParameters.ConnectionParameterSet)
{
Expand Down

0 comments on commit 8cc17fc

Please sign in to comment.