--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+using SettingBluetooth.Model;
+using SettingBluetooth;
+using Tizen;
+using Tizen.NUI.Components;
+using SettingBluetooth.res.locale;
+
+namespace SettingBluetooth.Controller
+{
+ internal static class AdapterController
+ {
+ static bool deviceViewInit = false;
+ static DeviceSource deviceSource;
+
+ internal static void BtModelDiscoveryStateChanged(object obj, BtModelDiscoveryStateChangedEventArgs ev)
+ {
+ if (ev.DiscoveryState == BtModelDiscoveryState.Started)
+ {
+ Log.Debug(Program.LogTag, "Device discovery started");
+ }
+ else if (ev.DiscoveryState == BtModelDiscoveryState.Found)
+ {
+ if (deviceViewInit == false)
+ {
+ deviceSource = new DeviceSource();
+ BtMainView.AddDeviceView(deviceSource);
+ BtMainView.AddScanButton();
+ deviceViewInit = true;
+ }
+
+ Log.Debug(Program.LogTag, "Device found. Address: " + ev.DiscoveredDevice.RemoteDeviceAddress);
+ deviceSource.AddDevice(ev.DiscoveredDevice.RemoteDeviceAddress);
+ }
+ else
+ {
+ Log.Debug(Program.LogTag, "Device discovery finished");
+ deviceSource.UpdateTitle(Resources.IDS_BT_BODY_AVAILABLE_DEVICES);
+ BtMainView.UpdateScanButton(Resources.IDS_BT_SK_SCAN);
+ }
+ }
+
+ internal static void BtModelStateChanged(object obj, BtModelStateChangedEventArgs ev)
+ {
+ if (ev.IsEnabled)
+ {
+ Log.Debug(Program.LogTag, "BT enabled. Start discovery");
+ BtModel.DiscoveryStateChanged += BtModelDiscoveryStateChanged;
+ BtModel.StartDiscovery();
+ }
+ else
+ {
+ Log.Debug(Program.LogTag, "BT disabled");
+ BtMainView.RemoveDeviceView();
+ BtMainView.RemoveScanButton();
+ }
+ }
+
+ internal static void OnOffSwitchSelectedChanged(object obj, SelectedChangedEventArgs ev)
+ {
+ Log.Debug(Program.LogTag, "btOnOffSwitch clicked. IsSelected: " + ev.IsSelected + ", isEnabled: " + BtModel.IsEnabled);
+ if (ev.IsSelected == true && BtModel.IsEnabled == false)
+ {
+ Log.Info(Program.LogTag, "Enable BT");
+ BtModel.StateChanged += BtModelStateChanged;
+ BtModel.Enable();
+ }
+ else if (ev.IsSelected == false && BtModel.IsEnabled == true)
+ {
+ Log.Info(Program.LogTag, "Disable BT");
+ BtModel.Disable();
+ }
+ else
+ {
+ Log.Error(Program.LogTag, "Invalid state");
+ }
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Controller
+{
+ class DeviceController
+ {
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Controller
+{
+ class ViewController
+ {
+ }
+}
+++ /dev/null
-using System;
-using System.ComponentModel;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using Tizen.NUI.BaseComponents;
-using Tizen.NUI.Components;
-using Tizen.NUI.Binding;
-using Tizen;
-using Tizen.Network.Bluetooth;
-using SettingBluetooth.res.locale;
-
-namespace SettingBluetooth
-{
- public class Device : INotifyPropertyChanged
- {
- string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";
- private string name;
- private bool connected;
- private bool registered;
- private BluetoothAppearanceType appearance;
- 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 Device(string deviceName, BluetoothAppearanceType deviceAppearance)
- {
- Log.Debug(Program.LogTag, "Device added. deviceName: " + deviceName + ", deviceAppearance: " + deviceAppearance);
- name = deviceName;
- appearance = deviceAppearance;
- }
-
- 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 BluetoothAppearanceType Appearance
- {
- get
- {
- return appearance;
- }
- set
- {
- appearance = value;
- OnPropertyChanged("Appearance");
- }
- }
- };
-
- public class DeviceCollection : ObservableCollection<Device>
- {
- string[] devicePool = {
- "Galaxy Buds2 Pro",
- "Galaxy Fold 4",
- };
-
- 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.
- Log.Info(Program.LogTag, "count: " + this.Count + ", pool count: " + devicePool.Length);
- while (this.Count > 0)
- {
- this.RemoveAt(this.Count - 1);
- }
-
- int count = devicePool.Length;
-
- for (int i = 0; i < count; i++)
- {
- this.Add(new Device(devicePool[i], false, false));
- }
- }
-
- public void AddDevice(string deviceName, BluetoothAppearanceType deviceAppearance)
- {
- this.Add(new Device(deviceName, deviceAppearance));
- }
- }
-
- public class DeviceSource : ObservableCollection<DeviceCollection>
- {
- private DeviceCollection collection;
-
- public DeviceSource()
- {
- Log.Info(Program.LogTag, "DeviceSource created");
- collection = new DeviceCollection(Resources.IDS_BT_BODY_SCANNING_FOR_DEVICES_ING);
- this.Add(collection);
- }
-
- public void UpdateTitle(string title)
- {
- collection.Title = title;
- }
-
- public void AddDevice(string deviceName, BluetoothAppearanceType deviceAppearance)
- {
- collection.AddDevice(deviceName, deviceAppearance);
- }
-
- public int GetCount()
- {
- Log.Debug(Program.LogTag, "count: " + collection.Count);
- return collection.Count;
- }
-
- public void UpdateDevices()
- {
- collection.UpdateDevices();
- }
- }
-}
\ No newline at end of file
--- /dev/null
+using SettingBluetooth.Controller;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen;
+using Tizen.Network.Bluetooth;
+
+namespace SettingBluetooth.Model
+{
+ internal static class BtModel
+ {
+ internal static bool IsEnabled
+ {
+ get
+ {
+ return BtModelImpl.Instance.IsEnabled;
+ }
+ }
+
+ internal static void Enable()
+ {
+ BtModelImpl.Instance.Enable();
+ }
+
+ internal static void Disable()
+ {
+ BtModelImpl.Instance.Disable();
+ }
+
+ internal static void StartDiscovery()
+ {
+ BtModelImpl.Instance.StartDiscovery();
+ }
+
+ internal static event EventHandler<BtModelStateChangedEventArgs> StateChanged
+ {
+ add
+ {
+ BtModelImpl.Instance.StateChanged += value;
+ }
+ remove
+ {
+ BtModelImpl.Instance.StateChanged -= value;
+ }
+ }
+
+ internal static event EventHandler<BtModelDiscoveryStateChangedEventArgs> DiscoveryStateChanged
+ {
+ add
+ {
+ BtModelImpl.Instance.DiscoveryStateChanged += value;
+ }
+ remove
+ {
+ BtModelImpl.Instance.DiscoveryStateChanged -= value;
+ }
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Model
+{
+ public class BtModelDevice
+ {
+ internal string RemoteDeviceAddress;
+ // TODO
+
+ internal BtModelDevice(string address) // TODO
+ {
+ RemoteDeviceAddress = address;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Model
+{
+ public enum BtModelDiscoveryState
+ {
+ Started,
+ Finished,
+ Found
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen.Network.Bluetooth;
+using Tizen;
+
+namespace SettingBluetooth.Model
+{
+ internal class BtModelStateChangedEventArgs : EventArgs
+ {
+ internal bool IsEnabled;
+
+ internal BtModelStateChangedEventArgs(StateChangedEventArgs args)
+ {
+ IsEnabled = args.BTState == BluetoothState.Enabled;
+ }
+ }
+
+ internal class BtModelDiscoveryStateChangedEventArgs : EventArgs
+ {
+ internal BtModelDiscoveryState DiscoveryState;
+ internal BtModelDevice DiscoveredDevice;
+
+ internal BtModelDiscoveryStateChangedEventArgs(BluetoothDeviceDiscoveryState state)
+ {
+ DiscoveryState = (BtModelDiscoveryState)state;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen;
+using Tizen.Network.Bluetooth;
+
+namespace SettingBluetooth.Model
+{
+ internal class BtModelImpl
+ {
+ static readonly BtModelImpl instance = new BtModelImpl();
+ static event EventHandler<BtModelStateChangedEventArgs> stateChanged = null;
+ static event EventHandler<BtModelDiscoveryStateChangedEventArgs> discoveryStateChanged = null;
+
+ internal bool IsEnabled
+ {
+ get
+ {
+ return BluetoothAdapter.IsBluetoothEnabled;
+ }
+ }
+
+ internal void Enable()
+ {
+ BluetoothAdapter.Enable();
+ }
+
+ internal void Disable()
+ {
+ BluetoothAdapter.Disable();
+ }
+
+ internal void StartDiscovery()
+ {
+ BluetoothAdapter.StartDiscovery();
+ }
+
+ private static void AdapterStateChanged(object obj, StateChangedEventArgs ev)
+ {
+ stateChanged?.Invoke(null, new BtModelStateChangedEventArgs(ev));
+ }
+
+ internal event EventHandler<BtModelStateChangedEventArgs> StateChanged
+ {
+ add
+ {
+ stateChanged += value;
+ }
+ remove
+ {
+ stateChanged -= value;
+ }
+ }
+
+ private static void AdapterDiscoveryChanged(object obj, DiscoveryStateChangedEventArgs ev)
+ {
+ BtModelDiscoveryStateChangedEventArgs args = new BtModelDiscoveryStateChangedEventArgs(ev.DiscoveryState);
+
+ if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Started)
+ {
+ discoveryStateChanged?.Invoke(null, args);
+ }
+ else if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Finished)
+ {
+ discoveryStateChanged?.Invoke(null, args);
+ }
+ else
+ {
+ BtModelDevice device = new BtModelDevice(ev.DeviceFound.Address);
+ args.DiscoveredDevice = device;
+ }
+
+ discoveryStateChanged?.Invoke(null, args);
+ }
+
+ internal event EventHandler<BtModelDiscoveryStateChangedEventArgs> DiscoveryStateChanged
+ {
+ add
+ {
+ discoveryStateChanged += value;
+ }
+ remove
+ {
+ discoveryStateChanged -= value;
+ }
+ }
+
+ private void Initialize()
+ {
+ BluetoothAdapter.StateChanged += AdapterStateChanged;
+ BluetoothAdapter.DiscoveryStateChanged += AdapterDiscoveryChanged;
+ }
+
+ internal static BtModelImpl Instance
+ {
+ get
+ {
+ return instance;
+ }
+ }
+
+ private BtModelImpl()
+ {
+ Initialize();
+ }
+ }
+}
static void Main(string[] args)
{
Dictionary<System.Type, string> widgetSet = new Dictionary<Type, string>();
- widgetSet.Add(typeof(WidgetSettingBluetooth), "bluetooth@org.tizen.cssetting-bluetooth");
+ widgetSet.Add(typeof(BtMainView), "bluetooth@org.tizen.cssetting-bluetooth");
var app = new Program(widgetSet);
app.Run(args);
}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth
+{
+ class BtDetailView
+ {
+ }
+}
--- /dev/null
+using System;
+using System.ComponentModel;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+using Tizen.NUI.Binding;
+using Tizen;
+using Tizen.Network.Bluetooth;
+using SettingBluetooth.res.locale;
+
+namespace SettingBluetooth
+{
+ public class Device : INotifyPropertyChanged
+ {
+ string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";
+ private string name;
+ private bool connected;
+ private bool registered;
+ private BluetoothAppearanceType appearance;
+ 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 Device(string deviceName)
+ {
+ Log.Debug(Program.LogTag, "Device added. deviceName: " + deviceName);
+ name = deviceName;
+ }
+
+ 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<Device>
+ {
+ private string title;
+
+ public DeviceCollection(string groupTitle)
+ {
+ title = groupTitle;
+ }
+
+ public string Title
+ {
+ get
+ {
+ return title;
+ }
+ set
+ {
+ title = value;
+ OnPropertyChanged(new PropertyChangedEventArgs("Title"));
+ }
+ }
+
+ public void AddDevice(string deviceName)
+ {
+ this.Add(new Device(deviceName));
+ }
+ }
+
+ public class DeviceSource : ObservableCollection<DeviceCollection>
+ {
+ private DeviceCollection collection;
+
+ public DeviceSource()
+ {
+ Log.Info(Program.LogTag, "DeviceSource created");
+ collection = new DeviceCollection(Resources.IDS_BT_BODY_SCANNING_FOR_DEVICES_ING);
+ this.Add(collection);
+ }
+
+ public void UpdateTitle(string title)
+ {
+ collection.Title = title;
+ }
+
+ public void AddDevice(string deviceName)
+ {
+ collection.AddDevice(deviceName);
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Tizen.Applications;
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+using Tizen.Network.Bluetooth;
+using Tizen;
+using SettingBluetooth.res.locale;
+using Tizen.NUI.Binding;
+using SettingBluetooth.Model;
+using SettingBluetooth.Controller;
+
+namespace SettingBluetooth
+{
+ class BtMainView : Widget
+ {
+ static ContentPage mainPage;
+ static View mainView;
+ static CollectionView deviceView = null;
+ static Button scanButton = null;
+
+ public BtMainView() : base()
+ {
+ }
+
+ public static void DeviceSelectionChanged(object obj, SelectionChangedEventArgs ev)
+ {
+ Log.Debug(Program.LogTag, "DeviceSelectionChanged called");
+
+ //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)
+ {
+ device.Connected = false;
+ }
+ }
+ foreach (object item in ev.CurrentSelection)
+ {
+ if (item == null) break;
+ if (item is Device device)
+ {
+ device.Connected = true;
+ device.Registered = true;
+ }
+ }
+ }
+
+ internal static void AddDeviceView(DeviceSource deviceSource)
+ {
+ deviceView = new CollectionView()
+ {
+ ItemsSource = deviceSource,
+ ItemsLayouter = new LinearLayouter(),
+ ItemTemplate = new DataTemplate(() =>
+ {
+ DefaultLinearItem item = new DefaultLinearItem()
+ {
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ };
+ item.Label.SetBinding(TextLabel.TextProperty, "Name");
+ item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+ item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
+ item.Icon.WidthSpecification = 40;
+ item.Icon.HeightSpecification = 40;
+ return item;
+ }),
+
+ GroupHeaderTemplate = new DataTemplate(() =>
+ {
+ DefaultTitleItem group = new DefaultTitleItem()
+ {
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ };
+ group.Label.SetBinding(TextLabel.TextProperty, "Title");
+ group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+
+ return group;
+ }),
+
+ IsGrouped = true,
+ ScrollingDirection = ScrollableBase.Direction.Vertical,
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ HeightSpecification = LayoutParamPolicies.MatchParent,
+ SelectionMode = ItemSelectionMode.Single,
+ };
+ deviceView.SelectionChanged += DeviceSelectionChanged;
+
+ mainView.Add(deviceView);
+ }
+
+ internal static void AddScanButton()
+ {
+ scanButton = new Button
+ {
+ Text = Resources.IDS_BT_SK_STOP,
+ //WidthSpecification = 300,
+ //HeightSpecification = 80,
+ };
+ scanButton.Clicked += (object obj, ClickedEventArgs ev) =>
+ {
+ //Log.Debug(Program.LogTag, "scanButton clicked. isScanning: " + isScanning);
+ // check isScanning
+ // scan stop
+ //if (isScanning == true)
+ //{
+ // BluetoothAdapter.StopDiscovery();
+ //}
+ //else
+ //{
+ // BluetoothAdapter.StartDiscovery();
+ //}
+ };
+
+ mainView.Add(scanButton);
+ }
+
+ internal static void UpdateScanButton(string text)
+ {
+ scanButton.Text = text;
+ }
+
+ internal static void RemoveDeviceView()
+ {
+ if (deviceView)
+ {
+ mainView.Remove(deviceView);
+ }
+ }
+
+ internal static void RemoveScanButton()
+ {
+ if (scanButton)
+ {
+ mainView.Remove(scanButton);
+ }
+ }
+
+ protected override void OnCreate(string contentInfo, Window window)
+ {
+ //window = NUIApplication.GetDefaultWindow();
+ //window.BackgroundColor = Color.White;
+
+ var appBar = new AppBar()
+ {
+ Title = Resources.IDS_BT_BODY_BLUETOOTH,
+ };
+
+ mainView = new View()
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Vertical,
+ HorizontalAlignment = HorizontalAlignment.Center,
+ },
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ HeightSpecification = LayoutParamPolicies.MatchParent,
+ };
+
+ var onOffItem = new DefaultLinearItem
+ {
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ Text = Resources.IDS_BT_BODY_BLUETOOTH,
+ };
+ onOffItem.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+
+ var onOffSwitch = new Switch()
+ {
+ IsSelected = BtModel.IsEnabled,
+ };
+ onOffSwitch.SelectedChanged += AdapterController.OnOffSwitchSelectedChanged;
+ onOffItem.Extra = onOffSwitch;
+ mainView.Add(onOffItem);
+
+ mainPage = new ContentPage()
+ {
+ AppBar = appBar,
+ Content = mainView,
+ };
+
+ window.GetDefaultNavigator().Push(mainPage);
+ }
+ }
+}
+++ /dev/null
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Tizen.Applications;
-using Tizen.NUI;
-using Tizen.NUI.BaseComponents;
-using Tizen.NUI.Components;
-using Tizen.Network.Bluetooth;
-using Tizen;
-using SettingBluetooth.res.locale;
-using Tizen.NUI.Binding;
-
-namespace SettingBluetooth
-{
- internal class WidgetSettingBluetooth : Widget
- {
- ContentPage mainPage;
- View mainView;
- CollectionView deviceView = null;
- DeviceSource deviceSource;
- static Button scanButton = null;
- bool isEnabled = false;
- bool isScanning = false;
- bool deviceSourceInit = false;
-
- public WidgetSettingBluetooth() : base()
- {
- }
-
- public void DeviceSelectionChanged(object obj, SelectionChangedEventArgs ev)
- {
- Log.Debug(Program.LogTag, "DeviceSelectionChanged called");
-
- //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)
- {
- device.Connected = false;
- }
- }
- foreach (object item in ev.CurrentSelection)
- {
- if (item == null) break;
- if (item is Device device)
- {
- device.Connected = true;
- device.Registered = true;
- }
- }
- }
-
- private void AddDeviceView()
- {
- deviceSource = new DeviceSource();
- deviceView = new CollectionView()
- {
- ItemsSource = deviceSource,
- ItemsLayouter = new LinearLayouter(),
- ItemTemplate = new DataTemplate(() =>
- {
- DefaultLinearItem item = new DefaultLinearItem()
- {
- WidthSpecification = LayoutParamPolicies.MatchParent,
- };
- item.Label.SetBinding(TextLabel.TextProperty, "Name");
- item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
- item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
- item.Icon.WidthSpecification = 40;
- item.Icon.HeightSpecification = 40;
- return item;
- }),
-
- GroupHeaderTemplate = new DataTemplate(() =>
- {
- DefaultTitleItem group = new DefaultTitleItem()
- {
- WidthSpecification = LayoutParamPolicies.MatchParent,
- };
- group.Label.SetBinding(TextLabel.TextProperty, "Title");
- group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
-
- return group;
- }),
-
- IsGrouped = true,
- ScrollingDirection = ScrollableBase.Direction.Vertical,
- WidthSpecification = LayoutParamPolicies.MatchParent,
- HeightSpecification = LayoutParamPolicies.MatchParent,
- SelectionMode = ItemSelectionMode.Single,
- };
- deviceView.SelectionChanged += DeviceSelectionChanged;
-
- mainView.Add(deviceView);
- }
-
- private void AddScanButton()
- {
- scanButton = new Button
- {
- Text = Resources.IDS_BT_SK_STOP,
- //WidthSpecification = 300,
- //HeightSpecification = 80,
- };
- scanButton.Clicked += (object obj, ClickedEventArgs ev) =>
- {
- Log.Debug(Program.LogTag, "scanButton clicked. isScanning: " + isScanning);
- // check isScanning
- // scan stop
- if (isScanning == true)
- {
- BluetoothAdapter.StopDiscovery();
- }
- else
- {
- BluetoothAdapter.StartDiscovery();
- }
- };
-
- mainView.Add(scanButton);
- }
-
- internal static void UpdateScanButton(string text)
- {
- scanButton.Text = text;
- }
-
- private void RemoveDeviceView()
- {
- if (deviceView)
- {
- mainView.Remove(deviceView);
- }
- }
-
- private void RemoveScanButton()
- {
- if (scanButton)
- {
- mainView.Remove(scanButton);
- }
- }
-
- private void EventHandlerDiscoveryChanged(object obj, DiscoveryStateChangedEventArgs ev)
- {
- if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Started)
- {
- Log.Debug(Program.LogTag, "Device discovery started");
- }
- else if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Found)
- {
- if (deviceSourceInit == false)
- {
- AddDeviceView();
- AddScanButton();
- deviceSourceInit = true;
- }
-
- Log.Debug(Program.LogTag, "Device found. Address: " + ev.DeviceFound.Address + ", Appearance: " + ev.DeviceFound.AppearanceType);
- deviceSource.AddDevice(ev.DeviceFound.Name, ev.DeviceFound.AppearanceType);
- }
- else
- {
- Log.Debug(Program.LogTag, "Device discovery finished");
- isScanning = false;
- deviceSource.UpdateTitle(Resources.IDS_BT_BODY_AVAILABLE_DEVICES);
- UpdateScanButton(Resources.IDS_BT_SK_SCAN);
- }
- }
-
- private void EventHandlerStateChanged(object obj, StateChangedEventArgs ev)
- {
- if (ev.BTState == BluetoothState.Enabled)
- {
- Log.Info(Program.LogTag, "BT enabled. Start discovery");
- isEnabled = true;
- BluetoothAdapter.StartDiscovery();
- isScanning = true;
- }
- else
- {
- Log.Info(Program.LogTag, "BT disabled");
- isEnabled = false;
- RemoveDeviceView();
- RemoveScanButton();
- deviceSourceInit = false;
- }
- }
-
- protected override void OnCreate(string contentInfo, Window window)
- {
- window = NUIApplication.GetDefaultWindow();
- window.BackgroundColor = Color.White;
-
- var appBar = new AppBar()
- {
- Title = Resources.IDS_BT_BODY_BLUETOOTH,
- };
-
- mainView = new View()
- {
- Layout = new LinearLayout()
- {
- LinearOrientation = LinearLayout.Orientation.Vertical,
- HorizontalAlignment = HorizontalAlignment.Center,
- },
- WidthSpecification = LayoutParamPolicies.MatchParent,
- HeightSpecification = LayoutParamPolicies.MatchParent,
- };
-
- isEnabled = BluetoothAdapter.IsBluetoothEnabled;
- BluetoothAdapter.StateChanged += EventHandlerStateChanged;
- BluetoothAdapter.DiscoveryStateChanged += EventHandlerDiscoveryChanged;
-
- var onOffItem = new DefaultLinearItem
- {
- WidthSpecification = LayoutParamPolicies.MatchParent,
- Text = Resources.IDS_BT_BODY_BLUETOOTH,
- };
- onOffItem.Label.HorizontalAlignment = HorizontalAlignment.Begin;
-
- var onOffSwitch = new Switch()
- {
- IsSelected = isEnabled,
- };
- onOffSwitch.SelectedChanged += (object obj, SelectedChangedEventArgs ev) =>
- {
- Log.Debug(Program.LogTag, "btOnOffSwitch clicked. IsSelected: " + ev.IsSelected + ", isEnabled: " + isEnabled);
- if (ev.IsSelected == true && isEnabled == false)
- {
- Log.Info(Program.LogTag, "Enable BT adapter");
- BluetoothAdapter.Enable();
- }
- else if (ev.IsSelected == false && isEnabled == true)
- {
- Log.Info(Program.LogTag, "Disable BT adapter");
- BluetoothAdapter.Disable();
- }
- else
- {
- Log.Error(Program.LogTag, "Invalid state");
- }
- };
- onOffItem.Extra = onOffSwitch;
- mainView.Add(onOffItem);
-
- if (isEnabled)
- {
- AddDeviceView();
- AddScanButton();
- }
-
- mainPage = new ContentPage()
- {
- AppBar = appBar,
- Content = mainView,
- };
-
- window.GetDefaultNavigator().Push(mainPage);
- }
- }
-}