{
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;
- // }
- // }
- //}
}
}
private bool mIsPaired;
private BtDeviceState mDeviceState;
private BluetoothDevice mBluetoothDevice;
-
- //private IEnumerable<string> mServiceList = null;
private string[] mServiceUuids = null;
private BluetoothServiceClassType mServiceMask = 0;
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;
}
}
}
}
+ 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);
//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<BluetoothHid>();
+ 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<BluetoothHid>();
+ mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
+ mBluetoothHid.Disconnect();
+ mDeviceState = BtDeviceState.Disconnecting;
+ BtModel.NotifyDeviceChanged(this);
+ //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
}
}
}
if (ev.BTState == BluetoothState.Enabled)
{
mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activated));
+
+ IEnumerable<BluetoothDevice> 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
{
}
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));
+ }
}
}
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
internal void AddDevice(BtDevice btDevice)
{
collection.AddDevice(btDevice);
+ UpdateState(btDevice);
}
internal void RemoveDevice(string address)
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;
}
}
{
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
{
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:
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:
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:
}
}
+ /// <summary>
+ /// 과(와) 유사한 지역화된 문자열을 찾습니다.
+ /// </summary>
+ public static string IDS_BT_POP_SEARCHING_SERVICES_ING {
+ get {
+ return ResourceManager.GetString("IDS_BT_POP_SEARCHING_SERVICES_ING", resourceCulture);
+ }
+ }
+
/// <summary>
/// 과(와) 유사한 지역화된 문자열을 찾습니다.
/// </summary>
<data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
<value>Disconnect device</value>
</data>
+ <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+ <value>Searching services...</value>
+ </data>
<data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
<value>Connected</value>
</data>
<data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
<value>디바이스 연결 해제</value>
</data>
+ <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+ <value>서비스 검색 중...</value>
+ </data>
<data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
<value>연결됨</value>
</data>
<data name="IDS_BT_HEADER_DISCONNECT_DEVICE_ABB" xml:space="preserve">
<value />
</data>
+ <data name="IDS_BT_POP_SEARCHING_SERVICES_ING" xml:space="preserve">
+ <value />
+ </data>
<data name="IDS_BT_SBODY_CONNECTED_M_STATUS" xml:space="preserve">
<value />
</data>