From: chaehee-hong Date: Tue, 4 Oct 2022 03:01:45 +0000 (+0900) Subject: Add WiFi activate/deactivate/scan X-Git-Tag: accepted/tizen/unified/20221216.024031~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0f132aadb4531dbbc441cb802a23777f53113749;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-wifi.git Add WiFi activate/deactivate/scan Change-Id: I8db907f173f227cc32274a23e306c89773b7af01 --- diff --git a/SettingWiFi/SettingWiFi/AP.cs b/SettingWiFi/SettingWiFi/AP.cs new file mode 100755 index 0000000..dd45e47 --- /dev/null +++ b/SettingWiFi/SettingWiFi/AP.cs @@ -0,0 +1,145 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Tizen; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; +using Tizen.NUI.Binding; +using Tizen.Network.WiFi; + + +namespace SettingWiFi +{ + public class AP : INotifyPropertyChanged + { + internal static readonly string LogTag = "SettingWiFi"; + string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; + private string essid; + private string state; + public event PropertyChangedEventHandler PropertyChanged; + + private void OnPropertyChanged(string propertyName) + { + Log.Debug(LogTag, "OnPropertyChanged"); + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public AP(string apEssid, string apState) + { + essid = apEssid; + state = apState; + } + + public string Essid + { + get + { + return essid; + } + set + { + essid = value; + OnPropertyChanged("Essid"); + } + } + + public string ImageUrl + { + get + { + return iconDir; + } + } + + public string State + { + get + { + return state; + } + set + { + state = value; + OnPropertyChanged("State"); + } + } + }; + + + public class DeviceCollection : ObservableCollection + { + internal static readonly string LogTag = "SettingWiFi"; + private string title; + + public DeviceCollection(string groupTitle) + { + title = groupTitle; + this.UpdateDevices(null); + } + + public string Title + { + get + { + return title; + } + set + { + title = value; + OnPropertyChanged(new PropertyChangedEventArgs("Title")); + } + } + + public void UpdateDevices(List apList) + { + Log.Debug(LogTag, "UpdateDevices"); + if (apList == null) + { + this.Add(new AP("", "")); + return; + } + // Clear method have some issue about asynchronous actions, + // so call Remove for all item is recommanded. + while (this.Count > 0) + { + this.RemoveAt(this.Count - 1); + } + + foreach (var item in apList) + { + this.Add(new AP(item.Essid, item.State)); + } + } + + public void RemoveDevices() + { + while (this.Count > 0) + { + this.RemoveAt(this.Count - 1); + } + } + } + + public class APSource : ObservableCollection + { + internal static readonly string LogTag = "SettingWiFi"; + private DeviceCollection available; + public APSource() + { + Log.Debug(LogTag, "new APSource"); + available = new DeviceCollection("Available networks"); + this.Add(available); + Log.Debug(LogTag, "Add DeviceCollection"); + } + public void UpdateDevices(List apList) + { + available.UpdateDevices(apList); + } + + public void RemoveDevices() + { + available.RemoveDevices(); + } + } +} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/Device.cs b/SettingWiFi/SettingWiFi/Device.cs deleted file mode 100755 index 7862484..0000000 --- a/SettingWiFi/SettingWiFi/Device.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using Tizen.NUI.BaseComponents; -using Tizen.NUI.Binding; -using Tizen.NUI.Components; - -namespace SettingWiFi -{ - public class Device : INotifyPropertyChanged - { - string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; - private string name; - private bool connected; - private bool registered; - public event PropertyChangedEventHandler PropertyChanged; - - private void OnPropertyChanged(string propertyName) - { - - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - public Device(string deviceName, bool con, bool reg) - { - name = deviceName; - connected = con; - registered = reg; - } - - public string Name - { - get - { - return name; - } - set - { - name = value; - OnPropertyChanged("Name"); - } - } - - public string ImageUrl - { - get - { - 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 - { - string[] devicePool = { - "Galaxy Buds2 Pro", - "Galaxy Fold 4", - "Galaxy Tab S8+", - "Galaxy Buds2", - "Galaxy Watch 5", - "[TV] Samsung 65", - "Galaxy Fit2", - "Sony WH-100XM5", - "Logitech MX Vertical", - "Ioniq 5", - "Galaxy Home Mini", - "Samsung Family Hub", - "Galaxy Book Pro2", - }; - - private string title; - - public DeviceCollection(string groupTitle) - { - title = groupTitle; - UpdateDevices(); - } - - public string Title - { - get - { - return title; - } - set - { - title = value; - OnPropertyChanged(new PropertyChangedEventArgs("Title")); - } - } - - public void UpdateDevices() - { - // Clear method have some issue about asynchronous actions, - // so call Remove for all item is recommanded. - while (this.Count > 0) - { - this.RemoveAt(this.Count - 1); - } - Random rand = new Random(); - int count = rand.Next(13); - - for (int i = 0; i < count; i++) - { - this.Add(new Device(devicePool[rand.Next(13)], false, false)); - } - } - } - - public class DeviceSource : ObservableCollection - { - private DeviceCollection available; - public DeviceSource() - { - available = new DeviceCollection("Available networks"); - this.Add(available); - } - - public void UpdateDevices() - { - available.UpdateDevices(); - } - } -} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/WiFi.cs b/SettingWiFi/SettingWiFi/WiFi.cs new file mode 100755 index 0000000..3d47b40 --- /dev/null +++ b/SettingWiFi/SettingWiFi/WiFi.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tizen; +using Tizen.Network.WiFi; + +namespace SettingWiFi +{ + public class WiFi + { + internal static readonly string LogTag = "SettingWiFi"; + IEnumerable apList = null; + + public async Task Activate() + { + try + { + await WiFiManager.ActivateAsync(); + if (WiFiManager.IsActive) + { + Log.Debug(LogTag, "WiFi is activated"); + apList = new List(); + } + } + catch (Exception e) + { + Log.Debug(LogTag, "Exception occurred" + e.ToString()); + } + } + + public Task Deactivate() + { + return WiFiManager.DeactivateAsync(); + } + + public async Task Scan() + { + try + { + await WiFiManager.ScanAsync(); + } + catch (Exception e) + { + Log.Debug(LogTag, "Exception occurred" + e.ToString()); + } + + } + + public List GetScanResult() + { + try + { + apList = WiFiManager.GetFoundAPs(); + + List apInfoList = new List(); + foreach (var item in apList) + { + Log.Debug(LogTag, "AP name: " + item.NetworkInformation.Essid); + //Log.Debug(LogTag, "Connection state: " + item.NetworkInformation.ConnectionState); + apInfoList.Add(new AP(item.NetworkInformation.Essid, item.NetworkInformation.ConnectionState.ToString())); + } + + return apInfoList; + } + catch (Exception e) + { + Log.Debug(LogTag, "Exception occurred" + e.ToString()); + } + + return null; + } + + public async Task Connect(string essid) + { + WiFiAP ap = null; + apList = WiFiManager.GetFoundAPs(); + + foreach (var item in apList) + { + if (item.NetworkInformation.Essid.Equals(essid)) + { + ap = item; + break; + } + } + + try + { + await ap.ConnectAsync(); + } + catch (Exception e) + { + Log.Debug(LogTag, "Fail to connect" + e.ToString()); + } + } + + + public bool IsActive() + { + if (WiFiManager.IsActive) + { + return true; + } + + return false; + } + } +} diff --git a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs b/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs index 2e3a294..ca3dfbb 100755 --- a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs +++ b/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs @@ -1,24 +1,21 @@ -using System; -using Tizen; -using Tizen.Applications; -using Tizen.NUI; -using Tizen.NUI.Accessibility; -using Tizen.NUI.BaseComponents; -using Tizen.NUI.Binding; -using Tizen.NUI.Components; -using Tizen.Network.WiFi; - - -namespace SettingWiFi -{ - internal class WidgetSettingWiFi : Widget - { - internal static readonly string LogTag = "SettingWiFi"; - Navigator navigator; - ContentPage page; - View rootView; - CollectionView deviceView; - DeviceSource deviceSource; +using System; +using System.Collections.Generic; +using Tizen; +using Tizen.Applications; +using Tizen.NUI; +using Tizen.NUI.Accessibility; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Binding; +using Tizen.NUI.Components; + + +namespace SettingWiFi +{ + internal class WidgetSettingWiFi : Widget + { + internal static readonly string LogTag = "SettingWiFi"; + WiFi wifi; + APSource apSource; private RecyclerViewItem GetHeader() { @@ -32,51 +29,35 @@ namespace SettingWiFi HeightSpecification = LayoutParamPolicies.WrapContent, }; - var wifiOnOff = new DefaultLinearItem() + var toggle = new DefaultLinearItem() { WidthSpecification = LayoutParamPolicies.MatchParent, Text = "Wi-Fi", + IsSelectable = false, }; - wifiOnOff.Label.HorizontalAlignment = HorizontalAlignment.Begin; - var wifiOnOffSwith = new Switch(); - wifiOnOffSwith.SelectedChanged += (object obj, SelectedChangedEventArgs ev) => - { - Tizen.Log.Debug("NUI", "Wi-Fi On&Off Switch changed"); - if (obj != null && obj is Switch switchButton) - { - var parent = switchButton.GetParent(); - if (parent is DefaultLinearItem item) - { - item.IsSelected = switchButton.IsSelected; - } - } - }; - wifiOnOff.Extra = wifiOnOffSwith; - /* - var myDeviceName = new DefaultLinearItem() - { - WidthSpecification = LayoutParamPolicies.MatchParent, - Text = "Tizen", - }; - myDeviceName.Label.HorizontalAlignment = HorizontalAlignment.Begin; - */ - header.Add(wifiOnOff); - //header.Add(myDeviceName); + toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin; + + var onOffSwitch = new Switch(); + toggle.Extra = onOffSwitch; + onOffSwitch.SelectedChanged += OnSelectedChanged; + header.Add(toggle); return header; - } - - protected override void OnCreate(string contentInfo, Window window) - { - Bundle bundle = Bundle.Decode(contentInfo); - navigator = window.GetDefaultNavigator(); - + } + + protected override void OnCreate(string contentInfo, Window window) + { + Bundle bundle = Bundle.Decode(contentInfo); + Navigator navigator = window.GetDefaultNavigator(); + wifi = new WiFi(); + apSource = new APSource(); + var appBar = new AppBar() { Title = "Wi-Fi", - }; - - rootView = new View() + }; + + View rootView = new View() { Layout = new LinearLayout() { @@ -85,21 +66,18 @@ namespace SettingWiFi }, WidthSpecification = LayoutParamPolicies.MatchParent, HeightSpecification = LayoutParamPolicies.MatchParent, - }; - - deviceSource = new DeviceSource(); + }; - deviceView = new CollectionView() + CollectionView deviceView = new CollectionView() { - ItemsSource = deviceSource, + ItemsSource = apSource, ItemsLayouter = new LinearLayouter(), ItemTemplate = new DataTemplate(() => { DefaultLinearItem item = new DefaultLinearItem(); - //Set Width Specification as MatchParent to fit the Item width with parent View. item.WidthSpecification = LayoutParamPolicies.MatchParent; //Decorate Label - item.Label.SetBinding(TextLabel.TextProperty, "Name"); + item.Label.SetBinding(TextLabel.TextProperty, "Essid"); item.Label.HorizontalAlignment = HorizontalAlignment.Begin; //Decorate Icon item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl"); @@ -111,7 +89,6 @@ namespace SettingWiFi GroupHeaderTemplate = new DataTemplate(() => { DefaultTitleItem group = new DefaultTitleItem(); - //Set Width Specification as MatchParent to fit the Item width with parent View. group.WidthSpecification = LayoutParamPolicies.MatchParent; group.Label.SetBinding(TextLabel.TextProperty, "Title"); @@ -119,48 +96,100 @@ namespace SettingWiFi return group; }), - Header = GetHeader(), IsGrouped = true, ScrollingDirection = ScrollableBase.Direction.Vertical, WidthSpecification = LayoutParamPolicies.MatchParent, HeightSpecification = LayoutParamPolicies.MatchParent, SelectionMode = ItemSelectionMode.Single, }; - deviceView.SelectionChanged += DeviceConnectedEvent; - - deviceSource.UpdateDevices(); - rootView.Add(deviceView); - - page = new ContentPage() + deviceView.SelectionChanged += DeviceConnectedEvent; + + var header = GetHeader(); + var scanButton = new Button() + { + Text = "Scan", + WidthSpecification = 300, + HeightSpecification = 80, + }; + scanButton.Clicked += OnScanButtonClicked; + + rootView.Add(header); + rootView.Add(deviceView); + rootView.Add(scanButton); + + ContentPage page = new ContentPage() { AppBar = appBar, Content = rootView, }; - navigator.Push(page); + navigator.Push(page); + } + + + /* Call WiFi */ + private async void OnSelectedChanged(object sender, SelectedChangedEventArgs e) + { + if (e.IsSelected) + { + if (!IsWiFiActive()) + { + await wifi.Activate(); + } + ScanAP(); + } + else + { + apSource.RemoveDevices(); + + if (IsWiFiActive()) + { + await wifi.Deactivate(); + } + } } - public void DeviceConnectedEvent(object sender, SelectionChangedEventArgs ev) + + private void OnScanButtonClicked(object sender, ClickedEventArgs e) { - Tizen.Log.Debug("NUI", "DeviceConnectedEvent called"); + Log.Debug(LogTag, "OnScanButtonClicked"); + ScanAP(); + } + private bool IsWiFiActive() + { + return wifi.IsActive(); + } + private async void ScanAP() + { + if (IsWiFiActive()) + { + await wifi.Scan(); + List apList = wifi.GetScanResult(); + apSource.UpdateDevices(apList); + } + } + + public async void DeviceConnectedEvent(object sender, SelectionChangedEventArgs ev) + { //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) + if (item is AP device) { - device.Connected = false; + // device.Connected = false; } } foreach (object item in ev.CurrentSelection) { if (item == null) break; - if (item is Device device) + if (item is AP device) { - device.Connected = true; - device.Registered = true; + await wifi.Connect(device.Essid); + // device.Connected = true; + // device.Registered = true; } } - } - } -} + } + } +} diff --git a/packaging/org.tizen.cssetting-wifi-1.0.0.tpk b/packaging/org.tizen.cssetting-wifi-1.0.0.tpk index 83470a8..4696b32 100755 Binary files a/packaging/org.tizen.cssetting-wifi-1.0.0.tpk and b/packaging/org.tizen.cssetting-wifi-1.0.0.tpk differ