Skip to content

Commit 9a87568

Browse files
committed
xbeeBleDevice: add a constructor with the Bluetooth GUID and password as params
- Modified the XBeeBLEDevice class constructor that requires the MAC address as parameter to allow for GUID too. The constructor verifies if the parameter is either a MAC address or a GUID. If not, it raises an exception. Signed-off-by: Diego Escalona <diego.escalona@digi.com>
1 parent ab253c1 commit 9a87568

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

XBeeLibrary.Xamarin/Connection/Bluetooth/BluetoothInterface.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Digi International Inc.
2+
* Copyright 2019, 2020, Digi International Inc.
33
*
44
* Permission to use, copy, modify, and/or distribute this software for any
55
* purpose with or without fee is hereby granted, provided that the above
@@ -49,7 +49,8 @@ public class BluetoothInterface : IConnectionInterface
4949

5050
private static readonly int REQUESTED_MTU = 256;
5151

52-
private static readonly string ERROR_INVALID_MAC = "Invalid MAC address, it has to follow the format 00112233AABB or 00:11:22:33:AA:BB";
52+
private static readonly string ERROR_INVALID_MAC_GUID = "Invalid MAC address or GUID, it has to follow the format 00112233AABB or " +
53+
"00:11:22:33:AA:BB for the MAC address or 01234567-0123-0123-0123-0123456789AB for the GUID";
5354
private static readonly string ERROR_CONNECTION = "Could not connect to the XBee BLE device";
5455
private static readonly string ERROR_CONNECTION_CANCELED = ERROR_CONNECTION + " > Connection canceled";
5556
private static readonly string ERROR_DISCONNECTION = "Could not disconnect the XBee BLE device";
@@ -62,6 +63,7 @@ public class BluetoothInterface : IConnectionInterface
6263
private static readonly string RX_CHAR_GUID = "F9279EE9-2CD0-410C-81CC-ADF11E4E5AEA";
6364

6465
private static readonly string MAC_REGEX = "^([0-9A-Fa-f]{12})|([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$";
66+
private static readonly string GUID_REGEX = "^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$";
6567
private static readonly string MAC_GUID = "00000000-0000-0000-0000-{0}";
6668

6769
// Variables.
@@ -110,16 +112,32 @@ public BluetoothInterface(IDevice device) : this()
110112
/// Class constructor. Instantiates a new <see cref="BluetoothInterface"/> object with the given
111113
/// Bluetooth device GUID.
112114
/// </summary>
113-
/// <param name="deviceAddress">The address of the Bluetooth device. It must follow the
114-
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</param>
115+
/// <param name="deviceAddress">The address or GUID of the Bluetooth device. It must follow the
116+
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> for the address or
117+
/// <c>01234567-0123-0123-0123-0123456789AB</c> for the GUID.</param>
115118
/// <exception cref="ArgumentException">If <paramref name="deviceAddress"/> does not follow
116-
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</exception>
119+
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> or
120+
/// <c>01234567-0123-0123-0123-0123456789AB</c>.</exception>
117121
public BluetoothInterface(string deviceAddress) : this()
118122
{
119-
if (!Regex.IsMatch(deviceAddress, MAC_REGEX))
120-
throw new ArgumentException(ERROR_INVALID_MAC);
123+
if (!Regex.IsMatch(deviceAddress, MAC_REGEX) && !Regex.IsMatch(deviceAddress, GUID_REGEX))
124+
throw new ArgumentException(ERROR_INVALID_MAC_GUID);
121125

122-
deviceGuid = Guid.Parse(string.Format(MAC_GUID, deviceAddress.Replace(":", "")));
126+
if (Regex.IsMatch(deviceAddress, MAC_REGEX))
127+
deviceGuid = Guid.Parse(string.Format(MAC_GUID, deviceAddress.Replace(":", "")));
128+
else
129+
deviceGuid = new Guid(deviceAddress);
130+
}
131+
132+
/// <summary>
133+
/// Class constructor. Instantiates a new <see cref="BluetoothInterface"/> object with the given
134+
/// Bluetooth device GUID.
135+
/// </summary>
136+
/// <param name="deviceGuid">The Bluetooth device GUID.</param>
137+
/// <seealso cref="Guid"/>
138+
public BluetoothInterface(Guid deviceGuid) : this()
139+
{
140+
this.deviceGuid = deviceGuid;
123141
}
124142

