Add hid connect/disconnect functions 57/282757/1
authorWootak Jung <wootak.jung@samsung.com>
Tue, 11 Oct 2022 06:56:31 +0000 (15:56 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Tue, 11 Oct 2022 06:56:43 +0000 (15:56 +0900)
Change-Id: I58206d65eb3c5211870341f7dc9f746f14fe33f3
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs
SettingBluetooth/SettingBluetooth/Model/BtDevice.cs
SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs
SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs
SettingBluetooth/SettingBluetooth/View/BtMainView.cs
SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs
SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx
SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx
SettingBluetooth/SettingBluetooth/res/locale/Resources.resx
packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk

index 76b995e064e58e1ed2f3c1a4bd4a9a16ca1bfce2..f519305b47fe29098cffd018481eb4b215389506 100644 (file)
@@ -11,97 +11,105 @@ namespace SettingBluetooth.Controller
 {
     internal static class DeviceController
     {
-        internal static void ConnectA2dp(BtDevice device)
+        private static void PopDialog()
+        {
+            Navigator navigator = NUIApplication.GetDefaultWindow().GetDefaultNavigator();
+            navigator?.Pop();
+        }
+
+        private static void ShowDialog(BtDevice btDevice, Button disconnectButton)
+        {
+            var cancelButton = new Button()
+            {
+                Text = Resources.IDS_BR_SK_CANCEL,
+            };
+            cancelButton.Clicked += (obj, ev) =>
+            {
+                Log.Info(Program.LogTag, "Disconnect canceled");
+                PopDialog();
+            };
+
+            DialogPage.ShowAlertDialog(Resources.IDS_BT_HEADER_DISCONNECT_DEVICE_ABB, String.Format(Resources.IDS_WMGR_POP_THIS_WILL_END_YOUR_CONNECTION_WITH_PS, btDevice.Name), cancelButton, disconnectButton);
+        }
+
+        private static void ConnectA2dp(BtDevice device)
         {
-            Log.Info(Program.LogTag, "Connect to the remote audio device. Address: " + device.Address);
             device.ConnectAudio();
         }
 
-        internal static void DisconnectA2dp(BtDevice device)
+        private static void DisconnectA2dp(BtDevice btDevice)
         {
-            Log.Info(Program.LogTag, "Disconnect to the remote audio device. Address: " + device.Address);
-            device.DisconnectAudio();
+            var disconnectButton = new Button()
+            {
+                Text = Resources.IDS_BT_SK_DISCONNECT,
+            };
+            disconnectButton.Clicked += (obj, ev) =>
+            {
+                Log.Info(Program.LogTag, "Disconnect to the remote audio device");
+                btDevice.DisconnectAudio();
+                PopDialog();
+            };
+            ShowDialog(btDevice, disconnectButton);
         }
 
-        private static void PopAlertDialog()
+        private static void ConnectHid(BtDevice device)
         {
-            Navigator navigator = NUIApplication.GetDefaultWindow().GetDefaultNavigator();
-            navigator?.Pop();
+            device.ConnectHid();
         }
 
-        private static void DoPairUnpair(Device device)
+        private static void DisconnectHid(BtDevice btDevice)
         {
+            var disconnectButton = new Button()
+            {
+                Text = Resources.IDS_BT_SK_DISCONNECT,
+            };
+            disconnectButton.Clicked += (obj, ev) =>
+            {
+                Log.Info(Program.LogTag, "Disconnect to the remote hid device");
+                btDevice.DisconnectHid();
+                PopDialog();
+            };
+            ShowDialog(btDevice, disconnectButton);
+        }
+
+        internal static void DeviceItemClicked(object obj, ClickedEventArgs ev)
+        {
+            DefaultLinearItem deviceItem = (DefaultLinearItem)obj;
+            var device = deviceItem.BindingContext as Device;
+            Log.Info(Program.LogTag, "DeviceItem clicked. Address: " + device.Address + ", Name: " + device.Name);
+
             if (device.BtItem.IsPaired)
             {
-                if (device.BtItem.IsA2dpConnected)
+                if (device.BtItem.IsA2dpSupported)
                 {
-                    var cancelButton = new Button()
+                    if (device.BtItem.IsA2dpConnected)
                     {
-                        Text = Resources.IDS_BR_SK_CANCEL,
-                    };
-                    cancelButton.Clicked += (obj, ev) =>
+                        DisconnectA2dp(device.BtItem);
+                    }
+                    else
                     {
-                        Log.Info(Program.LogTag, "Disconnect canceled");
-                        PopAlertDialog();
-                    };
-
-                    var disconnectButton = new Button()
-                    {
-                        Text = Resources.IDS_BT_SK_DISCONNECT,
-                    };
-                    disconnectButton.Clicked += (obj, ev) =>
-                    {
-                        device.BtItem.DisconnectAudio();
-                        PopAlertDialog();
-                    };
-                    DialogPage.ShowAlertDialog(Resources.IDS_BT_HEADER_DISCONNECT_DEVICE_ABB, String.Format(Resources.IDS_WMGR_POP_THIS_WILL_END_YOUR_CONNECTION_WITH_PS, device.Name), cancelButton, disconnectButton);
+                        Log.Info(Program.LogTag, "Connect to the remote audio device");
+                        ConnectA2dp(device.BtItem);
+                    }
                 }
-                else
+                else if (device.BtItem.IsHidSupported)
                 {
-                    Log.Info(Program.LogTag, "Unpair to the remote device. Address: " + device.BtItem.Address);
-                    device.BtItem.Unpair();
+                    if (device.BtItem.IsHidConnected)
+                    {
+                        DisconnectHid(device.BtItem);
+                    }
+                    else
+                    {
+                        Log.Info(Program.LogTag, "Connect to the remote hid device");
+                        ConnectHid(device.BtItem);
+                    }
                 }
             }
             else
             {
-                Log.Info(Program.LogTag, "Pair to the remote device. Address: " + device.BtItem.Address);
+                Log.Info(Program.LogTag, "Pair to the remote device");
                 device.BtItem.Pair();
             }
         }
-
-        internal static void DeviceItemClicked(object obj, ClickedEventArgs ev)
-        {
-            DefaultLinearItem deviceItem = (DefaultLinearItem)obj;
-            var device = deviceItem.BindingContext as Device;
-            Log.Debug(Program.LogTag, "DeviceItem clicked. Name: " + device.Name);
-            DoPairUnpair(device);
-        }
-
-        //internal static void DeviceViewSelectionChanged(object obj, SelectionChangedEventArgs ev)
-        //{
-        //    Log.Debug(Program.LogTag, "DeviceViewSelectionChanged");
-
-        //    //SingleSelection Only have 1 or nil object in the list.
-        //    foreach (object item in ev.PreviousSelection)
-        //    {
-        //        if (item == null) break;
-        //        if (item is Device device)
-        //        {
-        //            DoPairUnpair(device);
-        //            //device.Connected = false;
-        //        }
-        //    }
-        //    foreach (object item in ev.CurrentSelection)
-        //    {
-        //        if (item == null) break;
-        //        if (item is Device device)
-        //        {
-        //            DoPairUnpair(device);
-        //            device.Connected = false; // for test
-        //            //device.btDevice.Pair();
-        //            //device.Registered = true;
-        //        }
-        //    }
-        //}
     }
 }
