[Bluetooth][Non-ACR] Add public constructor for BluetoothDevice (#3065)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothDevice.cs
index 33adb66..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>
-    /// This class 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>
+        /// 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
@@ -72,6 +87,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The name of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public string Name
         {
             get
@@ -82,6 +98,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The strength indicator of received signal of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public int Rssi
         {
             get
@@ -92,6 +109,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The class of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public BluetoothClass Class
         {
             get
@@ -102,6 +120,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// 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
@@ -122,6 +142,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The paired state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsPaired
         {
             get
@@ -132,6 +153,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The connection state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsConnected
         {
             get
@@ -142,6 +164,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The authorization state of the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public bool IsAuthorized
         {
             get
@@ -152,6 +175,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The Bluetooth appearance.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public BluetoothAppearanceType AppearanceType
         {
             get
@@ -163,6 +187,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// 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
@@ -184,6 +210,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// 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
@@ -207,6 +234,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The BondDestroyed event is raised when the bond is destroyed.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<BondDestroyedEventArgs> BondDestroyed
         {
             add
@@ -230,6 +258,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// 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
@@ -253,6 +282,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// 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
@@ -276,6 +306,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The ConnectionStateChanged event is raised when the connection state is changed.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public event EventHandler<DeviceConnectionStateChangedEventArgs> ConnectionStateChanged
         {
             add
@@ -438,6 +469,7 @@ namespace Tizen.Network.Bluetooth
         /// </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)
@@ -459,6 +491,7 @@ namespace Tizen.Network.Bluetooth
         /// </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();
@@ -478,6 +511,7 @@ namespace Tizen.Network.Bluetooth
         /// </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)
@@ -500,6 +534,7 @@ namespace Tizen.Network.Bluetooth
         /// <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)
@@ -523,6 +558,7 @@ namespace Tizen.Network.Bluetooth
         /// <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)
@@ -543,6 +579,7 @@ namespace Tizen.Network.Bluetooth
         /// <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;
@@ -565,6 +602,7 @@ namespace Tizen.Network.Bluetooth
         /// </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");
@@ -588,12 +626,13 @@ namespace Tizen.Network.Bluetooth
         /// <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))
                     {
@@ -601,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);
@@ -625,6 +664,7 @@ namespace Tizen.Network.Bluetooth
         /// <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)
@@ -649,40 +689,21 @@ namespace Tizen.Network.Bluetooth
         /// <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"))
+            try
             {
-                BluetoothAvrcp avrcp = new BluetoothAvrcp();
-                profile = (avrcp as T);
-            }
-            else if (type.Equals("Tizen.Network.Bluetooth.BluetoothHid"))
-            {
-                BluetoothHid hid = new BluetoothHid();
-                profile = (hid as T);
-            }
-
-            else if (type.Equals("Tizen.Network.Bluetooth.BluetoothOppClient"))
-            {
-                BluetoothOppClient oppClient = new BluetoothOppClient();
-                profile = (oppClient 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>
@@ -690,6 +711,7 @@ namespace Tizen.Network.Bluetooth
         /// </summary>
         /// <returns>The IBluetoothClientSocket instance.</returns>
         /// <param name="serviceUuid">The UUID of the service.</param>
+        /// <since_tizen> 3 </since_tizen>
         public IBluetoothClientSocket CreateSocket(string serviceUuid)
         {
             BluetoothSocket clientSocket = new BluetoothSocket();