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.
0 commit comments