index b498ec18fa16eebbe901a3a24d8c432acf11637f..fc78f773b4f9117175ba4559d9afa6ecba741fed 100644 (file)
@@ -14,8 +14,6 @@ namespace SettingBluetooth.Model
         private bool mIsPaired;
         private BtDeviceState mDeviceState;
         private BluetoothDevice mBluetoothDevice;
-
-        //private IEnumerable<string> mServiceList = null;
         private string[] mServiceUuids = null;
         private BluetoothServiceClassType mServiceMask = 0;
 
@@ -41,10 +39,12 @@ namespace SettingBluetooth.Model
                 mServiceMask = device.GetMaskFromUuid(mServiceUuids);
                 if ((mServiceMask & BluetoothServiceClassType.A2dp) == BluetoothServiceClassType.A2dp)
                 {
+                    Log.Info(Program.LogTag, "A2dp supported");
                     mIsA2dpSupported = true;
                 }
                 if ((mServiceMask & BluetoothServiceClassType.Hid) == BluetoothServiceClassType.Hid)
                 {
+                    Log.Info(Program.LogTag, "Hid supported");
                     mIsHidSupported = true;
                 }
             }
@@ -121,6 +121,14 @@ namespace SettingBluetooth.Model
             }
         }
 
+        internal bool IsHidConnected
+        {
+            get
+            {
+                return mIsHidConnected;
+            }
+        }
+
         private void BluetoothDeviceBondCreated(object obj, BondCreatedEventArgs ev)
         {
             Log.Info(Program.LogTag, "BluetoothDeviceBondCreated. Address: " + ev.Device.Address + ", IsPaired: " + ev.Device.IsPaired);
@@ -271,14 +279,64 @@ namespace SettingBluetooth.Model
             //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
         }
 
