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 static readonly BluetoothAudioImpl _instance = new BluetoothAudioImpl();
27 private bool disposed = false;
29 internal event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
33 if (_audioConnectionChanged == null)
35 RegisterAudioConnectionChangedEvent();
37 _audioConnectionChanged += value;
41 _audioConnectionChanged -= value;
42 if (_audioConnectionChanged == null)
44 UnregisterAudioConnectionChangedEvent();
49 private void RegisterAudioConnectionChangedEvent()
51 _audioConnectionChangedCallback = (int result, bool connected, string deviceAddress, int profileType, IntPtr userData) =>
53 if (_audioConnectionChanged != null)
55 _audioConnectionChanged(null, 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 static BluetoothAudioImpl Instance
102 private BluetoothAudioImpl ()
104 Log.Info(Globals.LogTag, "Initializing audio");
108 ~BluetoothAudioImpl()
113 public void Dispose()
116 GC.SuppressFinalize(this);
119 private void Dispose(bool disposing)
126 // Free managed objects.
128 //Free unmanaged objects
130 RemoveAllRegisteredEvent();
134 private void initialize()
136 if (Globals.IsInitialize)
138 int ret = Interop.Bluetooth.InitializeAudio ();
139 if (ret != (int)BluetoothError.None)
141 Log.Error(Globals.LogTag, "Failed to initialize bluetoothaudio, Error - " + (BluetoothError)ret);
142 Globals.IsAudioInitialize = false;
143 BluetoothErrorFactory.ThrowBluetoothException (ret);
147 Globals.IsAudioInitialize = true;
152 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
156 private void deinitialize()
158 if (Globals.IsAudioInitialize) {
159 int ret = Interop.Bluetooth.DeinitializeAudio ();
160 if (ret != (int)BluetoothError.None) {
161 Log.Error (Globals.LogTag, "Failed to deinitialize bluetoothaudio, Error - " + (BluetoothError)ret);
166 private void RemoveAllRegisteredEvent()
168 if (_audioConnectionChanged != null)
170 UnregisterAudioConnectionChangedEvent();