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 Interop.Bluetooth.AudioConnectionStateChangedCallback _audioConnectionChangedCallback;
26 private event EventHandler<AgScoStateChangedEventArgs> _agScoStateChanged;
27 private Interop.Bluetooth.AgScoStateChangedCallback _agScoStateChangedCallback;
29 private static readonly BluetoothAudioImpl _instance = new BluetoothAudioImpl();
30 private bool disposed = false;
32 internal event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
36 if (_audioConnectionChanged == null)
38 RegisterAudioConnectionChangedEvent();
40 _audioConnectionChanged += value;
44 _audioConnectionChanged -= value;
45 if (_audioConnectionChanged == null)
47 UnregisterAudioConnectionChangedEvent();
52 private void RegisterAudioConnectionChangedEvent()
54 _audioConnectionChangedCallback = (int result, bool connected, string deviceAddress, int profileType, IntPtr userData) =>
56 _audioConnectionChanged?.Invoke(this, new AudioConnectionStateChangedEventArgs(result, connected, deviceAddress, (BluetoothAudioProfileType)profileType));
58 int ret = Interop.Bluetooth.SetAudioConnectionStateChangedCallback(_audioConnectionChangedCallback, IntPtr.Zero);
59 if (ret != (int)BluetoothError.None)
61 Log.Error(Globals.LogTag, "Failed to set audio connection changed callback, Error - " + (BluetoothError)ret);
65 private void UnregisterAudioConnectionChangedEvent()
67 int ret = Interop.Bluetooth.UnsetAudioConnectionStateChangedCallback();
68 if (ret != (int)BluetoothError.None)
70 Log.Error(Globals.LogTag, "Failed to unset audio connection changed callback, Error - " + (BluetoothError)ret);
74 internal int Connect(string deviceAddress, BluetoothAudioProfileType type)
76 int ret = Interop.Bluetooth.Connect(deviceAddress, (int)type);
77 if (ret != (int)BluetoothError.None)
79 Log.Error(Globals.LogTag, "Failed to connect device with the given profile type, Error - " + (BluetoothError)ret);
84 internal int Disconnect(string deviceAddress, BluetoothAudioProfileType type)
86 int ret = Interop.Bluetooth.Disconnect(deviceAddress, (int)type);
87 if (ret != (int)BluetoothError.None)
89 Log.Error(Globals.LogTag, "Failed to disconnect device with the given profile type, Error - " + (BluetoothError)ret);
94 internal void OpenAgSco()
96 if (Globals.IsAudioInitialize)
98 int ret = Interop.Bluetooth.OpenAgSco();
99 if (ret != (int)BluetoothError.None)
101 Log.Error(Globals.LogTag, "Failed to open ag sco to remote device, Error - " + (BluetoothError)ret);
102 BluetoothErrorFactory.ThrowBluetoothException(ret);
107 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
111 internal void CloseAgSco()
113 if (Globals.IsAudioInitialize)
115 int ret = Interop.Bluetooth.CloseAgSco();
116 if (ret != (int)BluetoothError.None)
118 Log.Error(Globals.LogTag, "Failed to close ag sco to remote device, Error - " + (BluetoothError)ret);
119 BluetoothErrorFactory.ThrowBluetoothException(ret);
124 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
128 internal bool IsAgScoOpened
133 int ret = Interop.Bluetooth.IsAgScoOpened(out isOpened);
134 if (ret != (int)BluetoothError.None)
136 Log.Error(Globals.LogTag, "Failed to check whether an opened SCO exists or not., Error - " + (BluetoothError)ret);
142 internal event EventHandler<AgScoStateChangedEventArgs> AgScoStateChanged
146 if (_agScoStateChanged == null)
148 RegisterAgScoStateChangedEvent();
150 _agScoStateChanged += value;
154 _agScoStateChanged -= value;
155 if (_agScoStateChanged == null)
157 UnregisterAgScoStateChangedEvent();
162 private void RegisterAgScoStateChangedEvent()
164 _agScoStateChangedCallback = (int result, bool opened, IntPtr userData) =>
166 _agScoStateChanged?.Invoke(null, new AgScoStateChangedEventArgs(opened));
169 int ret = Interop.Bluetooth.SetAgScoStateChangedCallback(_agScoStateChangedCallback, IntPtr.Zero);
170 if (ret != (int)BluetoothError.None)
172 Log.Error(Globals.LogTag, "Failed to set ag sco state changed callback, Error - " + (BluetoothError)ret);
176 private void UnregisterAgScoStateChangedEvent()
178 int ret = Interop.Bluetooth.UnsetAgScoStateChangedCallback();
179 if (ret != (int)BluetoothError.None)
181 Log.Error(Globals.LogTag, "Failed to unset ag sco state changed callback, Error - " + (BluetoothError)ret);
185 internal void NotifyAgVoiceRecognitionState(bool enable)
187 if (Globals.IsAudioInitialize)
189 int ret = Interop.Bluetooth.NotifyAgVoiceRecognitionState(enable);
190 if (ret != (int)BluetoothError.None)
192 Log.Error(Globals.LogTag, "Failed to notify sco voice recognition state, Error - " + (BluetoothError)ret);
193 BluetoothErrorFactory.ThrowBluetoothException(ret);
198 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
202 internal static BluetoothAudioImpl Instance
210 private BluetoothAudioImpl ()
212 Log.Info(Globals.LogTag, "Initializing audio");
216 ~BluetoothAudioImpl()
221 public void Dispose()
224 GC.SuppressFinalize(this);
227 private void Dispose(bool disposing)
234 // Free managed objects.
236 //Free unmanaged objects
238 RemoveAllRegisteredEvent();
242 private void initialize()
244 if (Globals.IsInitialize)
246 int ret = Interop.Bluetooth.InitializeAudio ();
247 if (ret != (int)BluetoothError.None)
249 Log.Error(Globals.LogTag, "Failed to initialize bluetoothaudio, Error - " + (BluetoothError)ret);
250 Globals.IsAudioInitialize = false;
251 BluetoothErrorFactory.ThrowBluetoothException (ret);
255 Globals.IsAudioInitialize = true;
260 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
264 private void deinitialize()
266 if (Globals.IsAudioInitialize) {
267 int ret = Interop.Bluetooth.DeinitializeAudio ();
268 if (ret != (int)BluetoothError.None) {
269 Log.Error (Globals.LogTag, "Failed to deinitialize bluetoothaudio, Error - " + (BluetoothError)ret);
274 private void RemoveAllRegisteredEvent()
276 if (_audioConnectionChanged != null)
278 UnregisterAudioConnectionChangedEvent();