From: Wootak Jung Date: Mon, 17 Apr 2023 23:40:51 +0000 (+0900) Subject: Add try catch logic to handle exception X-Git-Tag: accepted/tizen/unified/20230420.153147^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a6411aaf82686e52e7eb9695cfa6cc2059806f3d;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-bluetooth.git Add try catch logic to handle exception Change-Id: I39751182416b2eace509bc9dd3f86293dd769e59 Signed-off-by: Wootak Jung --- diff --git a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs index ce6efe0..241a833 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs @@ -257,11 +257,18 @@ namespace SettingBluetooth // 5. Set BT Operation state as Activated internal void Pair() { - mBluetoothDevice.BondCreated += BluetoothDeviceBondCreated; - mBluetoothDevice.CreateBond(); - mDeviceState = BtDeviceState.Pairing; - BtModel.NotifyDeviceChanged(this); - BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); + try + { + mBluetoothDevice.BondCreated += BluetoothDeviceBondCreated; + mBluetoothDevice.CreateBond(); + mDeviceState = BtDeviceState.Pairing; + BtModel.NotifyDeviceChanged(this); + BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "Pair failed: " + e.ToString()); + } } private void BluetoothDeviceBondDestroyed(object obj, BondDestroyedEventArgs ev) @@ -293,11 +300,18 @@ namespace SettingBluetooth // 5. Set BT Operation state as Activated internal void Unpair() { - mBluetoothDevice.BondDestroyed += BluetoothDeviceBondDestroyed; - mBluetoothDevice.DestroyBond(); - mDeviceState = BtDeviceState.Unpairing; - BtModel.NotifyDeviceChanged(this); - BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); + try + { + mBluetoothDevice.BondDestroyed += BluetoothDeviceBondDestroyed; + mBluetoothDevice.DestroyBond(); + mDeviceState = BtDeviceState.Unpairing; + BtModel.NotifyDeviceChanged(this); + BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "Unpair failed: " + e.ToString()); + } } private void AudioConnectionStateChanged(object obj, AudioConnectionStateChangedEventArgs ev) @@ -343,12 +357,19 @@ namespace SettingBluetooth return; } - mBluetoothAudio = mBluetoothDevice.GetProfile(); - mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged; - mBluetoothAudio.Connect(BluetoothAudioProfileType.AdvancedAudioDistribution); - mDeviceState = BtDeviceState.Connecting; - BtModel.NotifyDeviceChanged(this); - //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + try + { + mBluetoothAudio = mBluetoothDevice.GetProfile(); + mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged; + mBluetoothAudio.Connect(BluetoothAudioProfileType.AdvancedAudioDistribution); + mDeviceState = BtDeviceState.Connecting; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "ConnectAudio failed: " + e.ToString()); + } } // TODO @@ -368,12 +389,19 @@ namespace SettingBluetooth return; } - mBluetoothAudio = mBluetoothDevice.GetProfile(); - mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged; - mBluetoothAudio.Disconnect(BluetoothAudioProfileType.AdvancedAudioDistribution); - mDeviceState = BtDeviceState.Disconnecting; - BtModel.NotifyDeviceChanged(this); - //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + try + { + mBluetoothAudio = mBluetoothDevice.GetProfile(); + mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged; + mBluetoothAudio.Disconnect(BluetoothAudioProfileType.AdvancedAudioDistribution); + mDeviceState = BtDeviceState.Disconnecting; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "DisconnectAudio failed: " + e.ToString()); + } } private void HidConnectionStateChanged(object obj, HidConnectionStateChangedEventArgs ev) @@ -411,12 +439,19 @@ namespace SettingBluetooth return; } - mBluetoothHid = mBluetoothDevice.GetProfile(); - mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged; - mBluetoothHid.Connect(); - mDeviceState = BtDeviceState.Connecting; - BtModel.NotifyDeviceChanged(this); - //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + try + { + mBluetoothHid = mBluetoothDevice.GetProfile(); + mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged; + mBluetoothHid.Connect(); + mDeviceState = BtDeviceState.Connecting; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "ConnectHid failed: " + e.ToString()); + } } internal void DisconnectHid() @@ -428,12 +463,19 @@ namespace SettingBluetooth return; } - mBluetoothHid = mBluetoothDevice.GetProfile(); - mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged; - mBluetoothHid.Disconnect(); - mDeviceState = BtDeviceState.Disconnecting; - BtModel.NotifyDeviceChanged(this); - //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + try + { + mBluetoothHid = mBluetoothDevice.GetProfile(); + mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged; + mBluetoothHid.Disconnect(); + mDeviceState = BtDeviceState.Disconnecting; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "DisconnectHid failed: " + e.ToString()); + } } } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs index 67b65a5..200f8c1 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs @@ -31,50 +31,84 @@ namespace SettingBluetooth internal void Enable() { - BluetoothAdapter.Enable(); - mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activating)); + try + { + BluetoothAdapter.Enable(); + mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activating)); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "Enable failed: " + e.ToString()); + } } internal void Disable() { - BluetoothAdapter.Disable(); - mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Deactivating)); + try + { + BluetoothAdapter.Disable(); + mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Deactivating)); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "Disable failed: " + e.ToString()); + } } internal void StartDiscovery() { - BluetoothAdapter.StartDiscovery(); - mIsScanning = true; - mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searching)); + try + { + BluetoothAdapter.StartDiscovery(); + mIsScanning = true; + mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searching)); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "StartDiscovery failed: " + e.ToString()); + } } internal void StopDiscovery() { - BluetoothAdapter.StopDiscovery(); - mIsScanning = false; - mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched)); + try + { + BluetoothAdapter.StopDiscovery(); + mIsScanning = false; + mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched)); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "StopDiscovery failed: " + e.ToString()); + } } internal void GetBondedDevices() { - IEnumerable devices; - BtDevice btDevice; - BtDeviceChangedEventArgs args; - devices = BluetoothAdapter.GetBondedDevices(); - - foreach (BluetoothDevice device in devices) + try { - Log.Info(SettingBluetooth.LogTag, "GetBondedDevices. Address: " + device.Address + ", Name: " + device.Name); - if (device.IsConnected) - { - btDevice = new BtDevice(device, BtDeviceState.Connected); - } - else + IEnumerable devices; + BtDevice btDevice; + BtDeviceChangedEventArgs args; + devices = BluetoothAdapter.GetBondedDevices(); + foreach (BluetoothDevice device in devices) { - btDevice = new BtDevice(device, BtDeviceState.Paired); + Log.Info(SettingBluetooth.LogTag, "GetBondedDevices. Address: " + device.Address + ", Name: " + device.Name); + if (device.IsConnected) + { + btDevice = new BtDevice(device, BtDeviceState.Connected); + } + else + { + btDevice = new BtDevice(device, BtDeviceState.Paired); + } + args = new BtDeviceChangedEventArgs(btDevice); + mDeviceChanged?.Invoke(null, args); } - args = new BtDeviceChangedEventArgs(btDevice); - mDeviceChanged?.Invoke(null, args); + } + catch (Exception e) + { + Log.Error(SettingBluetooth.LogTag, "GetBondedDevices failed: " + e.ToString()); } } diff --git a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs index 8cb4504..e399dcc 100644 --- a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs +++ b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs @@ -84,7 +84,10 @@ namespace SettingBluetooth if (BtModel.IsEnabled) { BtDeviceView.AddDeviceView(mMainView); - AdapterController.AutoStart(); + if (!BtModel.IsScanning) + { + AdapterController.AutoStart(); + } } return mMainView; diff --git a/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk b/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk index 108723e..4d62c5a 100644 Binary files a/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk and b/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk differ