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.
19 namespace Tizen.Network.Bluetooth
21 internal class BluetoothHidImpl : IDisposable
23 private event EventHandler<HidConnectionStateChangedEventArgs> _hidConnectionChanged;
24 private Interop.Bluetooth.HidConnectionStateChangedCallback _hidConnectionChangedCallback;
26 private static readonly BluetoothHidImpl _instance = new BluetoothHidImpl();
27 private bool disposed = false;
29 internal event EventHandler<HidConnectionStateChangedEventArgs> HidConnectionStateChanged
33 _hidConnectionChanged += value;
41 internal int Connect(string deviceAddress)
43 if (Globals.IsHidInitialize)
45 int ret = Interop.Bluetooth.Connect (deviceAddress);
46 if (ret != (int)BluetoothError.None) {
47 Log.Error (Globals.LogTag, "Failed to connect device with the hid service, Error - " + (BluetoothError)ret);
51 return (int)BluetoothError.NotInitialized;
54 internal int Disconnect(string deviceAddress)
56 if (Globals.IsHidInitialize)
58 int ret = Interop.Bluetooth.Disconnect (deviceAddress);
59 if (ret != (int)BluetoothError.None) {
60 Log.Error (Globals.LogTag, "Failed to disconnect device with the hid service, Error - " + (BluetoothError)ret);
64 return (int)BluetoothError.NotInitialized;
67 internal static BluetoothHidImpl Instance
74 private BluetoothHidImpl ()
86 GC.SuppressFinalize(this);
89 private void Dispose(bool disposing)
96 // Free managed objects.
98 //Free unmanaged objects
103 private void initialize()
105 if (Globals.IsInitialize)
107 _hidConnectionChangedCallback = (int result, bool connected, string deviceAddress, IntPtr userData) =>
109 if (_hidConnectionChanged != null)
111 _hidConnectionChanged(null, new HidConnectionStateChangedEventArgs(result, connected, deviceAddress));
115 int ret = Interop.Bluetooth.InitializeHid (_hidConnectionChangedCallback, IntPtr.Zero);
116 if (ret != (int)BluetoothError.None)
118 Log.Error(Globals.LogTag, "Failed to initialize bluetooth hid, Error - " + (BluetoothError)ret);
119 BluetoothErrorFactory.ThrowBluetoothException (ret);
123 Globals.IsHidInitialize = true;
128 Log.Error(Globals.LogTag, "Failed to initialize HID, BT not initialized");
129 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
133 private void deinitialize()
135 if (Globals.IsHidInitialize)
137 int ret = Interop.Bluetooth.DeinitializeHid ();
138 if (ret != (int)BluetoothError.None) {
139 Log.Error (Globals.LogTag, "Failed to deinitialize bluetooth hid, Error - " + (BluetoothError)ret);
140 BluetoothErrorFactory.ThrowBluetoothException (ret);
142 Globals.IsHidInitialize = false;