Add try catch logic to handle exception 06/291506/1 accepted/tizen/unified/20230420.153147
authorWootak Jung <wootak.jung@samsung.com>
Mon, 17 Apr 2023 23:40:51 +0000 (08:40 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Mon, 17 Apr 2023 23:42:02 +0000 (08:42 +0900)
Change-Id: I39751182416b2eace509bc9dd3f86293dd769e59
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
SettingBluetooth/SettingBluetooth/Model/BtDevice.cs
SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs
SettingBluetooth/SettingBluetooth/View/BtMainView.cs
packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk

index ce6efe04fd8810b86e10f9eccc3229d49e0e8c5b..241a8332b75317c9ccb99ce46cb53af0276d68fc 100644 (file)
@@ -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<BluetoothAudio>();
-            mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
-            mBluetoothAudio.Connect(BluetoothAudioProfileType.AdvancedAudioDistribution);
-            mDeviceState = BtDeviceState.Connecting;
-            BtModel.NotifyDeviceChanged(this);
-            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+            try
+            {
+                mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
+                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<BluetoothAudio>();
-            mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
-            mBluetoothAudio.Disconnect(BluetoothAudioProfileType.AdvancedAudioDistribution);
-            mDeviceState = BtDeviceState.Disconnecting;
-            BtModel.NotifyDeviceChanged(this);
-            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+            try
+            {
+                mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
+                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<BluetoothHid>();
-            mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
-            mBluetoothHid.Connect();
-            mDeviceState = BtDeviceState.Connecting;
-            BtModel.NotifyDeviceChanged(this);
-            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+            try
+            {
+                mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+                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<BluetoothHid>();
-            mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
-            mBluetoothHid.Disconnect();
-            mDeviceState = BtDeviceState.Disconnecting;
-            BtModel.NotifyDeviceChanged(this);
-            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+            try
+            {
+                mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+                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());
+            }
         }
     }
 }
index 67b65a55f7ec7398b9e3fa39217e8ac10a165718..200f8c1d5c134b9b94db88039a749257331bdb08 100644 (file)
@@ -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<BluetoothDevice> 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<BluetoothDevice> 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());
             }
         }
 
index 8cb4504526eddba4f1c9944b78fa582e1e11baf8..e399dcc10897e9955c968f23b5002d3887da9e44 100644 (file)
@@ -84,7 +84,10 @@ namespace SettingBluetooth
             if (BtModel.IsEnabled)
             {
                 BtDeviceView.AddDeviceView(mMainView);
-                AdapterController.AutoStart();
+                if (!BtModel.IsScanning)
+                {
+                    AdapterController.AutoStart();
+                }
             }
 
             return mMainView;
index 108723e4338a46580af478bcaa9e1d40dc4eb3ac..4d62c5ab7a9611c22fc7bc6908ac34029f323b46 100644 (file)
Binary files a/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk and b/packaging/org.tizen.cssetting-bluetooth-1.0.0.rpk differ