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 BluetoothAvrcpImpl : IDisposable
23 private event EventHandler<TargetConnectionStateChangedEventArgs> _targetConnectionChanged;
24 private event EventHandler<EqualizerStateChangedEventArgs> _equalizerStateChanged;
25 private event EventHandler<RepeatModeChangedEventArgs> _repeatModeChanged;
26 private event EventHandler<ScanModeChangedEventArgs> _scanModeChanged;
27 private event EventHandler<ShuffleModeChangedeventArgs> _shuffleModeChanged;
29 private Interop.Bluetooth.TargetConnectionStateChangedCallback _targetConnectionChangedCallback;
30 private Interop.Bluetooth.EqualizerStateChangedCallback _equalizerStateChangedCallback;
31 private Interop.Bluetooth.RepeatModeChangedCallback _repeatModeChangedCallback;
32 private Interop.Bluetooth.ShuffleModeChangedCallback _shuffleModeChangedCallback;
33 private Interop.Bluetooth.ScanModeChangedCallback _scanModeChangedCallback;
35 private static BluetoothAvrcpImpl _instance = new BluetoothAvrcpImpl();
36 private bool disposed = false;
38 internal event EventHandler<TargetConnectionStateChangedEventArgs> TargetConnectionStateChanged
42 _targetConnectionChanged += value;
46 _targetConnectionChanged -= value;
50 internal event EventHandler<EqualizerStateChangedEventArgs> EqualizerStateChanged
54 if (_equalizerStateChanged == null)
56 RegisterEqualizerStateChangedEvent();
58 _equalizerStateChanged += value;
62 _equalizerStateChanged -= value;
63 if (_equalizerStateChanged == null)
65 UnregisterEqualizerStateChangedEvent();
70 internal event EventHandler<RepeatModeChangedEventArgs> RepeatModeChanged
74 if (_repeatModeChanged == null)
76 RegisterRepeatModeChangedEvent();
78 _repeatModeChanged += value;
82 _repeatModeChanged -= value;
83 if (_repeatModeChanged == null)
85 UnregisterRepeatModeChangedEvent();
90 internal event EventHandler<ShuffleModeChangedeventArgs> ShuffleModeChanged
94 if (_shuffleModeChanged == null)
96 RegisterShuffleModeChangedEvent();
98 _shuffleModeChanged += value;
102 _shuffleModeChanged -= value;
103 if (_shuffleModeChanged == null)
105 UnregisterShuffleModeChangedEvent();
110 internal event EventHandler<ScanModeChangedEventArgs> ScanModeChanged
114 if (_scanModeChanged == null)
116 RegisterScanModeChangedEvent();
118 _scanModeChanged += value;
122 _scanModeChanged -= value;
123 if (_scanModeChanged == null)
125 UnregisterScanModeChangedEvent();
130 private void RegisterEqualizerStateChangedEvent()
132 _equalizerStateChangedCallback = (int equalizer, IntPtr userData) =>
134 if (_equalizerStateChanged != null)
136 EqualizerState state = (EqualizerState) equalizer;
137 _equalizerStateChanged(null, new EqualizerStateChangedEventArgs(state));
140 int ret = Interop.Bluetooth.SetEqualizerStateChangedCallback(_equalizerStateChangedCallback, IntPtr.Zero);
141 if (ret != (int)BluetoothError.None)
143 Log.Error(Globals.LogTag, "Failed to set equalizer state changed callback, Error - " + (BluetoothError)ret);
146 private void UnregisterEqualizerStateChangedEvent()
148 int ret = Interop.Bluetooth.UnsetEqualizerStateChangedCallback();
149 if (ret != (int)BluetoothError.None)
151 Log.Error(Globals.LogTag, "Failed to unset equalizer state changed callback, Error - " + (BluetoothError)ret);
155 private void RegisterRepeatModeChangedEvent()
157 _repeatModeChangedCallback = (int repeat, IntPtr userData) =>
159 if (_repeatModeChanged != null)
161 RepeatMode mode = (RepeatMode)repeat;
162 _repeatModeChanged(null, new RepeatModeChangedEventArgs(mode));
165 int ret = Interop.Bluetooth.SetRepeatModeChangedCallback(_repeatModeChangedCallback, IntPtr.Zero);
166 if (ret != (int)BluetoothError.None)
168 Log.Error(Globals.LogTag, "Failed to set repeat mode changed callback, Error - " + (BluetoothError)ret);
171 private void UnregisterRepeatModeChangedEvent()
173 int ret = Interop.Bluetooth.UnsetRepeatModeChangedCallback();
174 if (ret != (int)BluetoothError.None)
176 Log.Error(Globals.LogTag, "Failed to unset repeat mode changed callback, Error - " + (BluetoothError)ret);
180 private void RegisterShuffleModeChangedEvent()
182 Log.Debug (Globals.LogTag, "inside RegisterShuffleModeChangedEvent");
183 _shuffleModeChangedCallback = (int shuffle, IntPtr userData) =>
185 Log.Debug (Globals.LogTag, "inside RegisterShuffleModeChangedEvent callback");
186 if (_shuffleModeChanged != null)
188 ShuffleMode mode = (ShuffleMode) shuffle;
189 _shuffleModeChanged(null, new ShuffleModeChangedeventArgs(mode));
192 int ret = Interop.Bluetooth.SetShuffleModeChangedCallback(_shuffleModeChangedCallback, IntPtr.Zero);
193 if (ret != (int)BluetoothError.None)
195 Log.Debug (Globals.LogTag, "failed inside RegisterShuffleModeChangedEvent");
196 Log.Error(Globals.LogTag, "Failed to set shuffle mode changed callback, Error - " + (BluetoothError)ret);
199 private void UnregisterShuffleModeChangedEvent()
201 int ret = Interop.Bluetooth.UnsetShuffleModeChangedCallback();
202 if (ret != (int)BluetoothError.None)
204 Log.Error(Globals.LogTag, "Failed to unset shuffle mode changed callback, Error - " + (BluetoothError)ret);
208 private void RegisterScanModeChangedEvent()
210 _scanModeChangedCallback = (int scan, IntPtr userData) =>
212 if (_scanModeChanged != null)
214 ScanMode mode = (ScanMode) scan;
215 _scanModeChanged(null, new ScanModeChangedEventArgs(mode));
218 int ret = Interop.Bluetooth.SetScanModeChangedCallback(_scanModeChangedCallback, IntPtr.Zero);
219 if (ret != (int)BluetoothError.None)
221 Log.Error(Globals.LogTag, "Failed to set scan mode changed callback, Error - " + (BluetoothError)ret);
224 private void UnregisterScanModeChangedEvent()
226 int ret = Interop.Bluetooth.UnsetScanModeChangedCallback();
227 if (ret != (int)BluetoothError.None)
229 Log.Error(Globals.LogTag, "Failed to unset scan mode changed callback, Error - " + (BluetoothError)ret);
233 private void targetInitialize()
235 if (Globals.IsInitialize)
237 _targetConnectionChangedCallback = (bool connected, string deviceAddress, IntPtr userData) =>
239 if (_targetConnectionChanged != null)
241 _targetConnectionChanged(null, new TargetConnectionStateChangedEventArgs(connected, deviceAddress));
245 int ret = Interop.Bluetooth.InitializeAvrcp(_targetConnectionChangedCallback, IntPtr.Zero);
246 if (ret != (int)BluetoothError.None)
248 Log.Error (Globals.LogTag, "Failed to initialize bluetooth avrcp, Error - " + (BluetoothError)ret);
249 BluetoothErrorFactory.ThrowBluetoothException(ret);
253 Globals.IsAudioInitialize = true;
258 Log.Error(Globals.LogTag, "Failed to initialize Avrcp, BT not initialized");
259 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
263 private void targetDeinitialize()
265 if (Globals.IsAudioInitialize)
267 int ret = Interop.Bluetooth.DeinitializeAvrcp();
268 if (ret != (int)BluetoothError.None)
270 Log.Error (Globals.LogTag, "Failed to deinitialize bluetooth avrcp, Error - " + (BluetoothError)ret);
271 BluetoothErrorFactory.ThrowBluetoothException(ret);
275 Globals.IsAudioInitialize = false;
280 internal void NotifyEqualizeState(EqualizerState state)
282 int ret = Interop.Bluetooth.NotifyEqualizerState((int)state);
283 if (ret != (int)BluetoothError.None)
285 Log.Error(Globals.LogTag, "Failed to notify equalizer state to remote device, Error - " + (BluetoothError)ret);
286 BluetoothErrorFactory.ThrowBluetoothException(ret);
290 internal void NotifyRepeatMode(RepeatMode repeat)
292 int ret = Interop.Bluetooth.NotifyRepeatMode((int)repeat);
293 if (ret != (int)BluetoothError.None)
295 Log.Error(Globals.LogTag, "Failed to notify repeat mode to remote device, Error - " + (BluetoothError)ret);
296 BluetoothErrorFactory.ThrowBluetoothException(ret);
300 internal void NotifyShuffleMode(ShuffleMode shuffle)
302 int ret = Interop.Bluetooth.NotifyShuffleMode((int)shuffle);
303 if (ret != (int)BluetoothError.None)
305 Log.Error(Globals.LogTag, "Failed to notify shuffle mode to remote device, Error - " + (BluetoothError)ret);
306 BluetoothErrorFactory.ThrowBluetoothException(ret);
310 internal void NotifyScanMode(ScanMode scan)
312 int ret = Interop.Bluetooth.NotifyScanMode((int)scan);
313 if (ret != (int)BluetoothError.None)
315 Log.Error(Globals.LogTag, "Failed to notify scan mode to remote device, Error - " + (BluetoothError)ret);
316 BluetoothErrorFactory.ThrowBluetoothException(ret);
320 internal void NotifyPlayerState(PlayerState state)
322 int ret = Interop.Bluetooth.NotifyPlayerState((int)state);
323 if (ret != (int)BluetoothError.None)
325 Log.Error(Globals.LogTag, "Failed to notify player state to remote device, Error - " + (BluetoothError)ret);
326 BluetoothErrorFactory.ThrowBluetoothException(ret);
330 internal void NotifyCurrentPosition(uint position)
332 int ret = Interop.Bluetooth.NotifyCurrentPosition(position);
333 if (ret != (int)BluetoothError.None)
335 Log.Error(Globals.LogTag, "Failed to notify position to remote device, Error - " + (BluetoothError)ret);
336 BluetoothErrorFactory.ThrowBluetoothException(ret);
340 internal void NotifyTrack(Track trackData)
342 string title = trackData.Title;
343 string artist = trackData.Artist;
344 string album = trackData.Album;
345 string genre = trackData.Genre;
346 uint trackNum = trackData.TrackNum;
347 uint totalTracks = trackData.TotalTracks;
348 uint duration = trackData.Duration;
350 int ret = Interop.Bluetooth.NotifyTrack(title, artist, album, genre, trackNum, totalTracks, duration);
351 if (ret != (int)BluetoothError.None)
353 Log.Error(Globals.LogTag, "Failed to notify track data to the remote device, Error - " + (BluetoothError)ret);
354 BluetoothErrorFactory.ThrowBluetoothException(ret);
358 internal static BluetoothAvrcpImpl Instance
366 private BluetoothAvrcpImpl()
371 ~BluetoothAvrcpImpl()
376 public void Dispose()
379 GC.SuppressFinalize(this);
382 private void Dispose(bool disposing)
389 // Free managed objects.
391 //Free unmanaged objects
392 targetDeinitialize();
393 RemoveAllRegisteredEvent();
397 private void RemoveAllRegisteredEvent()
399 //unregister all remaining events when this object is released.
400 if (_equalizerStateChanged != null)
402 UnregisterEqualizerStateChangedEvent();
404 if (_repeatModeChanged != null)
406 UnregisterRepeatModeChangedEvent();
408 if (_scanModeChanged != null)
410 UnregisterScanModeChangedEvent();
412 if (_shuffleModeChanged != null)
414 UnregisterShuffleModeChangedEvent();