+        private void HidConnectionStateChanged(object obj, HidConnectionStateChangedEventArgs ev)
+        {
+            if (ev.Result == (int)BluetoothError.None)
+            {
+                Log.Info(Program.LogTag, "HidConnectionStateChanged. IsConnected: " + ev.IsConnected);
+                mIsHidConnected = ev.IsConnected;
+                if (mIsHidConnected)
+                {
+                    mDeviceState = BtDeviceState.Connected;
+                }
+                else
+                {
+                    mDeviceState = BtDeviceState.Paired;
+                }
+                BtModel.NotifyDeviceChanged(this);
+                //BtModel.NotifyOperationStateChanged(BtOperationState.Activated);
+            }
+            else
+            {
+                Log.Error(Program.LogTag, (mDeviceState == BtDeviceState.Connecting ? "Connect" : "Disconnect") + "failed. result: " + ev.Result);
+                mDeviceState = BtDeviceState.Idle;
+                BtModel.NotifyDeviceChanged(this);
+            }
+            mBluetoothHid.HidConnectionStateChanged -= HidConnectionStateChanged;
+        }
+
         internal void ConnectHid()
         {
-            // TODO (Same with audio)
+            if (mIsHidSupported == false)
+            {
+                Log.Error(Program.LogTag, "This device does not support Hid profile");
+                // exception?
+                return;
+            }
+
+            mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+            mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
+            mBluetoothHid.Connect();
+            mDeviceState = BtDeviceState.Connecting;
+            BtModel.NotifyDeviceChanged(this);
+            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
         }
 
         internal void DisconnectHid()
         {
-            // TODO (Same with audio)
+            if (mIsHidSupported == false)
+            {
+                Log.Error(Program.LogTag, "This device does not support Hid profile");
+                // exception?
+                return;
+            }
+
+            mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+            mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
+            mBluetoothHid.Disconnect();
+            mDeviceState = BtDeviceState.Disconnecting;
+            BtModel.NotifyDeviceChanged(this);
+            //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
         }
     }
 }
index 5d3c041d8c701113a363ead3ccfb9ef3b74b14ae..2a63effa17dbaa1f367900ee45760a92b851a1d3 100644 (file)
@@ -60,6 +60,19 @@ namespace SettingBluetooth.Model
             if (ev.BTState == BluetoothState.Enabled)
             {
                 mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activated));
+
+                IEnumerable<BluetoothDevice> devices;
+                BtDevice btDevice;
+                BtDeviceChangedEventArgs args;
+
+                devices = BluetoothAdapter.GetBondedDevices();
+                foreach (BluetoothDevice device in devices)
+                {
+                    Log.Info(Program.LogTag, "GetBondedDevices. Address: " + device.Address + ", Name: " + device.Name);
+                    btDevice = new BtDevice(device, BtDeviceState.Paired);
+                    args = new BtDeviceChangedEventArgs(btDevice);
+                    mDeviceChanged?.Invoke(null, args);
+                }
             }
             else
             {
@@ -88,8 +101,11 @@ namespace SettingBluetooth.Model
             }
             else if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Finished)
             {
-                mIsScanning = false;
-                mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched));
+                if (mIsScanning)
+                {
+                    mIsScanning = false;
+                    mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched));
+                }
             }
         }
 
