Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit cd9ea01

Browse files
committed
Bump v.0.24.0
1 parent 1d90d5c commit cd9ea01

File tree

3 files changed

+108
-52
lines changed

3 files changed

+108
-52
lines changed

src/Unosquare.RaspberryIO.Abstractions/BluetoothErrorException.cs src/Unosquare.RaspberryIO/BluetoothErrorException.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace Unosquare.RaspberryIO.Abstractions
1+
namespace Unosquare.RaspberryIO
22
{
33
using System;
44

55
/// <inheritdoc />
66
/// <summary>
7-
/// Special Exception to transport the BluetoothErorItem.
7+
/// Occurs when an exception is thrown in the <c>Bluetooth</c> component.
88
/// </summary>
99
public class BluetoothErrorException : Exception
1010
{
+105-49
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
namespace Unosquare.RaspberryIO.Computer
22
{
3+
using Swan.Abstractions;
4+
using Swan.Components;
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
68
using System.Threading;
79
using System.Threading.Tasks;
8-
using Unosquare.RaspberryIO.Abstractions;
9-
using Unosquare.Swan.Abstractions;
10-
using Unosquare.Swan.Components;
1110

1211
/// <summary>
13-
/// Represents the bluetooth information.
12+
/// Represents the Bluetooth information.
1413
/// </summary>
1514
public class Bluetooth : SingletonBase<Bluetooth>
1615
{
17-
18-
private const string BC = "bluetoothctl";
16+
private const string BcCommand = "bluetoothctl";
1917

2018
/// <summary>
21-
/// Turns on the bluetooth adapter.
19+
/// Turns on the Bluetooth adapter.
2220
/// </summary>
23-
/// <returns>Returns true or false depending if the controller was turned on.</returns>
24-
public async Task<bool> PowerOn()
21+
/// <param name="cancellationToken">The cancellation token.</param>
22+
/// <returns>
23+
/// Returns true or false depending if the controller was turned on.
24+
/// </returns>
25+
/// <exception cref="BluetoothErrorException">Failed to power on:.</exception>
26+
public async Task<bool> PowerOn(CancellationToken cancellationToken = default)
2527
{
2628
try
2729
{
28-
var output = await ProcessRunner.GetProcessOutputAsync(BC, "power on").ConfigureAwait(false);
30+
var output = await ProcessRunner.GetProcessOutputAsync(BcCommand, "power on", cancellationToken).ConfigureAwait(false);
2931
return output.Contains("succeeded");
3032
}
3133
catch (Exception ex)
@@ -37,12 +39,16 @@ public async Task<bool> PowerOn()
3739
/// <summary>
3840
/// Turns off the bluetooth adapter.
3941
/// </summary>
40-
/// <returns>Returns true or false depending if the controller was turned off.</returns>
41-
public async Task<bool> PowerOff()
42+
/// <param name="cancellationToken">The cancellation token.</param>
43+
/// <returns>
44+
/// Returns true or false depending if the controller was turned off.
45+
/// </returns>
46+
/// <exception cref="BluetoothErrorException">Failed to power off:.</exception>
47+
public async Task<bool> PowerOff(CancellationToken cancellationToken = default)
4248
{
4349
try
4450
{
45-
var output = await ProcessRunner.GetProcessOutputAsync(BC, "power off").ConfigureAwait(false);
51+
var output = await ProcessRunner.GetProcessOutputAsync(BcCommand, "power off", cancellationToken).ConfigureAwait(false);
4652
return output.Contains("succeeded");
4753
}
4854
catch (Exception ex)
@@ -54,16 +60,20 @@ public async Task<bool> PowerOff()
5460
/// <summary>
5561
/// Gets the list of detected devices.
5662
/// </summary>
57-
/// <returns> Returns the list of detected devices. </returns>
58-
public async Task<IEnumerable<string>> ListDevices()
63+
/// <param name="cancellationToken">The cancellation token.</param>
64+
/// <returns>
65+
/// Returns the list of detected devices.
66+
/// </returns>
67+
/// <exception cref="BluetoothErrorException">Failed to retrieve devices:.</exception>
68+
public async Task<IEnumerable<string>> ListDevices(CancellationToken cancellationToken = default)
5969
{
6070
try
6171
{
6272
using (var cancellationTokenSource = new CancellationTokenSource(3000))
6373
{
64-
ProcessRunner.GetProcessOutputAsync(BC, "scan on", cancellationTokenSource.Token);
65-
await ProcessRunner.GetProcessOutputAsync(BC, "scan off").ConfigureAwait(false);
66-
var devices = await ProcessRunner.GetProcessOutputAsync(BC, "devices").ConfigureAwait(false);
74+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "scan on", cancellationTokenSource.Token).ConfigureAwait(false);
75+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "scan off", cancellationToken).ConfigureAwait(false);
76+
var devices = await ProcessRunner.GetProcessOutputAsync(BcCommand, "devices", cancellationToken).ConfigureAwait(false);
6777
return devices.Trim().Split('\n').Select(x => x.Trim());
6878
}
6979
}
@@ -76,12 +86,16 @@ public async Task<IEnumerable<string>> ListDevices()
7686
/// <summary>
7787
/// Gets the list of bluetooth controllers.
7888
/// </summary>
79-
/// <returns> Returns the list of bluetooth controllers. </returns>
80-
public async Task<IEnumerable<string>> ListControllers()
89+
/// <param name="cancellationToken">The cancellation token.</param>
90+
/// <returns>
91+
/// Returns the list of bluetooth controllers.
92+
/// </returns>
93+
/// <exception cref="BluetoothErrorException">Failed to retrieve controllers:.</exception>
94+
public async Task<IEnumerable<string>> ListControllers(CancellationToken cancellationToken = default)
8195
{
8296
try
8397
{
84-
var controllers = await ProcessRunner.GetProcessOutputAsync(BC, "list").ConfigureAwait(false);
98+
var controllers = await ProcessRunner.GetProcessOutputAsync(BcCommand, "list", cancellationToken).ConfigureAwait(false);
8599
return controllers.Trim().Split('\n').Select(x => x.Trim());
86100
}
87101
catch (Exception ex)
@@ -95,17 +109,30 @@ public async Task<IEnumerable<string>> ListControllers()
95109
/// </summary>
96110
/// <param name="controllerAddress">The mac address of the controller that will be used to pair.</param>
97111
/// <param name="deviceAddress">The mac address of the device that will be paired.</param>
98-
/// <returns> Returns true or false if the pair was succesfully.</returns>
99-
public async Task<bool> Pair(string controllerAddress, string deviceAddress)
112+
/// <param name="cancellationToken">The cancellation token.</param>
113+
/// <returns>
114+
/// Returns true or false if the pair was successfully.
115+
/// </returns>
116+
/// <exception cref="BluetoothErrorException">Failed to Pair:.</exception>
117+
public async Task<bool> Pair(string controllerAddress, string deviceAddress, CancellationToken cancellationToken = default)
100118
{
101119
try
102120
{
103-
await ProcessRunner.GetProcessOutputAsync(BC, $"select {controllerAddress}").ConfigureAwait(false); // Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
104-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable on").ConfigureAwait(false); // Makes the controller visible to other devices.
105-
await ProcessRunner.GetProcessOutputAsync(BC, "pairable on").ConfigureAwait(false); // Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
121+
// Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
122+
await ProcessRunner.GetProcessOutputAsync(BcCommand, $"select {controllerAddress}", cancellationToken)
123+
.ConfigureAwait(false);
124+
125+
// Makes the controller visible to other devices.
126+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable on", cancellationToken).ConfigureAwait(false);
127+
128+
// Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
129+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "pairable on", cancellationToken).ConfigureAwait(false);
130+
131+
// Pairs the device with the controller.
132+
var result = await ProcessRunner.GetProcessOutputAsync(BcCommand, $"pair {deviceAddress}", cancellationToken).ConfigureAwait(false);
106133

107-
var result = await ProcessRunner.GetProcessOutputAsync(BC, $"pair {deviceAddress}").ConfigureAwait(false); // Pairs the device with the controller.
108-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable off").ConfigureAwait(false); // Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
134+
// Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
135+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable off", cancellationToken).ConfigureAwait(false);
109136

110137
return result.Contains("Paired: yes");
111138
}
@@ -118,20 +145,31 @@ public async Task<bool> Pair(string controllerAddress, string deviceAddress)
118145
/// <summary>
119146
/// Performs a connection of a given controller with a given device.
120147
/// </summary>
121-
/// <param name="controllerAddress">The mac address of the controller that will be used to make the connection. </param>
122-
/// <param name="deviceAddress">The mac address of the device that will be connected. </param>
123-
/// <returns> Returns true or false if the connection was successfully. </returns>
124-
public async Task<bool> Connect(string controllerAddress, string deviceAddress)
148+
/// <param name="controllerAddress">The mac address of the controller that will be used to make the connection.</param>
149+
/// <param name="deviceAddress">The mac address of the device that will be connected.</param>
150+
/// <param name="cancellationToken">The cancellation token.</param>
151+
/// <returns>
152+
/// Returns true or false if the connection was successfully.
153+
/// </returns>
154+
/// <exception cref="BluetoothErrorException">Failed to connect:.</exception>
155+
public async Task<bool> Connect(string controllerAddress, string deviceAddress, CancellationToken cancellationToken = default)
125156
{
126157
try
127158
{
128-
await ProcessRunner.GetProcessOutputAsync(BC, $"select {controllerAddress}").ConfigureAwait(false); // Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
129-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable on").ConfigureAwait(false); // Makes the controller visible to other devices.
130-
await ProcessRunner.GetProcessOutputAsync(BC, "pairable on").ConfigureAwait(false); // Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
159+
// Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
160+
await ProcessRunner.GetProcessOutputAsync(BcCommand, $"select {controllerAddress}", cancellationToken).ConfigureAwait(false);
131161

132-
var result = await ProcessRunner.GetProcessOutputAsync(BC, $"connect {deviceAddress}").ConfigureAwait(false); // Readies the device for pairing.
162+
// Makes the controller visible to other devices.
163+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable on", cancellationToken).ConfigureAwait(false);
133164

134-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable off").ConfigureAwait(false); // Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
165+
// Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
166+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "pairable on", cancellationToken).ConfigureAwait(false);
167+
168+
// Readies the device for pairing.
169+
var result = await ProcessRunner.GetProcessOutputAsync(BcCommand, $"connect {deviceAddress}", cancellationToken).ConfigureAwait(false);
170+
171+
// Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
172+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable off", cancellationToken).ConfigureAwait(false);
135173

136174
return result.Contains("Connected: yes");
137175
}
@@ -146,19 +184,29 @@ public async Task<bool> Connect(string controllerAddress, string deviceAddress)
146184
/// </summary>
147185
/// <param name="controllerAddress">The mac address of the controller will be used.</param>
148186
/// <param name="deviceAddress">The mac address of the device will be added to the trust list devices.</param>
149-
/// <returns>Returns true or false if the operation was successful.</returns>
150-
public async Task<bool> Trust(string controllerAddress, string deviceAddress)
187+
/// <param name="cancellationToken">The cancellation token.</param>
188+
/// <returns>
189+
/// Returns true or false if the operation was successful.
190+
/// </returns>
191+
/// <exception cref="BluetoothErrorException">Failed to add to trust devices list:.</exception>
192+
public async Task<bool> Trust(string controllerAddress, string deviceAddress, CancellationToken cancellationToken = default)
151193
{
152194
try
153195
{
196+
// Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
197+
await ProcessRunner.GetProcessOutputAsync(BcCommand, $"select {controllerAddress}", cancellationToken).ConfigureAwait(false);
154198

155-
await ProcessRunner.GetProcessOutputAsync(BC, $"select {controllerAddress}").ConfigureAwait(false); // Selects the controller to pair. Once you select the controller, all controller-related commands will apply to it for three minutes.
156-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable on").ConfigureAwait(false); // Makes the controller visible to other devices.
157-
await ProcessRunner.GetProcessOutputAsync(BC, "pairable on").ConfigureAwait(false); // Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
199+
// Makes the controller visible to other devices.
200+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable on", cancellationToken).ConfigureAwait(false);
158201

159-
var result = await ProcessRunner.GetProcessOutputAsync(BC, $"trust {deviceAddress}").ConfigureAwait(false); // Sets the device to re-pair automatically when it is turned on, which eliminates the need to pair all over again.
202+
// Readies the controller for pairing. Remember that you have three minutes after running this command to pair.
203+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "pairable on", cancellationToken).ConfigureAwait(false);
160204

161-
await ProcessRunner.GetProcessOutputAsync(BC, "discoverable off").ConfigureAwait(false); // Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
205+
// Sets the device to re-pair automatically when it is turned on, which eliminates the need to pair all over again.
206+
var result = await ProcessRunner.GetProcessOutputAsync(BcCommand, $"trust {deviceAddress}", cancellationToken).ConfigureAwait(false);
207+
208+
// Hides the controller from other Bluetooth devices. Otherwise, any device that can detect it has access to it, leaving a major security hole.
209+
await ProcessRunner.GetProcessOutputAsync(BcCommand, "discoverable off", cancellationToken).ConfigureAwait(false);
162210

163211
return result.Contains("Trusted: yes");
164212
}
@@ -172,11 +220,19 @@ public async Task<bool> Trust(string controllerAddress, string deviceAddress)
172220
/// Displays information about a particular device.
173221
/// </summary>
174222
/// <param name="deviceAddress">The mac address of the device which info will be retrieved.</param>
175-
/// <returns> Returns the device info.</returns>
176-
public async Task<string> DeviceInfo(string deviceAddress)
223+
/// <param name="cancellationToken">The cancellation token.</param>
224+
/// <returns>
225+
/// Returns the device info.
226+
/// </returns>
227+
/// <exception cref="BluetoothErrorException">Failed to retrieve info for {deviceAddress}.</exception>
228+
public async Task<string> DeviceInfo(string deviceAddress, CancellationToken cancellationToken = default)
177229
{
178-
var info = await ProcessRunner.GetProcessOutputAsync(BC, $"info {deviceAddress}").ConfigureAwait(false);
179-
return !string.IsNullOrEmpty(info) ? info : throw new BluetoothErrorException($"Failed to retrieve info for {deviceAddress}");
230+
var info = await ProcessRunner.GetProcessOutputAsync(BcCommand, $"info {deviceAddress}", cancellationToken)
231+
.ConfigureAwait(false);
232+
233+
return !string.IsNullOrEmpty(info)
234+
? info
235+
: throw new BluetoothErrorException($"Failed to retrieve info for {deviceAddress}");
180236
}
181237
}
182-
}
238+
}

src/Unosquare.RaspberryIO/Unosquare.RaspberryIO.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This library enables developers to use the various Raspberry Pi's hardware modul
1111
<AssemblyName>Unosquare.RaspberryIO</AssemblyName>
1212
<PackageId>Unosquare.Raspberry.IO</PackageId>
1313
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
14-
<Version>0.23.0</Version>
14+
<Version>0.24.0</Version>
1515
<Authors>Unosquare</Authors>
1616
<PackageIconUrl>https://github.com/unosquare/raspberryio/raw/master/logos/raspberryio-logo-32.png</PackageIconUrl>
1717
<PackageProjectUrl>https://github.com/unosquare/raspberryio</PackageProjectUrl>

0 commit comments

Comments
 (0)