From: Wootak Jung Date: Thu, 6 Oct 2022 06:19:58 +0000 (+0900) Subject: Add pair/unpiar functions X-Git-Tag: accepted/tizen/unified/20221111.105330~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F77%2F282577%2F1;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-bluetooth.git Add pair/unpiar functions Change-Id: I6f526090d005c649151ae04d60785bb7685f33cc Signed-off-by: Wootak Jung --- diff --git a/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs b/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs index 7508ea4..7195c33 100644 --- a/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs +++ b/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs @@ -8,6 +8,20 @@ namespace SettingBluetooth.Controller { internal static class DeviceController { + private static void DoPairUnpair(Device device) + { + if (device.BtItem.IsPaired) + { + Log.Info(Program.LogTag, "Unpair to the remote device. Address: " + device.BtItem.Address); + device.BtItem.Unpair(); + } + else + { + Log.Info(Program.LogTag, "Pair to the remote device. Address: " + device.BtItem.Address); + device.BtItem.Pair(); + } + } + internal static void DeviceViewSelectionChanged(object obj, SelectionChangedEventArgs ev) { Log.Debug(Program.LogTag, "DeviceViewSelectionChanged"); @@ -18,7 +32,8 @@ namespace SettingBluetooth.Controller if (item == null) break; if (item is Device device) { - device.Connected = false; + DoPairUnpair(device); + //device.Connected = false; } } foreach (object item in ev.CurrentSelection) @@ -26,10 +41,10 @@ namespace SettingBluetooth.Controller if (item == null) break; if (item is Device device) { - Log.Debug(Program.LogTag, "Name: " + device.Name); - device.Connected = true; + DoPairUnpair(device); + //device.Connected = true; //device.btDevice.Pair(); - device.Registered = true; + //device.Registered = true; } } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs index 7f4d7cb..4e20024 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtDevice.cs @@ -1,47 +1,131 @@ using System; using System.Collections.Generic; using System.Text; +using Tizen.Network.Bluetooth; +using Tizen; namespace SettingBluetooth.Model { - public class BtDevice + internal class BtDevice { - internal string RemoteDeviceAddress; - internal BtDeviceState DeviceState; + private string address; + private string name; + private bool isPaired; + private BtDeviceState deviceState; + private BluetoothDevice bluetoothDevice; // TODO - internal BtDevice(string address) // TODO + internal BtDevice(BluetoothDevice device, BtDeviceState state) { - RemoteDeviceAddress = address; + bluetoothDevice = device; + address = bluetoothDevice.Address; + name = bluetoothDevice.Name; + isPaired = bluetoothDevice.IsPaired; + deviceState = state; } - internal static void Pair() + // for dummy instance (No devices found, Paired device) + internal BtDevice(string addr) { - // TODO - // 1. Call CreateBond API - // 2. Set BT Operation state as Pairing - // 3. Set Device state as Pairing - // 4. Bonding Created - // --> Remove device into searched list - // --> Add device into paired list - // --> Invoke Remove Searched Device event to View - // --> Invoke New Paired Device event to View - // 5. Set BT Operation state as Activated + address = addr; + name = addr; } - internal static void Unpair() + internal string Address { - // TODO - // 1. Call DestroyBond API - // 2. Set BT Operation state as Pairing - // 3. Set Device state as Unpairing - // 4. Bonding Destroyed - // --> Remove device into paired list - // --> Invoke Remove Paired Device event to View - // 5. Set BT Operation state as Activated + get + { + return address; + } + } + + internal string Name + { + get + { + return name; + } + } + + internal bool IsPaired + { + get + { + return isPaired; + } + } + + internal BtDeviceState DeviceState + { + get + { + return deviceState; + } + } + + private void BluetoothDeviceBondCreated(object obj, BondCreatedEventArgs ev) + { + Log.Info(Program.LogTag, "BluetoothDeviceBondCreated. Address: " + ev.Device.Address + ", IsPaired: " + ev.Device.IsPaired); + if (ev.Device.IsPaired) + { + isPaired = true; + deviceState = BtDeviceState.Paired; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Activated); + } + bluetoothDevice.BondCreated -= BluetoothDeviceBondCreated; + } + + // TODO + // 1. Call CreateBond API + // 2. Set BT Operation state as Pairing + // 3. Set Device state as Pairing + // 4. Bonding Created + // --> Remove device into searched list + // --> Add device into paired list + // --> Invoke Remove Searched Device event to View + // --> Invoke New Paired Device event to View + // 5. Set BT Operation state as Activated + internal void Pair() + { + bluetoothDevice.BondCreated += BluetoothDeviceBondCreated; + bluetoothDevice.CreateBond(); + deviceState = BtDeviceState.Pairing; + BtModel.NotifyDeviceChanged(this); + BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); + } + + private void BluetoothDeviceBondDestroyed(object obj, BondDestroyedEventArgs ev) + { + Log.Info(Program.LogTag, "BluetoothDeviceBondDestroyed. Address: " + ev.DeviceAddress + ", Result: " + ev.Result); + if (ev.Result == BluetoothError.None) + { + isPaired = false; + deviceState = BtDeviceState.Unpaired; + BtModel.NotifyDeviceChanged(this); + //BtModel.NotifyOperationStateChanged(BtOperationState.Activated); + } + bluetoothDevice.BondDestroyed -= BluetoothDeviceBondDestroyed; + } + + // TODO + // 1. Call DestroyBond API + // 2. Set BT Operation state as Pairing + // 3. Set Device state as Unpairing + // 4. Bonding Destroyed + // --> Remove device into paired list + // --> Invoke Remove Paired Device event to View + // 5. Set BT Operation state as Activated + internal void Unpair() + { + bluetoothDevice.BondDestroyed += BluetoothDeviceBondDestroyed; + bluetoothDevice.DestroyBond(); + deviceState = BtDeviceState.Unpairing; + BtModel.NotifyDeviceChanged(this); + BtModel.NotifyOperationStateChanged(BtOperationState.Pairing); } - internal static void ConnectAudio() + internal void ConnectAudio() { // TODO // 1. Call Connect API (Audio) @@ -53,7 +137,7 @@ namespace SettingBluetooth.Model // 5. Set BT Operation state as Activated } - internal static void DisConnectAudio() + internal void DisConnectAudio() { // TODO // 1. Call Disconnect API (Audio) @@ -65,12 +149,12 @@ namespace SettingBluetooth.Model // 5. Set BT Operation state as Activated } - internal static void ConnectHid() + internal void ConnectHid() { // TODO (Same with audio) } - internal static void DisConnectHid() + internal void DisConnectHid() { // TODO (Same with audio) } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtEnumerations.cs b/SettingBluetooth/SettingBluetooth/Model/BtEnumerations.cs index 03cb74d..264e426 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtEnumerations.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtEnumerations.cs @@ -4,7 +4,7 @@ using System.Text; namespace SettingBluetooth.Model { - public enum BtOperationState + internal enum BtOperationState { Deactivated, Deactivating, @@ -16,12 +16,13 @@ namespace SettingBluetooth.Model Connecting, } - public enum BtDeviceState + internal enum BtDeviceState { Idle, Pairing, Unpairing, Paired, + Unpaired, Connecting, Connected, Disconnecting, diff --git a/SettingBluetooth/SettingBluetooth/Model/BtEventArgs.cs b/SettingBluetooth/SettingBluetooth/Model/BtEventArgs.cs index ae58c37..3bbfb1f 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtEventArgs.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtEventArgs.cs @@ -8,21 +8,37 @@ namespace SettingBluetooth.Model { internal class BtOperationStateChangedEventArgs : EventArgs { - internal BtOperationState OperationState; + private BtOperationState operationState; internal BtOperationStateChangedEventArgs(BtOperationState state) { - OperationState = state; + operationState = state; + } + + internal BtOperationState OperationState + { + get + { + return operationState; + } } } internal class BtDeviceChangedEventArgs : EventArgs { - internal BtDevice btDevice; + private BtDevice btDevice; internal BtDeviceChangedEventArgs(BtDevice device) { btDevice = device; } + + internal BtDevice BtDevice + { + get + { + return btDevice; + } + } } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModel.cs b/SettingBluetooth/SettingBluetooth/Model/BtModel.cs index 37316d6..75c3ba1 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtModel.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtModel.cs @@ -67,5 +67,15 @@ namespace SettingBluetooth.Model BtModelImpl.Instance.DeviceChanged -= value; } } + + internal static void NotifyOperationStateChanged(BtOperationState state) + { + BtModelImpl.Instance.NotifyOperationStateChanged(state); + } + + internal static void NotifyDeviceChanged(BtDevice btDevice) + { + BtModelImpl.Instance.NotifyDeviceChanged(btDevice); + } } } diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs index c2a8234..3d6c3fc 100644 --- a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs +++ b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs @@ -71,14 +71,16 @@ namespace SettingBluetooth.Model { if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Found) { - BtDevice btDevice = new BtDevice(ev.DeviceFound.Address); + BtDevice btDevice; + //Log.Info(Program.LogTag, "address: " + ev.DeviceFound.Address); + //Log.Info(Program.LogTag, "name: " + ev.DeviceFound.Name); if (ev.DeviceFound.IsPaired) { - btDevice.DeviceState = BtDeviceState.Paired; + btDevice = new BtDevice(ev.DeviceFound, BtDeviceState.Paired); } else { - btDevice.DeviceState = BtDeviceState.Idle; + btDevice = new BtDevice(ev.DeviceFound, BtDeviceState.Idle); } BtDeviceChangedEventArgs args = new BtDeviceChangedEventArgs(btDevice); @@ -115,6 +117,16 @@ namespace SettingBluetooth.Model } } + internal void NotifyOperationStateChanged(BtOperationState state) + { + operationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(state)); + } + + internal void NotifyDeviceChanged(BtDevice btDevice) + { + deviceChanged?.Invoke(null, new BtDeviceChangedEventArgs(btDevice)); + } + private void Initialize() { BluetoothAdapter.StateChanged += AdapterStateChanged; diff --git a/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs b/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs index 7c639b9..f2c8219 100644 --- a/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs +++ b/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs @@ -6,19 +6,20 @@ using Tizen.NUI.BaseComponents; using Tizen.NUI.Components; using Tizen.NUI.Binding; using Tizen; -using Tizen.Network.Bluetooth; using SettingBluetooth.res.locale; +using SettingBluetooth.Model; namespace SettingBluetooth { public class Device : INotifyPropertyChanged { string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; + private string address; private string name; - private bool connected; - private bool registered; - private BluetoothAppearanceType appearance; + //private bool connected; + //private bool registered; public event PropertyChangedEventHandler PropertyChanged; + BtDevice btItem; private void OnPropertyChanged(string propertyName) { @@ -28,61 +29,78 @@ namespace SettingBluetooth public Device(string deviceName, bool con, bool reg) { name = deviceName; - connected = con; - registered = reg; + //connected = con; + //registered = reg; } - public Device(string deviceName) // BtDevice + internal Device(BtDevice btDevice) { - Log.Debug(Program.LogTag, "Device added. deviceName: " + deviceName); - name = deviceName; + address = btDevice.Address; + name = btDevice.Name; + btItem = btDevice; } - public string Name + internal BtDevice BtItem { get { - return name; - } - set - { - name = value; - OnPropertyChanged("Name"); + return btItem; } } - public string ImageUrl + public string Address { get { - return iconDir; + return address; } } - public bool Connected + public string Name { get { - return connected; + return name; } set { - connected = value; - OnPropertyChanged("Connected"); + name = value; + OnPropertyChanged("Name"); } } - public bool Registered + + public string ImageUrl { get { - return registered; - } - set - { - registered = value; - OnPropertyChanged("Registered"); + return iconDir; } } + + //public bool Connected + //{ + // get + // { + // return connected; + // } + // set + // { + // connected = value; + // OnPropertyChanged("Connected"); + // } + //} + //public bool Registered + //{ + // get + // { + // return registered; + // } + // set + // { + // registered = value; + // OnPropertyChanged("Registered"); + // } + //} } public class DeviceCollection : ObservableCollection @@ -107,19 +125,20 @@ namespace SettingBluetooth } } - public void AddDevice(string deviceName) + internal void AddDevice(BtDevice btDevice) { - this.Add(new Device(deviceName)); + this.Add(new Device(btDevice)); + Log.Info(Program.LogTag, "device(" + btDevice.Address + ") added"); } - public void RemoveDevice(string deviceName) + internal void RemoveDevice(string address) { foreach (Device device in this) { - if (device.Name == deviceName) + if (device.Address == address) { this.Remove(device); - Log.Info(Program.LogTag, "device(" + deviceName + ") removed"); + Log.Info(Program.LogTag, "device(" + address + ") removed"); break; } } @@ -137,31 +156,32 @@ namespace SettingBluetooth this.Add(collection); } - public void UpdateTitle(string title) + internal void UpdateTitle(string title) { collection.Title = title; } - public void AddDevice(string deviceName) // BtDevice + internal void AddDevice(BtDevice btDevice) { - collection.AddDevice(deviceName); + collection.AddDevice(btDevice); } - public void RemoveDevice(string deviceName) + internal void RemoveDevice(string address) { - collection.RemoveDevice(deviceName); + collection.RemoveDevice(address); } - public Device FindDevice(string deviceName) + internal Device FindDevice(string address) { foreach (Device device in collection) { - if (device.Name == deviceName) + if (device.Address == address) { - Log.Info(Program.LogTag, "Device(" + deviceName + ") found"); + Log.Info(Program.LogTag, "Device(" + address + ") found"); return device; } } + Log.Info(Program.LogTag, "Device(" + address + ") not found"); return null; } } diff --git a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs index 7ba8be0..3c1daf7 100644 --- a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs +++ b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs @@ -21,14 +21,14 @@ namespace SettingBluetooth static CollectionView pairedDeviceView = null; static CollectionView searchedDeviceView = null; static Button scanButton = null; - static BtOperationState operationState; - static DeviceSource searchedDevice; - static DeviceSource pairedDevice; + static DeviceSource searchedDevice = null; + static DeviceSource pairedDevice = null; public BtMainView() : base() { } + // TODO: Need to move device view related functions to BtDeviceView.cs. private static CollectionView CreateCollectionView(DeviceSource source, bool fixHeight = true) { var view = new CollectionView() @@ -90,20 +90,20 @@ namespace SettingBluetooth return view; } - internal static void AddPairedDeviceView() + private static void AddPairedDeviceView() { pairedDevice = new DeviceSource(); pairedDeviceView = CreateCollectionView(pairedDevice, false); - pairedDevice.AddDevice("Paired device"); // temporary device + pairedDevice.AddDevice(new BtDevice("dummy paired device")); // temporary device pairedDevice.UpdateTitle(Resources.IDS_BT_BODY_PAIRED_DEVICES); mainView.Add(pairedDeviceView); } - internal static void AddSearchedDeviceView() + private static void AddSearchedDeviceView() { searchedDevice = new DeviceSource(); searchedDeviceView = CreateCollectionView(searchedDevice); - searchedDevice.AddDevice("No devices found"); // temporary device + searchedDevice.AddDevice(new BtDevice("dummy searched device")); // temporary device mainView.Add(searchedDeviceView); scanButton = new Button @@ -120,36 +120,39 @@ namespace SettingBluetooth // AddSearchedDeviceView(); //} - internal static void UpdateSearchedViewTitle(string title) + private static void UpdateSearchedViewTitle(string title) { searchedDevice.UpdateTitle(title); } - internal static void UpdateScanButtonText(string text) + private static void UpdateScanButtonText(string text) { scanButton.Text = text; } - internal static void RemoveSearchedDeviceView() + private static void RemoveSearchedDeviceView() { if (searchedDeviceView) { mainView.Remove(searchedDeviceView); + searchedDeviceView = null; } if (scanButton) { mainView.Remove(scanButton); + scanButton = null; } } - internal static void RemoveAllView() + private static void RemoveAllView() { RemoveSearchedDeviceView(); if (pairedDeviceView) { mainView.Remove(pairedDeviceView); + pairedDeviceView = null; } } @@ -164,8 +167,16 @@ namespace SettingBluetooth case BtOperationState.Deactivating: break; case BtOperationState.Activated: - AddPairedDeviceView(); - AddSearchedDeviceView(); + if (pairedDeviceView == null && searchedDeviceView == null) + { + Log.Info(Program.LogTag, "Add initial view"); + AddPairedDeviceView(); + AddSearchedDeviceView(); + } + else + { + Log.Info(Program.LogTag, "Do nothing"); + } break; case BtOperationState.Activating: break; @@ -188,22 +199,62 @@ namespace SettingBluetooth private static void BtModelDeviceChanged(object obj, BtDeviceChangedEventArgs ev) { - Log.Debug(Program.LogTag, "BtModelDeviceChanged. Address: " + ev.btDevice.RemoteDeviceAddress + ", State: " + ev.btDevice.DeviceState); - if (ev.btDevice.DeviceState == BtDeviceState.Paired) - { - if (pairedDevice.FindDevice("Paired device") != null) - { - pairedDevice.RemoveDevice("Paired device"); - } - pairedDevice.AddDevice(ev.btDevice.RemoteDeviceAddress); - } - else if (ev.btDevice.DeviceState == BtDeviceState.Idle) + Log.Debug(Program.LogTag, "BtModelDeviceChanged. Address: " + ev.BtDevice.Address + ", State: " + ev.BtDevice.DeviceState); + switch (ev.BtDevice.DeviceState) { - if (searchedDevice.FindDevice("No devices found") != null) - { - searchedDevice.RemoveDevice("No devices found"); - } - searchedDevice.AddDevice(ev.btDevice.RemoteDeviceAddress); + case BtDeviceState.Idle: + //if (searchedDevice.FindDevice("dummy searched device") != null) + //{ + // searchedDevice.RemoveDevice("dummy searched device"); + //} + + if (ev.BtDevice.IsPaired) + { + pairedDevice.AddDevice(ev.BtDevice); + } + else + { + searchedDevice.AddDevice(ev.BtDevice); + } + break; + case BtDeviceState.Pairing: + break; + case BtDeviceState.Unpairing: + //pairedDevice.RemoveDevice(ev.BtDevice.Address); + break; + case BtDeviceState.Paired: + //if (pairedDevice.FindDevice("dummy paired device") != null) + //{ + // pairedDevice.RemoveDevice("dummy paired device"); + //} + + if (searchedDevice.FindDevice(ev.BtDevice.Address) != null) + { + searchedDevice.RemoveDevice(ev.BtDevice.Address); + } + if (pairedDevice.FindDevice(ev.BtDevice.Address) == null) + { + pairedDevice.AddDevice(ev.BtDevice); + } + break; + case BtDeviceState.Unpaired: + if (searchedDevice.FindDevice(ev.BtDevice.Address) == null) + { + searchedDevice.AddDevice(ev.BtDevice); + } + if (pairedDevice.FindDevice(ev.BtDevice.Address) != null) + { + pairedDevice.RemoveDevice(ev.BtDevice.Address); + } + break; + case BtDeviceState.Connecting: + break; + case BtDeviceState.Connected: + break; + case BtDeviceState.Disconnecting: + break; + case BtDeviceState.ServiceSearching: + break; } } diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs b/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs index 2f205a5..ba06b95 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.Designer.cs @@ -78,6 +78,15 @@ namespace SettingBluetooth.res.locale { } } + /// + /// 과(와) 유사한 지역화된 문자열을 찾습니다. + /// + public static string IDS_BT_BODY_PAIRED_DEVICES { + get { + return ResourceManager.GetString("IDS_BT_BODY_PAIRED_DEVICES", resourceCulture); + } + } + /// /// 과(와) 유사한 지역화된 문자열을 찾습니다. /// diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx index df03170..eb5c9ed 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.en.resx @@ -123,6 +123,9 @@ Bluetooth + + Paired devices + Scanning for devices... diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx index 5b425a5..0f082d9 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.ko-KR.resx @@ -123,6 +123,9 @@ 블루투스 + + 등록된 디바이스 + 디바이스 찾는 중... @@ -132,4 +135,7 @@ 중지 + + + \ No newline at end of file diff --git a/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx b/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx index 7153687..aa3c956 100644 --- a/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx +++ b/SettingBluetooth/SettingBluetooth/res/locale/Resources.resx @@ -123,6 +123,9 @@ + + + diff --git a/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk b/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk index 2013b84..33a3398 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