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 BluetoothAudioImpl : IDisposable
23 private event EventHandler<AudioConnectionStateChangedEventArgs> _audioConnectionChanged;
24 private event EventHandler<AgScoStateChangedEventArgs> _agScoStateChanged;
25 private Interop.Bluetooth.AudioConnectionStateChangedCallback _audioConnectionChangedCallback;
27 private static readonly BluetoothAudioImpl _instance = new BluetoothAudioImpl();
28 private bool disposed = false;
30 internal event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
34 if (_audioConnectionChanged == null)
36 RegisterAudioConnectionChangedEvent();
38 _audioConnectionChanged += value;
42 _audioConnectionChanged -= value;
43 if (_audioConnectionChanged == null)
45 UnregisterAudioConnectionChangedEvent();
50 private void RegisterAudioConnectionChangedEvent()
52 _audioConnectionChangedCallback = (int result, bool connected, string deviceAddress, int profileType, IntPtr userData) =>
54 _audioConnectionChanged?.Invoke(this, new AudioConnectionStateChangedEventArgs(result, connected, deviceAddress, (BluetoothAudioProfileType)profileType));
56 int ret = Interop.Bluetooth.SetAudioConnectionStateChangedCallback(_audioConnectionChangedCallback, IntPtr.Zero);
57 if (ret != (int)BluetoothError.None)
59 Log.Error(Globals.LogTag, "Failed to set audio connection changed callback, Error - " + (BluetoothError)ret);
63 private void UnregisterAudioConnectionChangedEvent()
65 int ret = Interop.Bluetooth.UnsetAudioConnectionStateChangedCallback();
66 if (ret != (int)BluetoothError.None)
68 Log.Error(Globals.LogTag, "Failed to unset audio connection changed callback, Error - " + (BluetoothError)ret);
72 internal int Connect(string deviceAddress, BluetoothAudioProfileType type)
74 int ret = Interop.Bluetooth.Connect(deviceAddress, (int)type);
75 if (ret != (int)BluetoothError.None)
77 Log.Error(Globals.LogTag, "Failed to connect device with the given profile type, Error - " + (BluetoothError)ret);
82 internal int Disconnect(string deviceAddress, BluetoothAudioProfileType type)
84 int ret = Interop.Bluetooth.Disconnect(deviceAddress, (int)type);
85 if (ret != (int)BluetoothError.None)
87 Log.Error(Globals.LogTag, "Failed to disconnect device with the given profile type, Error - " + (BluetoothError)ret);
92 internal void OpenAgSco()
94 if (Globals.IsAudioInitialize)
96 int ret = Interop.Bluetooth.OpenAgSco();
97 if (ret != (int)BluetoothError.None)
99 Log.Error(Globals.LogTag, "Failed to open ag sco to remote device, Error - " + (BluetoothError)ret);
100 BluetoothErrorFactory.ThrowBluetoothException(ret);
105 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
109 internal void CloseAgSco()
111 if (Globals.IsAudioInitialize)
113 int ret = Interop.Bluetooth.CloseAgSco();
114 if (ret != (int)BluetoothError.None)
116 Log.Error(Globals.LogTag, "Failed to close ag sco to remote device, Error - " + (BluetoothError)ret);
117 BluetoothErrorFactory.ThrowBluetoothException(ret);
122 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
126 internal bool IsAgScoOpened
131 int ret = Interop.Bluetooth.IsAgScoOpened(out isOpened);
132 if (ret != (int)BluetoothError.None)
134 Log.Error(Globals.LogTag, "Failed to check whether an opened SCO exists or not., Error - " + (BluetoothError)ret);
140 internal event EventHandler<AgScoStateChangedEventArgs> AgScoStateChanged
144 if (_agScoStateChanged == null)
146 RegisterAgScoStateChangedEvent();
148 _agScoStateChanged += value;
152 _agScoStateChanged -= value;
153 if (_agScoStateChanged == null)
155 UnregisterAgScoStateChangedEvent();
160 private void RegisterAgScoStateChangedEvent()
162 Interop.Bluetooth.AgScoStateChangedCallback _agScoStateChangedCallback = (int result, bool opened, IntPtr userData) =>
164 _agScoStateChanged?.Invoke(null, new AgScoStateChangedEventArgs(opened));
167 int ret = Interop.Bluetooth.SetAgScoStateChangedCallback(_agScoStateChangedCallback, IntPtr.Zero);
168 if (ret != (int)BluetoothError.None)
170 Log.Error(Globals.LogTag, "Failed to set ag sco state changed callback, Error - " + (BluetoothError)ret);
174 private void UnregisterAgScoStateChangedEvent()
176 int ret = Interop.Bluetooth.UnsetAgScoStateChangedCallback();
177 if (ret != (int)BluetoothError.None)
179 Log.Error(Globals.LogTag, "Failed to unset ag sco state changed callback, Error - " + (BluetoothError)ret);
183 internal void NotifyAgVoiceRecognitionState(bool enable)
185 if (Globals.IsAudioInitialize)
187 int ret = Interop.Bluetooth.NotifyAgVoiceRecognitionState(enable);
188 if (ret != (int)BluetoothError.None)
190 Log.Error(Globals.LogTag, "Failed to notify sco voice recognition state, Error - " + (BluetoothError)ret);
191 BluetoothErrorFactory.ThrowBluetoothException(ret);
196 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
200 internal static BluetoothAudioImpl Instance
208 private BluetoothAudioImpl ()
210 Log.Info(Globals.LogTag, "Initializing audio");
214 ~BluetoothAudioImpl()
219 public void Dispose()
222 GC.SuppressFinalize(this);
225 private void Dispose(bool disposing)
232 // Free managed objects.
234 //Free unmanaged objects
236 RemoveAllRegisteredEvent();
240 private void initialize()
242 if (Globals.IsInitialize)
244 int ret = Interop.Bluetooth.InitializeAudio ();
245 if (ret != (int)BluetoothError.None)
247 Log.Error(Globals.LogTag, "Failed to initialize bluetoothaudio, Error - " + (BluetoothError)ret);
248 Globals.IsAudioInitialize = false;
249 BluetoothErrorFactory.ThrowBluetoothException (ret);
253 Globals.IsAudioInitialize = true;
258 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
262 private void deinitialize()
264 if (Globals.IsAudioInitialize) {
265 int ret = Interop.Bluetooth.DeinitializeAudio ();
266 if (ret != (int)BluetoothError.None) {
267 Log.Error (Globals.LogTag, "Failed to deinitialize bluetoothaudio, Error - " + (BluetoothError)ret);
272 private void RemoveAllRegisteredEvent()
274 if (_audioConnectionChanged != null)
276 UnregisterAudioConnectionChangedEvent();