[Bluetooth][Non-ACR] Fix GattConnection event not occured issue (#1741)
authorwootak <wootak.jung@samsung.com>
Tue, 23 Jun 2020 06:39:15 +0000 (15:39 +0900)
committerGitHub <noreply@github.com>
Tue, 23 Jun 2020 06:39:15 +0000 (15:39 +0900)
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapter.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs

index bb7bb75..11d42e0 100644 (file)
@@ -261,19 +261,11 @@ namespace Tizen.Network.Bluetooth
         private static event EventHandler<GattConnectionStateChangedEventArgs> s_connectionStateChanged;
         private static Interop.Bluetooth.GattConnectionStateChangedCallBack s_connectionStateChangeCallback;
 
-        internal BluetoothGattClient(string remoteAddress, bool fromLe)
+        internal BluetoothGattClient(string remoteAddress)
         {
             _impl = new BluetoothGattClientImpl(remoteAddress);
             _remoteAddress = remoteAddress;
-            if (fromLe == false)
-            {
-                StaticConnectionStateChanged += OnConnectionStateChanged;
-            }
-            else
-            {
-                // fromLe will be removed after BluetoothLeDevice.GattConnect removed for backward compatibility.
-                // BluetoothLeDevice.GattConnectionStateChanged event will be occured in this case.
-            }
+            StaticConnectionStateChanged += OnConnectionStateChanged;
         }
 
         /// <summary>
@@ -287,13 +279,7 @@ namespace Tizen.Network.Bluetooth
         /// <since_tizen> 6 </since_tizen>
         public static BluetoothGattClient CreateClient(string remoteAddress)
         {
-            BluetoothGattClient client = new BluetoothGattClient(remoteAddress, false);
-            return client.Isvalid() ? client : null;
-        }
-
-        internal static BluetoothGattClient CreateClient(string remoteAddress, bool fromLe)
-        {
-            BluetoothGattClient client = new BluetoothGattClient(remoteAddress, fromLe);
+            BluetoothGattClient client = new BluetoothGattClient(remoteAddress);
             return client.Isvalid() ? client : null;
         }
 
@@ -340,7 +326,7 @@ namespace Tizen.Network.Bluetooth
             }
         }
 
-        private static event EventHandler<GattConnectionStateChangedEventArgs> StaticConnectionStateChanged
+        internal static event EventHandler<GattConnectionStateChangedEventArgs> StaticConnectionStateChanged
         {
             add
             {
index 6b7a28a..5f29846 100644 (file)
@@ -131,17 +131,7 @@ namespace Tizen.Network.Bluetooth {
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         [Obsolete("Deprecated since API level 6. Please use ConnectionStateChanged event on BluetoothGattClient.")]
-        public event EventHandler<GattConnectionStateChangedEventArgs> GattConnectionStateChanged
-        {
-            add
-            {
-                BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged += value;
-            }
-            remove
-            {
-                BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged -= value;
-            }
-        }
+        public event EventHandler<GattConnectionStateChangedEventArgs> GattConnectionStateChanged;
 
         internal BluetoothLeDevice(BluetoothLeScanData scanData)
         {
@@ -170,6 +160,11 @@ namespace Tizen.Network.Bluetooth {
                 _scanDataValue = new byte[_scanData.ScanDataLength];
                 scanData.ScanData.CopyTo(_scanDataValue, 0);
             }
+
+            BluetoothGattClient.StaticConnectionStateChanged += (s, e) =>
+            {
+                GattConnectionStateChanged?.Invoke(this, e);
+            };
         }
 
         /// <summary>
@@ -514,7 +509,7 @@ namespace Tizen.Network.Bluetooth {
                 }
                 else
                 {
-                    client = BluetoothGattClient.CreateClient(_remoteAddress, true);
+                    client = BluetoothGattClient.CreateClient(_remoteAddress);
                 }
             }
             else
index 143f86a..77ea945 100644 (file)
@@ -34,9 +34,6 @@ namespace Tizen.Network.Bluetooth
         private event EventHandler<AdvertisingStateChangedEventArgs> _advertisingStateChanged = null;
         private Interop.Bluetooth.AdvertisingStateChangedCallBack _advertisingStateChangedCallback;
 
-        private event EventHandler<GattConnectionStateChangedEventArgs> _gattConnectionStateChanged = null;
-        private Interop.Bluetooth.GattConnectionStateChangedCallBack _gattConnectionStateChangedCallback;
-
         private int _serviceListCount = 0;
         private bool _scanStarted;
 
@@ -72,11 +69,6 @@ namespace Tizen.Network.Bluetooth
             {
                 // Free managed objects.
             }
-            //Free unmanaged objects
-            if (_gattConnectionStateChanged != null)
-            {
-                UnRegisterGattConnectionStateChangedEvent();
-            }
 
             //stop scan operation in progress
             StopScan ();
@@ -107,54 +99,6 @@ namespace Tizen.Network.Bluetooth
             }
         }
 
-        internal event EventHandler<GattConnectionStateChangedEventArgs> LeGattConnectionStateChanged
-        {
-            add
-            {
-                if (_gattConnectionStateChanged == null)
-                {
-                    RegisterGattConnectionStateChangedEvent();
-                }
-                _gattConnectionStateChanged += value;
-            }
-            remove
-            {
-                _gattConnectionStateChanged -= value;
-                if (_gattConnectionStateChanged == null)
-                {
-                    UnRegisterGattConnectionStateChangedEvent();
-                }
-            }
-        }
-
-        internal void RegisterGattConnectionStateChangedEvent()
-        {
-            _gattConnectionStateChangedCallback = (int result, bool connected,
-                                    string remoteDeviceAddress, IntPtr userData) =>
-            {
-                Log.Info(Globals.LogTag, "Setting gatt connection state changed callback");
-                GattConnectionStateChangedEventArgs e = new GattConnectionStateChangedEventArgs(result,
-                        connected, remoteDeviceAddress);
-                _gattConnectionStateChanged?.Invoke(null, e);
-            };
-
-            int ret = Interop.Bluetooth.SetGattConnectionStateChangedCallback(
-                                    _gattConnectionStateChangedCallback, IntPtr.Zero);
-            if (ret != (int)BluetoothError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to set gatt connection state changed callback, Error - " + (BluetoothError)ret);
-            }
-        }
-
-        internal void UnRegisterGattConnectionStateChangedEvent()
-        {
-            int ret = Interop.Bluetooth.UnsetGattConnectionStateChangedCallback();
-            if (ret != (int)BluetoothError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to unset gatt connection state changed callback, Error - " + (BluetoothError)ret);
-            }
-        }
-
         internal int StartScan()
         {
             _adapterLeScanResultChangedCallback = (int result, ref BluetoothLeScanDataStruct scanData, IntPtr userData) =>