From abcae5c9b69e60daccf6e5096ed1be1623922b12 Mon Sep 17 00:00:00 2001 From: wootak Date: Tue, 23 Jun 2020 15:39:15 +0900 Subject: [PATCH] [Bluetooth][Non-ACR] Fix GattConnection event not occured issue (#1741) Signed-off-by: Wootak Jung --- .../Tizen.Network.Bluetooth/BluetoothGatt.cs | 22 ++------- .../Tizen.Network.Bluetooth/BluetoothLeAdapter.cs | 19 +++----- .../BluetoothLeAdapterImpl.cs | 56 ---------------------- 3 files changed, 11 insertions(+), 86 deletions(-) diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs index bb7bb75..11d42e0 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs @@ -261,19 +261,11 @@ namespace Tizen.Network.Bluetooth private static event EventHandler 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; } /// @@ -287,13 +279,7 @@ namespace Tizen.Network.Bluetooth /// 6 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 StaticConnectionStateChanged + internal static event EventHandler StaticConnectionStateChanged { add { diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapter.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapter.cs index 6b7a28a..5f29846 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapter.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapter.cs @@ -131,17 +131,7 @@ namespace Tizen.Network.Bluetooth { /// /// 3 [Obsolete("Deprecated since API level 6. Please use ConnectionStateChanged event on BluetoothGattClient.")] - public event EventHandler GattConnectionStateChanged - { - add - { - BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged += value; - } - remove - { - BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged -= value; - } - } + public event EventHandler 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); + }; } /// @@ -514,7 +509,7 @@ namespace Tizen.Network.Bluetooth { } else { - client = BluetoothGattClient.CreateClient(_remoteAddress, true); + client = BluetoothGattClient.CreateClient(_remoteAddress); } } else diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs index 143f86a..77ea945 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs @@ -34,9 +34,6 @@ namespace Tizen.Network.Bluetooth private event EventHandler _advertisingStateChanged = null; private Interop.Bluetooth.AdvertisingStateChangedCallBack _advertisingStateChangedCallback; - private event EventHandler _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 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) => -- 2.7.4