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 event EventHandler<HidDeviceDataReceivedEventArgs> _hidDeviceDataReceived;
27 private static readonly BluetoothHidDeviceImpl _instance = new BluetoothHidDeviceImpl();
29 internal event EventHandler<HidDeviceConnectionStateChangedEventArgs> ConnectionStateChanged
33 _hidDeviceConnectionStateChanged += value;
37 _hidDeviceConnectionStateChanged -= value;
41 internal int ConnectHidDevice(string deviceAddress)
43 return Interop.Bluetooth.ConnectHidDevice(deviceAddress);
46 internal int DisconnectHidDevice(string deviceAddress)
48 return Interop.Bluetooth.DisconnectHidDevice(deviceAddress);
51 internal void SendHidDeviceMouseEvent(string deviceAddress, BluetoothHidMouseData mouseData)
53 int ret = Interop.Bluetooth.SendHidDeviceMouseEvent(deviceAddress, mouseData);
54 if (ret != (int)BluetoothError.None)
56 Log.Error(Globals.LogTag, "Failed to send mouse event to the remote device, Error - " + (BluetoothError)ret);
57 BluetoothErrorFactory.ThrowBluetoothException(ret);
61 internal void SendHidDeviceKeyEvent(string deviceAddress, BluetoothHidKeyData keyData)
63 int ret = Interop.Bluetooth.SendHidDeviceKeyEvent(deviceAddress, keyData);
64 if (ret != (int)BluetoothError.None)
66 Log.Error(Globals.LogTag, "Failed to send key event to the remote device, Error - " + (BluetoothError)ret);
67 BluetoothErrorFactory.ThrowBluetoothException(ret);
71 internal event EventHandler<HidDeviceDataReceivedEventArgs> DataReceived
75 if (_hidDeviceDataReceived == null)
77 RegisterHidDataReceivedEvent();
79 _hidDeviceDataReceived += value;
83 _hidDeviceDataReceived -= value;
84 if (_hidDeviceDataReceived == null)
86 UnregisterHidDataReceivedEvent();
91 private void RegisterHidDataReceivedEvent()
93 Interop.Bluetooth.HidDeviceDataReceivedCallback _hidDeviceDataReceivedCallback = (ref BluetoothHidDeviceReceivedDataStruct receivedData, IntPtr userData) =>
95 _hidDeviceDataReceived?.Invoke(null, new HidDeviceDataReceivedEventArgs(BluetoothHidDeviceReceivedData.Create(receivedData)));
98 int ret = Interop.Bluetooth.SetHidDeviceDataReceivedCallback(_hidDeviceDataReceivedCallback, IntPtr.Zero);
99 if (ret != (int)BluetoothError.None)
101 Log.Error(Globals.LogTag, "Failed to set data received callback, Error - " + (BluetoothError)ret);
102 BluetoothErrorFactory.ThrowBluetoothException(ret);
106 private void UnregisterHidDataReceivedEvent()
108 int ret = Interop.Bluetooth.UnsetHidDeviceDataReceivedCallback();
109 if (ret != (int)BluetoothError.None)
111 Log.Error(Globals.LogTag, "Failed to unset data received callback, Error - " + (BluetoothError)ret);
112 BluetoothErrorFactory.ThrowBluetoothException(ret);
116 internal void ReplyToReportHidDevice(string deviceAddress, BluetoothHidHeaderType headerType, BluetoothHidParamType paramType, byte[] data)
118 int ret = Interop.Bluetooth.ReplyToReportHidDevice(deviceAddress, headerType, paramType, data, data.Length, IntPtr.Zero);
119 if (ret != (int)BluetoothError.None)
121 Log.Error(Globals.LogTag, "Failed to reply to report from hid host, Error - " + (BluetoothError)ret);
122 BluetoothErrorFactory.ThrowBluetoothException(ret);
126 internal static BluetoothHidDeviceImpl Instance
134 private BluetoothHidDeviceImpl()
139 ~BluetoothHidDeviceImpl()
144 private void Initialize()
146 if (Globals.IsInitialize)
148 Interop.Bluetooth.HidDeviceConnectionStateChangedCallback _hidDeviceConnectionStateChangedCallback = (int result, bool isConnected, string address, IntPtr userData) =>
150 _hidDeviceConnectionStateChanged?.Invoke(null, new HidDeviceConnectionStateChangedEventArgs(result, isConnected, address));
153 int ret = Interop.Bluetooth.ActivateHidDevice(_hidDeviceConnectionStateChangedCallback, IntPtr.Zero);
154 if (ret != (int)BluetoothError.None)
156 Log.Error(Globals.LogTag, "Failed to activate to the remote device, Error - " + (BluetoothError)ret);
157 BluetoothErrorFactory.ThrowBluetoothException(ret);
162 Log.Error(Globals.LogTag, "Failed to initialize HID Device, BT not initialized");
163 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
167 private void Deinitialize()
169 int ret = Interop.Bluetooth.DeactivateHidDevice();
170 if (ret != (int)BluetoothError.None)
172 Log.Error(Globals.LogTag, "Failed to deactivate to the remote device, Error - " + (BluetoothError)ret);
173 BluetoothErrorFactory.ThrowBluetoothException(ret);