index 099ab3be074d071d36cbf50963d01750fa5add33..2645713e652ea1bf72b3cf0e7f1cbfc8da98bff4 100644 (file)
@@ -39,14 +39,6 @@ namespace SettingBluetooth
             mAddress = btDevice.Address;
             mName = btDevice.Name;
             mBtItem = btDevice;
-            if (btDevice.DeviceState == BtDeviceState.Pairing)
-            {
-                mState = Resources.IDS_BT_SBODY_CONNECTING_ING;
-            }
-            else if (btDevice.DeviceState == BtDeviceState.Paired)
-            {
-                mState = Resources.IDS_BT_BODY_PAIRED;
-            }
         }
 
         internal BtDevice BtItem
@@ -186,6 +178,7 @@ namespace SettingBluetooth
         internal void AddDevice(BtDevice btDevice)
         {
             collection.AddDevice(btDevice);
+            UpdateState(btDevice);
         }
 
         internal void RemoveDevice(string address)
@@ -207,10 +200,35 @@ namespace SettingBluetooth
             return null;
         }
 
-        internal void UpdateState(BtDevice btDevice, string state)
+        internal void UpdateState(BtDevice btDevice)
         {
             var device = FindDevice(btDevice);
-            Log.Info(Program.LogTag, "Device(" + btDevice.Address + ") updated. state: " + state);
+            string state = null;
+            switch (btDevice.DeviceState)
+            {
+                case BtDeviceState.Idle:
+                case BtDeviceState.Unpaired:
+                    state = "";
+                    break;
+                case BtDeviceState.Pairing:
+                case BtDeviceState.Connecting:
+                    state = Resources.IDS_BT_SBODY_CONNECTING_ING;
+                    break;
+                case BtDeviceState.Unpairing:
+                case BtDeviceState.Disconnecting:
+                    state = Resources.IDS_BT_BODY_DISCONNECTING;
+                    break;
+                case BtDeviceState.Paired:
+                    state = Resources.IDS_BT_BODY_PAIRED;
+                    break;
+                case BtDeviceState.Connected:
+                    state = Resources.IDS_BT_SBODY_CONNECTED_M_STATUS;
+                    break;
+                case BtDeviceState.ServiceSearching:
+                    state = Resources.IDS_BT_POP_SEARCHING_SERVICES_ING;
+                    break;
+            }
+            //Log.Info(Program.LogTag, "Device(" + btDevice.Address + ") updated. state: " + state);
             device.State = state;
         }
     }
index bb548da2832ea73dbc93674e4aef1956650d9085..060c28d6dc0f3e14d9bd6b5b2b675123173b039c 100644 (file)
@@ -221,12 +221,16 @@ namespace SettingBluetooth
                         {
                             mPairedDevice.AddDevice(ev.BtDevice);
                         }
