Skip to content

Commit cba8a5c

Browse files
committed
* Reduce the size of the published executable by using .Net Framework which is present by default on all supported versions of Windows 10 and higher.
* Use Costura Fody to make a single executable. * Improve the Readme file.
1 parent 9b8473a commit cba8a5c

File tree

11 files changed

+68
-49
lines changed

11 files changed

+68
-49
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ charset = utf-8
88
indent_style = space
99
indent_size = 4
1010

11-
[*.{xml,wxs,csproj,yaml,props}]
11+
[*.{xml,wxs,csproj,yaml,props,config}]
1212
indent_size = 2

.github/workflows/build.ps1

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Function CheckReturnCodeOfPreviousCommand($msg) {
1414
}
1515
}
1616

17-
Function CreateZipArchive($dir) {
18-
Info "Create zip archive `n ${dir}.zip"
19-
Compress-Archive -Force -Path "$dir/*" -DestinationPath "${dir}.zip"
17+
Function CreateZipArchive($file, $archiveFile) {
18+
Info "Archive `n '$file' `n to `n '$archiveFile'"
19+
Compress-Archive -Force -Path $file -DestinationPath $archiveFile
2020
}
2121

2222
Function GetVersion() {
@@ -34,35 +34,35 @@ Function GetVersion() {
3434
return "$($nearestTag.Substring(1))-$commitHash"
3535
}
3636

37-
Function Publish($slnFile, $version, $outDir) {
38-
Info "Run 'dotnet publish' command: `n slnFile=$slnFile `n version='$version' `n outDir=$outDir"
37+
Function ForceCopy($file, $dstFolder) {
38+
Info "Copy `n '$file' `n to `n '$dstFolder'"
39+
New-Item $publishFolder -Force -ItemType "directory" > $null
40+
Copy-Item $buildResultExecutable -Destination $publishFolder -Force
41+
}
42+
43+
Function Build($slnFile, $version) {
44+
Info "Run 'dotnet build' command: `n slnFile=$slnFile `n version='$version'"
3945

4046
$Env:DOTNET_NOLOGO = "true"
4147
$Env:DOTNET_CLI_TELEMETRY_OPTOUT = "true"
42-
dotnet publish `
43-
--self-contained true `
44-
--runtime win-x86 `
48+
dotnet build `
4549
--configuration Release `
46-
--output $outDir `
47-
/property:PublishSingleFile=true `
48-
/property:IncludeAllContentForSelfExtract=true `
49-
/property:PublishTrimmed=true `
50-
/property:TrimMode=link `
5150
/property:DebugType=None `
5251
/property:Version=$version `
5352
$slnFile
54-
CheckReturnCodeOfPreviousCommand "'dotnet publish' command failed"
55-
56-
CreateZipArchive $outDir
53+
CheckReturnCodeOfPreviousCommand "'dotnet build' command failed"
5754
}
5855

5956
Set-StrictMode -Version Latest
6057
$ErrorActionPreference = "Stop"
6158

6259
$root = Resolve-Path "$PSScriptRoot/../.."
60+
$buildRoot = "$root/build"
61+
$buildResultsFolder = "$buildRoot/Release/net472"
6362
$projectName = "BluetoothDevicePairing"
63+
$buildResultExecutable = "$buildResultsFolder/$projectName.exe"
64+
$publishFolder = "$buildRoot/Publish"
6465

65-
Publish `
66-
-slnFile $root/$projectName.sln `
67-
-version (GetVersion) `
68-
-outDir $root/build/Publish/$projectName
66+
Build -slnFile $root/$projectName.sln -version (GetVersion)
67+
ForceCopy $buildResultExecutable $publishFolder
68+
CreateZipArchive "$publishFolder/${projectName}.exe" "$publishFolder/${projectName}.zip"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/build/
22
.vs/
3+
/src/FodyWeavers.xsd

BluetoothDevicePairing.sln

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio Version 17
43
VisualStudioVersion = 17.0.32014.148
@@ -17,14 +16,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Files", "Files", "{F4EA3749
1716
EndProject
1817
Global
1918
GlobalSection(SolutionConfigurationPlatforms) = preSolution
20-
Debug|x86 = Debug|x86
21-
Release|x86 = Release|x86
19+
Debug|Any CPU = Debug|Any CPU
20+
Release|Any CPU = Release|Any CPU
2221
EndGlobalSection
2322
GlobalSection(ProjectConfigurationPlatforms) = postSolution
24-
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Debug|x86.ActiveCfg = Debug|x86
25-
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Debug|x86.Build.0 = Debug|x86
26-
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Release|x86.ActiveCfg = Release|x86
27-
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Release|x86.Build.0 = Release|x86
23+
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{728939B3-82EA-4471-9CC7-3403EAD1E537}.Release|Any CPU.Build.0 = Release|Any CPU
2827
EndGlobalSection
2928
GlobalSection(SolutionProperties) = preSolution
3029
HideSolutionNode = FALSE

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# BluetoothDevicePairing
2-
Console utility to discover and pair or connect to Bluetooth and Bluetooth Low Energy devices.
2+
Console utility to discover and pair Bluetooth and Bluetooth Low Energy devices.
3+
4+
# Note on connecting to Bluetooth devices
5+
If you pair a device which is not already paired, the utility will also connect to it (this is default behaviour of Windows Bluetooth API)<br>
6+
However, if device is paired but not connected, the pairing will fail.<br>
7+
Unfortunately, currently it is not possible to simulate what "Connect" button from Windows `Bluetooth and Other devices` dialog does.
8+
More details can be found here: [How to connect to a paired audio Bluetooth device](https://stackoverflow.com/questions/62502414/how-to-connect-to-a-paired-audio-bluetooth-device-using-windows-uwp-api). Specifically, [here](https://github.com/inthehand/32feet/issues/132#issuecomment-1019786324) I have described my failed attempts to implement this functionality.<br>
39

410
# System requirements
511
Windows 10 1809 (10.0.17763) or higher
@@ -13,19 +19,19 @@ Windows 10 1809 (10.0.17763) or higher
1319
```
1420
BluetoothDevicePairing.exe discover
1521
```
16-
* Pair and connect to a device using its mac address:
22+
* Pair a device using its mac address:
1723
```
1824
BluetoothDevicePairing.exe pair-by-mac --mac 12:34:56:78:9A:BC --type Bluetooth
1925
```
20-
* Pair and connect to a device using its name:
26+
* Pair a device using its name:
2127
```
2228
BluetoothDevicePairing.exe pair-by-name --name "MX Ergo" --type BluetoothLE
2329
```
24-
* Pair and connect to a device using its name and pin code:
30+
* Pair a device using its name and pin code:
2531
```
2632
BluetoothDevicePairing.exe pair-by-name --name "Device name" --type BluetoothLE --pin 1234
2733
```
28-
* Pair and connect to a device using its mac and pin code:
34+
* Pair a device using its mac and pin code:
2935
```
3036
BluetoothDevicePairing.exe pair-by-mac --mac 12:34:56:78:9A:BC --type Bluetooth --pin 1234
3137
```
@@ -43,13 +49,12 @@ BluetoothDevicePairing.exe list-adapters
4349
```
4450

4551
# How it works
46-
The program uses [Windows.Devices.Enumeration API](https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration?redirectedfrom=MSDN&view=winrt-22000) to work with Bluetooth.
47-
48-
## Device pairing by mac
49-
The utility gets the default bluetooth adapter and generates the bluetooth device id using combination of bluetooth type, adapter mac address and device's mac address. Using this id, it is possible to request to pair or unpair the device.
52+
The program uses
53+
* [Windows.Devices.Enumeration API](https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration?redirectedfrom=MSDN&view=winrt-22000) to work with Bluetooth.
54+
* [Costura Fody](https://github.com/Fody/Costura) to create a single file executable.
5055

5156
## Device pairing by name
52-
The utility discovers all available devices (for unpairing only paired devices are checked) and tries to find a device with the required name. After that pairing or unpairing is requested for found device. The command will fail if there are several devices with the same name.
57+
In order to pair a device by name, the utility starts with discovering all available devices and tries to find a device with the required name. After a device is found its mac address is used to request pairing. The command will fail if there are several devices with the same name.
5358

5459
# Return values
5560
In case of failure the command returns value `-1`. In case of success the `0` is returned.
@@ -60,3 +65,4 @@ In case of failure the command returns value `-1`. In case of success the `0` is
6065

6166
# References
6267
* [Windows.Devices.Enumeration API usage examples](https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing)
68+
* [Windows.Devices.Enumeration Namespace](https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration)

nuget.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositoryPath" value="build/nuget" />
5+
<add key="globalPackagesFolder" value="build/nuget" />
6+
</config>
7+
</configuration>

src/BluetoothDevicePairing.csproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<Platforms>x86</Platforms>
4+
<Platforms>AnyCPU</Platforms>
55
<Nullable>disable</Nullable>
6-
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
7-
<Copyright>Console utility to discover and pair or connect to Bluetooth and Bluetooth Low Energy devices.</Copyright>
6+
<TargetFramework>net472</TargetFramework>
7+
<Copyright>Console utility to discover and pair Bluetooth and Bluetooth Low Energy devices.</Copyright>
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
11+
<PackageReference Include="Costura.Fody" Version="5.7.0">
12+
<PrivateAssets>all</PrivateAssets>
13+
</PackageReference>
14+
<PackageReference Include="Fody" Version="6.6.0">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.17763.1000" />
1119
</ItemGroup>
1220
</Project>

src/Commands/PairDeviceByMac.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace BluetoothDevicePairing.Commands
66
{
77
[Verb("pair-by-mac",
8-
HelpText = "Pair and connect to a device using its mac address")]
8+
HelpText = "Pair a device using its mac address")]
99
internal sealed class PairDeviceByMacOptions : MacAndDeviceTypeOptions
1010
{
1111
[Option("pin",

src/Commands/PairDeviceByName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace BluetoothDevicePairing.Commands
77
{
88
[Verb("pair-by-name",
9-
HelpText = "Pair and connect to a device using its name.")]
9+
HelpText = "Pair a device using its name.")]
1010
internal sealed class PairDeviceByNameOptions : PairAndUnpairDeviceByNameOptions
1111
{
1212
[Option("pin",

src/Directory.Build.props

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
<BuildFolder>$(MSBuildThisFileDirectory)..\build\</BuildFolder>
44
<OutputPath>$(BuildFolder)$(Configuration)\</OutputPath>
55
<BaseIntermediateOutputPath>$(BuildFolder)obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
6+
<LangVersion>latest</LangVersion>
67
<WarningLevel>5</WarningLevel>
78
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
89
<Version>0.0-dev</Version>
9-
10-
<!--
11-
When the build script builds the project with "TrimMode=link" property, Msbuild produces the following warning which is treated as error because "TreatWarningsAsErrors=true":
12-
error IL2104: Assembly '<name>' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
13-
-->
14-
<NoWarn>IL2104</NoWarn>
1510
</PropertyGroup>
1611
</Project>

0 commit comments

Comments
 (0)