From 5b3d41fdd5edc485c860a082a415ae5443fbec56 Mon Sep 17 00:00:00 2001 From: Gowtham Anandha Babu Date: Tue, 12 Sep 2017 21:28:13 +0530 Subject: [PATCH] [Bluetooth] Handle System.TypeInitializationException If bluetooth is not supported in platform, lower layer will return NOT_SUPPORTED error. BluetoothAdapterImpl class will throw System.TypeInitializationException, which needs to be handled in BluetoothAdapter class. Change-Id: Ia32ceec4dba24f81ae18051f8724eafef096ea28 Signed-off-by: Gowtham Anandha Babu --- .../Tizen.Network.Bluetooth/BluetoothAdapter.cs | 150 ++++++++++++++++++--- .../BluetoothAdapterImpl.cs | 1 - 2 files changed, 134 insertions(+), 17 deletions(-) diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs index ee9aca5..756944a 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs @@ -35,7 +35,14 @@ namespace Tizen.Network.Bluetooth { get { - return BluetoothAdapterImpl.Instance.IsBluetoothEnabled; + try + { + return BluetoothAdapterImpl.Instance.IsBluetoothEnabled; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } @@ -85,7 +92,14 @@ namespace Tizen.Network.Bluetooth } set { - BluetoothAdapterImpl.Instance.Name = value; + try + { + BluetoothAdapterImpl.Instance.Name = value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } @@ -161,89 +175,186 @@ namespace Tizen.Network.Bluetooth /// /// The StateChanged event is raised when the Bluetooth adapter state is changed. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler StateChanged { add { - BluetoothAdapterImpl.Instance.StateChanged += value; + try + { + BluetoothAdapterImpl.Instance.StateChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } remove { - BluetoothAdapterImpl.Instance.StateChanged -= value; + try + { + BluetoothAdapterImpl.Instance.StateChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } /// /// The NameChanged event is raised when the Bluetooth adapter name is changed. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler NameChanged { add { - BluetoothAdapterImpl.Instance.NameChanged += value; + try + { + BluetoothAdapterImpl.Instance.NameChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } remove { - BluetoothAdapterImpl.Instance.NameChanged -= value; + try + { + BluetoothAdapterImpl.Instance.NameChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } /// /// The VisibilityModeChanged event is raised when the Bluetooth adapter visibility mode is changed. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler VisibilityModeChanged { add { - BluetoothAdapterImpl.Instance.VisibilityModeChanged += value; + try + { + BluetoothAdapterImpl.Instance.VisibilityModeChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } remove { - BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value; + try + { + BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } /// /// The VisibilityDurationChanged event is raised very second until the visibility mode is changed to NonDiscoverable. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler VisibilityDurationChanged { add { - BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value; + try + { + BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } remove { - BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value; + try + { + BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } /// /// The DiscoveryStateChanged event is raised when the device discovery state is changed. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler DiscoveryStateChanged { add { - BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value; + try + { + BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } remove { - BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value; + try + { + BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } /// /// This event is called when the LE scan result is obtained. /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. static public event EventHandler ScanResultChanged { add { - BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value; + try + { + BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } - remove { - BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value; + remove + { + try + { + BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value; + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } } @@ -336,7 +447,14 @@ namespace Tizen.Network.Bluetooth /// Thrown when the Bluetooth is not enabled. static public bool IsServiceUsed(string serviceUuid) { - return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid); + try + { + return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid); + } + catch (TypeInitializationException e) + { + throw e.InnerException; + } } /// diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs index 444c461..14e2981 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs @@ -598,7 +598,6 @@ namespace Tizen.Network.Bluetooth if (ret != (int)BluetoothError.None) { Log.Error (Globals.LogTag, "Failed to deinitialize bluetooth, Error - " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException (ret); } else { -- 2.7.4