+                        else
+                        {
+                            mPairedDevice.UpdateState(ev.BtDevice);
+                        }
                     }
                     else
                     {
                         if (mSearchedDevice.FindDevice(ev.BtDevice) != null) // bond/unbond/audio failed case
                         {
-                            mSearchedDevice.UpdateState(ev.BtDevice, ""); // TODO: need to remove SubLabel in this case
+                            mSearchedDevice.UpdateState(ev.BtDevice); // TODO: need to remove SubLabel in this case
                         }
                         else
                         {
@@ -237,7 +241,7 @@ namespace SettingBluetooth
                 case BtDeviceState.Pairing:
                     if (mSearchedDevice.FindDevice(ev.BtDevice) != null)
                     {
-                        mSearchedDevice.UpdateState(ev.BtDevice, Resources.IDS_BT_SBODY_CONNECTING_ING);
+                        mSearchedDevice.UpdateState(ev.BtDevice);
                     }
                     break;
                 case BtDeviceState.Unpairing:
@@ -256,14 +260,14 @@ namespace SettingBluetooth
                     if (mPairedDevice.FindDevice(ev.BtDevice) == null)
                     {
                         mPairedDevice.AddDevice(ev.BtDevice);
-                        if (ev.BtDevice.IsA2dpSupported)
-                        {
-                            DeviceController.ConnectA2dp(ev.BtDevice);
-                        }
+                        //if (ev.BtDevice.IsA2dpSupported)
+                        //{
+                        //    DeviceController.ConnectA2dp(ev.BtDevice);
+                        //}
                     }
                     else // audio disconnected case
                     {
-                        mPairedDevice.UpdateState(ev.BtDevice, "");
+                        mPairedDevice.UpdateState(ev.BtDevice);
                     }
                     break;
                 case BtDeviceState.Unpaired:
@@ -279,19 +283,19 @@ namespace SettingBluetooth
                 case BtDeviceState.Connecting:
                     if (mPairedDevice.FindDevice(ev.BtDevice) != null)
                     {
-                        mPairedDevice.UpdateState(ev.BtDevice, Resources.IDS_BT_SBODY_CONNECTING_ING);
+                        mPairedDevice.UpdateState(ev.BtDevice);
                     }
                     break;
                 case BtDeviceState.Connected:
                     if (mPairedDevice.FindDevice(ev.BtDevice) != null)
                     {
-                        mPairedDevice.UpdateState(ev.BtDevice, Resources.IDS_BT_SBODY_CONNECTED_M_STATUS);
+                        mPairedDevice.UpdateState(ev.BtDevice);
                     }
                     break;
                 case BtDeviceState.Disconnecting:
                     if (mPairedDevice.FindDevice(ev.BtDevice) != null)
                     {
-                        mPairedDevice.UpdateState(ev.BtDevice, Resources.IDS_BT_BODY_DISCONNECTING);
+                        mPairedDevice.UpdateState(ev.BtDevice);
                     }
                     break;
                 case BtDeviceState.ServiceSearching:
index d8e27f541f83fe789c0f599a12dc5f44fc10a3ef..18a9f84e4778c986ab9692817c66a25bc33f75c5 100644 (file)
@@ -132,6 +132,15 @@ namespace SettingBluetooth.res.locale {
             }
         }
         
+        /// <summary>
+        ///   과(와) 유사한 지역화된 문자열을 찾습니다.
+        /// </summary>
+        public static string IDS_BT_POP_SEARCHING_SERVICES_ING {
+            get {
+                return ResourceManager.GetString("IDS_BT_POP_SEARCHING_SERVICES_ING", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   과(와) 유사한 지역화된 문자열을 찾습니다.
         /// </summary>
index 4d87bf19ce69a949e4e4920a24fb52c019d754dc..8f57d7385d1bcd56788c529eebfe8ddbc78db466 100644 (file)
   <data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
     <value>Disconnect device</value>
   </data>
+  <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+    <value>Searching services...</value>
+  </data>
   <data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
     <value>Connected</value>
   </data>
index 5465560794c2d3ca91dd6ad6e751ff31afb9741e..87163013813bc6ef08ec884f508f7fd3f28764f3 100644 (file)
   <data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
     <value>디바이스 연결 해제</value>
   </data>
+  <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+    <value>서비스 검색 중...</value>
+  </data>
   <data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
     <value>연결됨</value>
   </data>
index 73250b6e8350c6657cd8a8353eed794a03c5d237..7cf6660ca4797b3d5519451e03a11d4a48e4e6d9 100644 (file)
   <data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
     <value />
   </data>
+  <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+    <value />
+  </data>
   <data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
     <value />
   </data>
index 230ace3925b4566c68deef5666e45858df195ca6..c697ae8fa9b06c6620f8c85c0eb29c30c617e34a 100644 (file)
Binary files a/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk and b/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk differ