[Bluetooth][Non-ACR] Add public constructor for BluetoothDevice (#3065)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothDevice.cs
index 1a06057..6f3586f 100644 (file)
@@ -20,6 +20,8 @@ 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
 {
@@ -42,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;
@@ -61,6 +64,16 @@ 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>
@@ -619,7 +632,7 @@ namespace Tizen.Network.Bluetooth
             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))
                     {
@@ -627,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);
@@ -676,41 +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>