8ff829dbc5669acc824d46d44f26f6ba5b2ae637
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothAudio.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Network.Bluetooth
20 {
21     /// <summary>
22     /// This class is used to handle the connection with other Bluetooth audio devices
23     /// like headset, hands-free, and headphone.
24     /// </summary>
25     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
26     /// <since_tizen> 3 </since_tizen>
27     public class BluetoothAudio : BluetoothProfile
28     {
29         internal BluetoothAudio()
30         {
31         }
32
33         /// <summary>
34         /// The AudioConnectionStateChanged event is called when the audio connection state is changed.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         public event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
38         {
39             add
40             {
41                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged += value;
42             }
43             remove
44             {
45                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged -= value;
46             }
47         }
48
49         /// <summary>
50         /// Connects the remote device with the given audio profile.
51         /// </summary>
52         /// <remarks>
53         /// The device must be bonded with the remote device by CreateBond(). If connection request succeeds, the AudioConnectionStateChanged event will be invoked.
54         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br/>
55         /// and AdvancedAudioDistribution is connected.
56         /// </remarks>
57         /// <param name="profileType">The type of the audio profile.</param>
58         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
59         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
60         /// or when the connection attempt fails.</exception>
61         /// <since_tizen> 3 </since_tizen>
62         public void Connect(BluetoothAudioProfileType profileType)
63         {
64             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
65             {
66                 int ret = BluetoothAudioImpl.Instance.Connect(RemoteAddress, profileType);
67                 if (ret != (int)BluetoothError.None)
68                 {
69                     Log.Error(Globals.LogTag, "Failed to Connect - " + (BluetoothError)ret);
70                     BluetoothErrorFactory.ThrowBluetoothException(ret);
71                 }
72             }
73             else
74             {
75                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
76             }
77         }
78
79         /// <summary>
80         /// Disconnects the remote device with the given audio profile.
81         /// </summary>
82         /// <remarks>
83         /// The device must be connected by Connect(). If the disconnection request succeeds, the AudioConnectionStateChanged event will be invoked.
84         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br/>
85         /// and AdvancedAudioDistribution is disconnected.
86         /// </remarks>
87         /// <param name="type">The type of the audio profile.</param>
88         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
89         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
90         /// or when Disconnection attempt fails.</exception>
91         /// <since_tizen> 3 </since_tizen>
92         public void Disconnect(BluetoothAudioProfileType type)
93         {
94             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
95             {
96                 int ret = BluetoothAudioImpl.Instance.Disconnect(RemoteAddress, type);
97                 if (ret != (int)BluetoothError.None)
98                 {
99                     Log.Error(Globals.LogTag, "Failed to Disconnect - " + (BluetoothError)ret);
100                     BluetoothErrorFactory.ThrowBluetoothException(ret);
101                 }
102             }
103             else
104             {
105                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
106             }
107         }
108     }
109 }