From: Wootak Jung Date: Tue, 11 Oct 2022 06:56:31 +0000 (+0900) Subject: Add hid connect/disconnect functions X-Git-Tag: accepted/tizen/unified/20221111.105330~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F57%2F282757%2F1;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-bluetooth.git Add hid connect/disconnect functions Change-Id: I58206d65eb3c5211870341f7dc9f746f14fe33f3 Signed-off-by: Wootak Jung --- diff --git a/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs b/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs index 76b995e..f519305 100644 --- a/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs +++ b/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs @@ -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; - // } - // } - //} } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs index b498ec1..fc78f77 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs @@ -14,8 +14,6 @@ namespace SettingBluetooth.Model private bool mIsPaired; private BtDeviceState mDeviceState; private BluetoothDevice mBluetoothDevice; - - //private IEnumerable 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(); + 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(); + mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged; + mBluetoothHid.Disconnect(); + mDeviceState = BtDeviceState.Disconnecting; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting); } } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs index 5d3c041..2a63eff 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs @@ -60,6 +60,19 @@ namespace SettingBluetooth.Model if (ev.BTState == BluetoothState.Enabled) { mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activated)); + + IEnumerable 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)); + } } } diff --git a/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs b/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs index 099ab3b..2645713 100644 --- a/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs +++ b/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs @@ -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; } } diff --git a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs index bb548da..060c28d 100644 --- a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs +++ b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs @@ -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: diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs b/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs index d8e27f5..18a9f84 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs @@ -132,6 +132,15 @@ namespace SettingBluetooth.res.locale { } } + /// + /// 과(와) 유사한 지역화된 문자열을 찾습니다. + /// + public static string IDS_BT_POP_SEARCHING_SERVICES_ING { + get { + return ResourceManager.GetString("IDS_BT_POP_SEARCHING_SERVICES_ING", resourceCulture); + } + } + /// /// 과(와) 유사한 지역화된 문자열을 찾습니다. /// diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx index 4d87bf1..8f57d73 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx @@ -141,6 +141,9 @@ Disconnect device + + Searching services... + Connected diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx index 5465560..8716301 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx @@ -141,6 +141,9 @@ 디바이스 연결 해제 + + 서비스 검색 중... + 연결됨 diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx index 73250b6..7cf6660 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx @@ -141,6 +141,9 @@ + + + diff --git a/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk b/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk index 230ace3..c697ae8 100644 Binary files a/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk and b/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk differ