125143
// Properties.

XBeeLibrary.Xamarin/XBee.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Digi International Inc.
2+
* Copyright 2019, 2020, Digi International Inc.
33
*
44
* Permission to use, copy, modify, and/or distribute this software for any
55
* purpose with or without fee is hereby granted, provided that the above
@@ -42,16 +42,30 @@ public static IConnectionInterface CreateConnectionInterface(IDevice device)
4242
/// <summary>
4343
/// Retrieves a bluetooth connection interface for the device with the provided GUID.
4444
/// </summary>
45-
/// <param name="deviceAddress">The address of the Bluetooth device. It must follow the
46-
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</param>
45+
/// <param name="deviceAddress">The address or GUID of the Bluetooth device. It must follow the
46+
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> for the address or
47+
/// <c>01234567-0123-0123-0123-0123456789AB</c> for the GUID.</param>
4748
/// <returns>The connection interface of the Bluetooth device.</returns>
4849
/// <exception cref="ArgumentException">If <paramref name="deviceAddress"/> does not follow
49-
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</exception>
50+
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> or
51+
/// <c>01234567-0123-0123-0123-0123456789AB</c>.</exception>
5052
/// <seealso cref="IConnectionInterface"/>
5153
public static IConnectionInterface CreateConnectionInterface(string deviceAddress)
5254
{
5355
IConnectionInterface connectionInterface = new BluetoothInterface(deviceAddress);
5456
return connectionInterface;
5557
}
58+
59+
/// <summary>
60+
/// Retrieves a bluetooth connection interface for the device with the provided GUID.
61+
/// </summary>
62+
/// <param name="deviceGuid">The Bluetooth device GUID.</param>
63+
/// <seealso cref="Guid"/>
64+
/// <seealso cref="IConnectionInterface"/>
65+
public static IConnectionInterface CreateConnectionInterface(Guid deviceGuid)
66+
{
67+
IConnectionInterface connectionInterface = new BluetoothInterface(deviceGuid);
68+
return connectionInterface;
69+
}
5670
}
5771
}

XBeeLibrary.Xamarin/XBeeBLEDevice.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,36 @@ public XBeeBLEDevice(IDevice device, string password)
6060
/// The Bluetooth password must be provided before calling the <see cref="Open"/> method,
6161
/// either through this constructor or the <see cref="SetBluetoothPassword(string)"/> method.
6262
/// </remarks>
63-
/// <param name="deviceAddress">The address of the Bluetooth device. It must follow the
64-
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</param>
63+
/// <param name="deviceAddress">The address or GUID of the Bluetooth device. It must follow the
64+
/// format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> for the address or
65+
/// <c>01234567-0123-0123-0123-0123456789AB</c> for the GUID.</param>
6566
/// <param name="password">Bluetooth password (can be <c>null</c>).</param>
6667
/// <exception cref="ArgumentException">If <paramref name="deviceAddress"/> does not follow
67-
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c>.</exception>
68+
/// the format <c>00112233AABB</c> or <c>00:11:22:33:AA:BB</c> or
69+
/// <c>01234567-0123-0123-0123-0123456789AB</c>.</exception>
6870
public XBeeBLEDevice(string deviceAddress, string password)
6971
: base(XBee.CreateConnectionInterface(deviceAddress))
7072
{
7173
bluetoothPassword = password;
7274
}
7375

76+
/// <summary>
77+
/// Class constructor. Instantiates a new <see cref="XBeeBLEDevice"/> object with the given
78+
/// parameters.
79+
/// </summary>
80+
/// <remarks>
81+
/// The Bluetooth password must be provided before calling the <see cref="Open"/> method,
82+
/// either through this constructor or the <see cref="SetBluetoothPassword(string)"/> method.
83+
/// </remarks>
84+
/// <param name="deviceGuid">The Bluetooth device GUID.</param>
85+
/// <param name="password">Bluetooth password (can be <c>null</c>).</param>
86+
/// <seealso cref="Guid"/>
87+
public XBeeBLEDevice(Guid deviceGuid, string password)
88+
: base(XBee.CreateConnectionInterface(deviceGuid))
89+
{
90+
bluetoothPassword = password;
91+
}
92+
7493
// Events.
7594
/// <summary>
7695
/// Represents the method that will handle the Data received event.

0 commit comments

Comments
 (0)