Skip to content

Commit 0fdff64

Browse files
committed
Add a script example in the Readme
1 parent e9bf3c0 commit 0fdff64

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
Console utility to discover and pair Bluetooth and Bluetooth Low Energy devices.
33

44
# 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.
5+
If you pair a device that is not already paired, the utility will also connect to it (this is the default behavior of Windows Bluetooth API)<br>
6+
However, the pairing will fail if a device is paired but not connected.<br>
7+
Unfortunately, it is impossible to simulate what the "Connect" button from Windows `Bluetooth and Other devices` dialog does.
88
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>
99

1010
# System requirements
1111
Windows 10 1809 (10.0.17763) or higher
1212

1313
# How to use
1414
* Download and unpack the latest [release](https://github.com/PolarGoose/BluetoothDevicePairing/releases).
15-
* Run `BluetoothDevicePairing.exe --help` and `BluetoothDevicePairing.exe <command> --help` to get usage information and check the `Examples` section bellow.
15+
* Run `BluetoothDevicePairing.exe --help` and `BluetoothDevicePairing.exe <command> --help` to get usage information and check the `Examples` section below.
1616

1717
# Examples
1818
* Discover devices:
1919
```
2020
BluetoothDevicePairing.exe discover
2121
```
22-
* Pair a device using its mac address:
22+
* Pair a device using its Mac address:
2323
```
2424
BluetoothDevicePairing.exe pair-by-mac --mac 12:34:56:78:9A:BC --type Bluetooth
2525
```
@@ -31,37 +31,59 @@ BluetoothDevicePairing.exe pair-by-name --name "MX Ergo" --type BluetoothLE
3131
```
3232
BluetoothDevicePairing.exe pair-by-name --name "Device name" --type BluetoothLE --pin 1234
3333
```
34-
* Pair a device using its mac and pin code:
34+
* Pair a device using its Mac and pin code:
3535
```
3636
BluetoothDevicePairing.exe pair-by-mac --mac 12:34:56:78:9A:BC --type Bluetooth --pin 1234
3737
```
38-
* Unpair a device using its mac address:
38+
* Unpair a device using its Mac address:
3939
```
4040
BluetoothDevicePairing.exe unpair-by-mac --mac 12:34:56:78:9A:BC --type Bluetooth
4141
```
4242
* Unpair a device using its name:
4343
```
4444
BluetoothDevicePairing.exe unpair-by-name --name "MX Ergo" --type BluetoothLE
4545
```
46-
* List all Bluetooth adapters available you your machine
46+
* List all Bluetooth adapters available to your machine
4747
```
4848
BluetoothDevicePairing.exe list-adapters
4949
```
5050

51+
# Examples of scripts
52+
The BluetoothDevicePairing utility can be used in bat and PowerShell scripts.
53+
54+
## Script to pair a Bluetooth device
55+
The following `bat` script allows to automate the connection of Bluetooth devices.
56+
Before using this script, you need to find out the Bluetooth type and name of your device:
57+
1. Put the device into the pairing mode
58+
2. Run the `BluetoothDevicePairing.exe discover` command that will print the required information.
59+
3. Adapt the script below to use your device's name and Bluetooth type.
60+
61+
How to use the script:
62+
1. Put the device into a pairing mode
63+
2. Run the script
64+
```
65+
C:\my\apps\BluetoothDevicePairing.exe unpair-by-name --name "MX Ergo" --type BluetoothLE --discovery-time 1
66+
C:\my\apps\BluetoothDevicePairing.exe pair-by-name --name "MX Ergo" --type BluetoothLE --discovery-time 10
67+
if %ErrorLevel% NEQ 0 (
68+
pause
69+
exit /b %errorlevel%
70+
)
71+
```
72+
5173
# How it works
5274
The program uses
5375
* [Windows.Devices.Enumeration API](https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration?redirectedfrom=MSDN&view=winrt-22000) to work with Bluetooth.
5476
* [Costura Fody](https://github.com/Fody/Costura) to create a single file executable.
5577

5678
### Device pairing by 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.
79+
To pair a device by name, the utility starts by 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.
5880

5981
# Return values
60-
In case of failure the command returns value `-1`. In case of success the `0` is returned.
82+
If the command fails, it returns the value `-1`. If it succeeds, it returns `0`.
6183

6284
# Build
6385
* Use `Visual Studio 2022` to open the solution file and work with the code
64-
* Run `.github/workflows/build.ps1` to build a release (to run this script `git.exe` should be in your PATH)
86+
* Run `.github/workflows/build.ps1` to build a release (to run this script, `git.exe` should be in your PATH)
6587

6688
# References
6789
* [Windows.Devices.Enumeration API usage examples](https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing)

0 commit comments

Comments
 (0)