2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Threading.Tasks;
20 namespace Tizen.Network.Bluetooth
22 internal class BluetoothHidDeviceImpl
24 private event EventHandler<HidDeviceConnectionStateChangedEventArgs> _hidDeviceConnectionStateChanged;
25 private Interop.Bluetooth.HidDeviceConnectionStateChangedCallback _hidDeviceConnectionStateChangedCallback;
27 private event EventHandler<HidDeviceDataReceivedEventArgs> _hidDeviceDataReceived;
28 private Interop.Bluetooth.HidDeviceDataReceivedCallback _hidDeviceDataReceivedCallback;
30 private static readonly BluetoothHidDeviceImpl _instance = new BluetoothHidDeviceImpl();
32 internal event EventHandler<HidDeviceConnectionStateChangedEventArgs> ConnectionStateChanged
36 _hidDeviceConnectionStateChanged += value;
40 _hidDeviceConnectionStateChanged -= value;
44 internal int ConnectHidDevice(string deviceAddress)
46 return Interop.Bluetooth.ConnectHidDevice(deviceAddress);
49 internal int DisconnectHidDevice(string deviceAddress)
51 return Interop.Bluetooth.DisconnectHidDevice(deviceAddress);
54 internal void SendHidDeviceMouseEvent(string deviceAddress, BluetoothHidMouseData mouseData)
56 int ret = Interop.Bluetooth.SendHidDeviceMouseEvent(deviceAddress, mouseData);
57 if (ret != (int)BluetoothError.None)
59 Log.Error(Globals.LogTag, "Failed to send mouse event to the remote device, Error - " + (BluetoothError)ret);
60 BluetoothErrorFactory.ThrowBluetoothException(ret);
64 internal void SendHidDeviceKeyEvent(string deviceAddress, BluetoothHidKeyData keyData)
66 int ret = Interop.Bluetooth.SendHidDeviceKeyEvent(deviceAddress, keyData);
67 if (ret != (int)BluetoothError.None)
69 Log.Error(Globals.LogTag, "Failed to send key event to the remote device, Error - " + (BluetoothError)ret);
70 BluetoothErrorFactory.ThrowBluetoothException(ret);
74 internal event EventHandler<HidDeviceDataReceivedEventArgs> DataReceived
78 if (_hidDeviceDataReceived == null)
80 RegisterHidDataReceivedEvent();
82 _hidDeviceDataReceived += value;
86 _hidDeviceDataReceived -= value;
87 if (_hidDeviceDataReceived == null)
89 UnregisterHidDataReceivedEvent();
94 private void RegisterHidDataReceivedEvent()
96 _hidDeviceDataReceivedCallback = (ref BluetoothHidDeviceReceivedDataStruct receivedData, IntPtr userData) =>
98 _hidDeviceDataReceived?.Invoke(null, new HidDeviceDataReceivedEventArgs(BluetoothHidDeviceReceivedData.Create(receivedData)));
101 int ret = Interop.Bluetooth.SetHidDeviceDataReceivedCallback(_hidDeviceDataReceivedCallback, IntPtr.Zero);
102 if (ret != (int)BluetoothError.None)
104 Log.Error(Globals.LogTag, "Failed to set data received callback, Error - " + (BluetoothError)ret);
105 BluetoothErrorFactory.ThrowBluetoothException(ret);
109 private void UnregisterHidDataReceivedEvent()
111 int ret = Interop.Bluetooth.UnsetHidDeviceDataReceivedCallback();
112 if (ret != (int)BluetoothError.None)
114 Log.Error(Globals.LogTag, "Failed to unset data received callback, Error - " + (BluetoothError)ret);
115 BluetoothErrorFactory.ThrowBluetoothException(ret);
119 internal void ReplyToReportHidDevice(string deviceAddress, BluetoothHidHeaderType headerType, BluetoothHidParamType paramType, byte[] data)
121 int ret = Interop.Bluetooth.ReplyToReportHidDevice(deviceAddress, headerType, paramType, data, data.Length, IntPtr.Zero);
122 if (ret != (int)BluetoothError.None)
124 Log.Error(Globals.LogTag, "Failed to reply to report from hid host, Error - " + (BluetoothError)ret);
125 BluetoothErrorFactory.ThrowBluetoothException(ret);
129 internal static BluetoothHidDeviceImpl Instance
137 private BluetoothHidDeviceImpl()
142 ~BluetoothHidDeviceImpl()
147 private void Initialize()
149 if (Globals.IsInitialize)
151 _hidDeviceConnectionStateChangedCallback = (int result, bool isConnected, string address, IntPtr userData) =>
153 _hidDeviceConnectionStateChanged?.Invoke(null, new HidDeviceConnectionStateChangedEventArgs(result, isConnected, address));
156 int ret = Interop.Bluetooth.ActivateHidDevice(_hidDeviceConnectionStateChangedCallback, IntPtr.Zero);
157 if (ret != (int)BluetoothError.None)
159 Log.Error(Globals.LogTag, "Failed to activate to the remote device, Error - " + (BluetoothError)ret);
160 BluetoothErrorFactory.ThrowBluetoothException(ret);
165 Log.Error(Globals.LogTag, "Failed to initialize HID Device, BT not initialized");
166 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
170 private void Deinitialize()
172 int ret = Interop.Bluetooth.DeactivateHidDevice();
173 if (ret != (int)BluetoothError.None)
175 Log.Error(Globals.LogTag, "Failed to deactivate to the remote device, Error - " + (BluetoothError)ret);
176 BluetoothErrorFactory.ThrowBluetoothException(ret);