[Bluetooth][Non-ACR] Add public constructor for BluetoothDevice (#3065)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothDevice.cs
index 503861e..6f3586f 100644 (file)
@@ -20,14 +20,17 @@ using System.Runtime.InteropServices;
 using System.Collections.Concurrent;
 using System.Collections.ObjectModel;
 using System.Collections.Specialized;
+using System.Reflection;
+using System.ComponentModel;
 
 namespace Tizen.Network.Bluetooth
 {
     /// <summary>
-    /// A class which is used to handle the connection with other devices and set authorization of other devices.<br>
+    /// This class is used to handle the connection with other devices and set authorization of other devices.<br/>
     /// The BluetoothDevice class is used to search for services available on remote devices.
     /// </summary>
     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
+    /// <since_tizen> 3 </since_tizen>
     public class BluetoothDevice
     {
         private event EventHandler<BondCreatedEventArgs> _bondCreated;
@@ -41,6 +44,7 @@ namespace Tizen.Network.Bluetooth
         private Interop.Bluetooth.AuthorizationChangedCallback _authorizationChangedCallback;
         private Interop.Bluetooth.ServiceSearchedCallback _serviceSearchedCallback;
         private Interop.Bluetooth.DeviceConnectionStateChangedCallback _connectionChangedCallback;
+        private Interop.Bluetooth.ConnectedProfileCallback _connectedProfileCallback;
 
         internal string RemoteDeviceAddress;
         internal string RemoteDeviceName;
@@ -60,8 +64,19 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// Address of device.
+        /// The constructor
         /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public BluetoothDevice(BluetoothLeDevice leDevice)
+        {
+            RemoteDeviceAddress = leDevice?.RemoteAddress;
+        }
+
+        /// <summary>
+        /// The address of the device.
+        /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public string Address
         {
             get
@@ -70,8 +85,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// Name of device.
+        /// The name of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public string Name
         {
             get
@@ -80,8 +96,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// Strength indicator of received signal of device.
+        /// The strength indicator of received signal of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public int Rssi
         {
             get
@@ -90,8 +107,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// Class of device.
+        /// The class of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public BluetoothClass Class
         {
             get
@@ -100,8 +118,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// Service UUID list of device.
+        /// The service UUID list of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public IEnumerable<string> ServiceUuidList
         {
             get
@@ -112,6 +131,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The number of services.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public int ServiceCount
         {
             get
@@ -120,8 +140,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// The paired state of device.
+        /// The paired state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsPaired
         {
             get
@@ -130,8 +151,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// The connection state of device.
+        /// The connection state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsConnected
         {
             get
@@ -140,8 +162,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// The authorization state of device.
+        /// The authorization state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsAuthorized
         {
             get
@@ -150,8 +173,9 @@ namespace Tizen.Network.Bluetooth
             }
         }
         /// <summary>
-        /// Bluetooth Appearance.
+        /// The Bluetooth appearance.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public BluetoothAppearanceType AppearanceType
         {
             get
@@ -161,8 +185,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// The length of manufacturer data.
+        /// The length of the manufacturer data.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public int ManufacturerDataLength
         {
             get
@@ -173,6 +198,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The manufacturer data.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public string ManufacturerData
         {
             get
@@ -182,8 +208,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// (event) BondCreated is raised when process of creating bond is finished.
+        /// The BondCreated event is raised when the process of creating the bond is finished.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<BondCreatedEventArgs> BondCreated
         {
             add
@@ -205,8 +232,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// (event) BondDestroyed is raised when the bond is destroyed.
+        /// The BondDestroyed event is raised when the bond is destroyed.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<BondDestroyedEventArgs> BondDestroyed
         {
             add
@@ -228,8 +256,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// (event) AuthorizationChanged is raised when the authorization of device is changed.
+        /// The AuthorizationChanged event is raised when the authorization of the device is changed.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<AuthorizationChangedEventArgs> AuthorizationChanged
         {
             add
@@ -251,8 +280,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// (event) ServiceSearched is raised when the process of service searched is finished.
+        /// The ServiceSearched event is raised when the process of service searched is finished.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<ServiceSearchedEventArgs> ServiceSearched
         {
             add
@@ -274,8 +304,9 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// (event) ConnectionStateChanged is raised when the connection state is changed.
+        /// The ConnectionStateChanged event is raised when the connection state is changed.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<DeviceConnectionStateChangedEventArgs> ConnectionStateChanged
         {
             add
@@ -430,12 +461,15 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// Creates a bond with remote Bluetooth device.
+        /// Creates a bond with the remote Bluetooth device.
         /// </summary>
         /// <remarks>
-        /// The Bluetooth must be enabled and remote device must be discoverable by StartDiscovery().The bond can be destroyed by DestroyBond().
-        /// The bonding request can be cancelled by CancelBonding().If this succeeds, BondCreated event will be invoked.
+        /// The Bluetooth must be enabled and the remote device must be discoverable by StartDiscovery(). The bond can be destroyed by DestroyBond().
+        /// The bonding request can be cancelled by CancelBonding(). If this succeeds, the BondCreated event will be invoked.
         /// </remarks>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the create bonding process to the remote device fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void CreateBond()
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
@@ -455,6 +489,9 @@ namespace Tizen.Network.Bluetooth
         /// <remarks>
         /// Bonding must be in progress by CreateBond().
         /// </remarks>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the cancel bonding procedure to remote device fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void CancelBonding()
         {
             int ret = Interop.Bluetooth.CancelBonding();
@@ -470,8 +507,11 @@ namespace Tizen.Network.Bluetooth
         /// </summary>
         /// <remarks>
         /// The Bluetooth must be enabled and the bond must be created by CreateBond().
-        /// If this succeeds, BondDestroyed event will be invoked.
+        /// If this succeeds, the BondDestroyed event will be invoked.
         /// </remarks>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the destroy bonding procedure fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void DestroyBond()
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
@@ -491,7 +531,10 @@ namespace Tizen.Network.Bluetooth
         /// <remarks>
         /// The Bluetooth must be enabled and the bond must be created by CreateBond().
         /// </remarks>
-        /// <param name="aliasName">Alias name of remote device.</param>
+        /// <param name="aliasName">The alias name of the remote device.</param>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the set alias name to remote device fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void SetAlias(string aliasName)
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
@@ -510,9 +553,12 @@ namespace Tizen.Network.Bluetooth
         /// </summary>
         /// <remarks>
         /// The Bluetooth must be enabled and the bond must be created by CreateBond().
-        /// If this succeeds, AuthorizationChanged event will be invoked.
+        /// If this succeeds, the AuthorizationChanged event will be invoked.
         /// </remarks>
-        /// <param name="authorizationState">Authorization state.</param>
+        /// <param name="authorizationState">The authorization state.</param>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the set authorization to remote device fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void SetAuthorization(BluetoothAuthorizationType authorizationState)
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
@@ -527,10 +573,13 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// Gets the mask from UUID.
+        /// Gets the mask from the UUID.
         /// </summary>
         /// <returns>The service mask list converted from the given UUID list.</returns>
-        /// <param name="uuids">Uuid list of the device.</param>
+        /// <param name="uuids">The UUID list of the device.</param>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the get Mask from UUID fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public BluetoothServiceClassType GetMaskFromUuid(string[] uuids)
         {
             BluetoothServiceClassType serviceMask;
@@ -548,9 +597,12 @@ namespace Tizen.Network.Bluetooth
         /// Starts the search for services supported by the specified device.
         /// </summary>
         /// <remarks>
-        /// The Bluetooth must be enabled and remote device must be discoverable by StartDiscovery().The bond must be created by CreateBond().
-        /// If this succeeds, ServiceSearched will be invoked.
+        /// The Bluetooth must be enabled and remote device must be discoverable by StartDiscovery(). The bond must be created by CreateBond().
+        /// If this succeeds, the ServiceSearched event will be invoked.
         /// </remarks>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when the remote device service search fails.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public void StartServiceSearch()
         {
             Log.Info(Globals.LogTag, "startservicesearch entry");
@@ -572,12 +624,15 @@ namespace Tizen.Network.Bluetooth
         /// The Bluetooth must be enabled.
         /// </remarks>
         /// <returns>The connected Bluetooth profiles.</returns>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when there is no BT connection.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public IEnumerable<BluetoothProfileType> GetConnectedProfiles()
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
             {
                 List<BluetoothProfileType> profileList = new List<BluetoothProfileType>();
-                Interop.Bluetooth.ConnectedProfileCallback callback = (int profile, IntPtr userData) =>
+                _connectedProfileCallback = (int profile, IntPtr userData) =>
                 {
                     if (!profile.Equals(null))
                     {
@@ -585,7 +640,7 @@ namespace Tizen.Network.Bluetooth
                     }
                     return true;
                 };
-                int ret = Interop.Bluetooth.GetConnectedProfiles(RemoteDeviceAddress, callback, IntPtr.Zero);
+                int ret = Interop.Bluetooth.GetConnectedProfiles(RemoteDeviceAddress, _connectedProfileCallback, IntPtr.Zero);
                 if (ret != (int)BluetoothError.None)
                 {
                     Log.Error(Globals.LogTag, "Failed to get connected profiles, Error - " + (BluetoothError)ret);
@@ -605,8 +660,11 @@ namespace Tizen.Network.Bluetooth
         /// <remarks>
         /// The Bluetooth must be enabled.
         /// </remarks>
-        /// <returns><c>true</c> if profile is connected; otherwise, <c>false</c>.</returns>
-        /// <param name="profileType">Bluetooth Profile type.</param>
+        /// <returns><c>true</c> if profile is connected, otherwise <c>false</c>.</returns>
+        /// <param name="profileType">The Bluetooth profile type.</param>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
+        /// or when there is no BT connection.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsProfileConnected(BluetoothProfileType profileType)
         {
             if (BluetoothAdapter.IsBluetoothEnabled)
@@ -626,46 +684,34 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
-        /// Returns the instance of Bluetooth profile type.
+        /// Returns the instance of the Bluetooth profile type.
         /// </summary>
         /// <remarks>
         /// The Bluetooth must be enabled.
         /// </remarks>
+        /// <returns>The profile instance.</returns>
+        /// <since_tizen> 3 </since_tizen>
         public T GetProfile<T>() where T : BluetoothProfile
         {
-            /*
-             * FIXME: Find a proper way for dynamic allocation.
-             */
-            T profile = null;
-            String type = typeof(T).ToString();
-            if (type.Equals("Tizen.Network.Bluetooth.BluetoothAudio"))
-            {
-                BluetoothAudio audio = new BluetoothAudio();
-                profile = (audio as T);
-            }
-            else if (type.Equals("Tizen.Network.Bluetooth.BluetoothAvrcp"))
-            {
-                BluetoothAvrcp avrcp = new BluetoothAvrcp();
-                profile = (avrcp as T);
-            }
-            else if (type.Equals("Tizen.Network.Bluetooth.BluetoothHid"))
+            try
             {
-                BluetoothHid hid = new BluetoothHid();
-                profile = (hid as T);
+                // TODO : Need to check capability of supporting profiles
+                var profile = (T)Activator.CreateInstance(typeof(T), true);
+                profile.RemoteAddress = RemoteDeviceAddress;
+                return profile;
             }
-
-            if (profile != null)
+            catch (TargetInvocationException err)
             {
-                profile.RemoteAddress = RemoteDeviceAddress;
+                throw err.InnerException;
             }
-            return profile;
         }
 
         /// <summary>
         /// Creates the client socket.
         /// </summary>
         /// <returns>The IBluetoothClientSocket instance.</returns>
-        /// <param name="serviceUuid">The UUID of service.</param>
+        /// <param name="serviceUuid">The UUID of the service.</param>
+        /// <since_tizen> 3 </since_tizen>
         public IBluetoothClientSocket CreateSocket(string serviceUuid)
         {
             BluetoothSocket clientSocket = new BluetoothSocket();