{
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");
if (item == null) break;
if (item is Device device)
{
- device.Connected = false;
+ DoPairUnpair(device);
+ //device.Connected = false;
}
}
foreach (object item in ev.CurrentSelection)
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;
}
}
}
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)
// 5. Set BT Operation state as Activated
}
- internal static void DisConnectAudio()
+ internal void DisConnectAudio()
{
// TODO
// 1. Call Disconnect API (Audio)
// 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)
}
namespace SettingBluetooth.Model
{
- public enum BtOperationState
+ internal enum BtOperationState
{
Deactivated,
Deactivating,
Connecting,
}
- public enum BtDeviceState
+ internal enum BtDeviceState
{
Idle,
Pairing,
Unpairing,
Paired,
+ Unpaired,
Connecting,
Connected,
Disconnecting,
{
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;
+ }
+ }
}
}
BtModelImpl.Instance.DeviceChanged -= value;
}
}
+
+ internal static void NotifyOperationStateChanged(BtOperationState state)
+ {
+ BtModelImpl.Instance.NotifyOperationStateChanged(state);
+ }
+
+ internal static void NotifyDeviceChanged(BtDevice btDevice)
+ {
+ BtModelImpl.Instance.NotifyDeviceChanged(btDevice);
+ }
}
}
{
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);
}
}
+ 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;
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)
{
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<Device>
}
}
- 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;
}
}
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;
}
}
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()
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
// 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;
}
}
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;
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;
}
}
}
}
+ /// <summary>
+ /// 과(와) 유사한 지역화된 문자열을 찾습니다.
+ /// </summary>
+ public static string IDS_BT_BODY_PAIRED_DEVICES {
+ get {
+ return ResourceManager.GetString("IDS_BT_BODY_PAIRED_DEVICES", resourceCulture);
+ }
+ }
+
/// <summary>
/// 과(와) 유사한 지역화된 문자열을 찾습니다.
/// </summary>
<data name="IDS_BT_BODY_BLUETOOTH" xml:space="preserve">
<value>Bluetooth</value>
</data>
+ <data name="IDS_BT_BODY_PAIRED_DEVICES" xml:space="preserve">
+ <value>Paired devices</value>
+ </data>
<data name="IDS_BT_BODY_SCANNING_FOR_DEVICES_ING" xml:space="preserve">
<value>Scanning for devices...</value>
</data>
<data name="IDS_BT_BODY_BLUETOOTH" xml:space="preserve">
<value>블루투스</value>
</data>
+ <data name="IDS_BT_BODY_PAIRED_DEVICES" xml:space="preserve">
+ <value>등록된 디바이스</value>
+ </data>
<data name="IDS_BT_BODY_SCANNING_FOR_DEVICES_ING" xml:space="preserve">
<value>디바이스 찾는 중...</value>
</data>
<data name="IDS_BT_SK_STOP" xml:space="preserve">
<value>중지</value>
</data>
+ <data name="String1" xml:space="preserve">
+ <value />
+ </data>
</root>
\ No newline at end of file
<data name="IDS_BT_BODY_BLUETOOTH" xml:space="preserve">
<value />
</data>
+ <data name="IDS_BT_BODY_PAIRED_DEVICES" xml:space="preserve">
+ <value />
+ </data>
<data name="IDS_BT_BODY_SCANNING_FOR_DEVICES_ING" xml:space="preserve">
<value />
</data>