8a20ab285b989effe69eaa33ebb67a67f44841ee
[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 using System.ComponentModel;
19
20 namespace Tizen.Network.Bluetooth
21 {
22     /// <summary>
23     /// This class is used to handle the connection with other Bluetooth audio devices
24     /// like headset, hands-free, and headphone.
25     /// </summary>
26     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
27     /// <since_tizen> 3 </since_tizen>
28     public class BluetoothAudio : BluetoothProfile
29     {
30         internal BluetoothAudio()
31         {
32         }
33
34         /// <summary>
35         /// The AudioConnectionStateChanged event is called when the audio connection state is changed.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         public event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
39         {
40             add
41             {
42                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged += value;
43             }
44             remove
45             {
46                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged -= value;
47             }
48         }
49
50         /// <summary>
51         /// Connects the remote device with the given audio profile.
52         /// </summary>
53         /// <remarks>
54         /// The device must be bonded with the remote device by CreateBond(). If connection request succeeds, the AudioConnectionStateChanged event will be invoked.
55         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br/>
56         /// and AdvancedAudioDistribution is connected.
57         /// </remarks>
58         /// <param name="profileType">The type of the audio profile.</param>
59         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
60         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
61         /// or when the connection attempt fails.</exception>
62         /// <since_tizen> 3 </since_tizen>
63         public void Connect(BluetoothAudioProfileType profileType)
64         {
65             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
66             {
67                 int ret = BluetoothAudioImpl.Instance.Connect(RemoteAddress, profileType);
68                 if (ret != (int)BluetoothError.None)
69                 {
70                     Log.Error(Globals.LogTag, "Failed to Connect - " + (BluetoothError)ret);
71                     BluetoothErrorFactory.ThrowBluetoothException(ret);
72                 }
73             }
74             else
75             {
76                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
77             }
78         }
79
80         /// <summary>
81         /// Disconnects the remote device with the given audio profile.
82         /// </summary>
83         /// <remarks>
84         /// The device must be connected by Connect(). If the disconnection request succeeds, the AudioConnectionStateChanged event will be invoked.
85         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br/>
86         /// and AdvancedAudioDistribution is disconnected.
87         /// </remarks>
88         /// <param name="type">The type of the audio profile.</param>
89         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
90         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
91         /// or when Disconnection attempt fails.</exception>
92         /// <since_tizen> 3 </since_tizen>
93         public void Disconnect(BluetoothAudioProfileType type)
94         {
95             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
96             {
97                 int ret = BluetoothAudioImpl.Instance.Disconnect(RemoteAddress, type);
98                 if (ret != (int)BluetoothError.None)
99                 {
100                     Log.Error(Globals.LogTag, "Failed to Disconnect - " + (BluetoothError)ret);
101                     BluetoothErrorFactory.ThrowBluetoothException(ret);
102                 }
103             }
104             else
105             {
106                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
107             }
108         }
109
110         /// <summary>
111         /// Opens a AG(Audio Gateway) SCO(Synchronous Connection Oriented link) to connected remote device, asynchronously.
112         /// </summary>
113         /// <since_tizen> 6 </since_tizen>
114         /// <feature>http://tizen.org/feature/network.bluetooth</feature>
115         /// <feature>http://tizen.org/feature/network.bluetooth.audio.call</feature>
116         /// <privilege>http://tizen.org/privilege/bluetooth.admin</privilege>
117         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
118         /// <exception cref="InvalidOperationException">Thrown when the method is failed with message.</exception>
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         public static void OpenAgSco()
121         {
122             BluetoothAudioImpl.Instance.OpenAgSco();
123         }
124
125         /// <summary>
126         /// Closes a AG(Audio Gateway) SCO(Synchronous Connection Oriented link) to connected remote device, asynchronously.
127         /// </summary>
128         /// <since_tizen> 6 </since_tizen>
129         /// <feature>http://tizen.org/feature/network.bluetooth</feature>
130         /// <feature>http://tizen.org/feature/network.bluetooth.audio.call</feature>
131         /// <privilege>http://tizen.org/privilege/bluetooth.admin</privilege>
132         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
133         /// <exception cref="InvalidOperationException">Thrown when the method is failed with message.</exception>
134         [EditorBrowsable(EditorBrowsableState.Never)]
135         public static void CloseAgSco()
136         {
137             BluetoothAudioImpl.Instance.CloseAgSco();
138         }
139
140         /// <summary>
141         /// A property to check whether an opened AG(Audio Gateway) SCO(Synchronous Connection Oriented link) exists or not.
142         /// </summary>
143         /// <since_tizen> 6 </since_tizen>
144         /// <feature>http://tizen.org/feature/network.bluetooth</feature>
145         /// <feature>http://tizen.org/feature/network.bluetooth.audio.call</feature>
146         [EditorBrowsable(EditorBrowsableState.Never)]
147         public static bool IsAgScoOpened
148         {
149             get
150             {   
151                 return BluetoothAudioImpl.Instance.IsAgScoOpened;
152             }
153         }
154
155         /// <summary>
156         /// This event is called when the AG(Audio Gateway) SCO(Synchronous Connection Oriented link) state is changed.
157         /// </summary>
158         /// <since_tizen> 6 </since_tizen>
159         /// <feature>http://tizen.org/feature/network.bluetooth</feature>
160         /// <feature>http://tizen.org/feature/network.bluetooth.audio.call</feature>
161         [EditorBrowsable(EditorBrowsableState.Never)]
162         public static event EventHandler<AgScoStateChangedEventArgs> AgScoStateChanged
163         {
164             add
165             {
166                 BluetoothAudioImpl.Instance.AgScoStateChanged += value;
167             }
168             remove
169             {
170                 BluetoothAudioImpl.Instance.AgScoStateChanged -= value;
171             }
172         }
173
174         /// <summary>
175         /// Notifies the state of AG(Audio Gateway) voice recognition to connected remote device.
176         /// </summary>
177         /// <param name="enable">The state of voice recognition. It is true if voice recognition state is enabled.</param>
178         /// <since_tizen> 6 </since_tizen>
179         /// <feature>http://tizen.org/feature/network.bluetooth</feature>
180         /// <feature>http://tizen.org/feature/network.bluetooth.audio.call</feature>
181         /// <privilege>http://tizen.org/privilege/bluetooth.admin</privilege>
182         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
183         /// <exception cref="InvalidOperationException">Thrown when the method is failed with message.</exception>
184         [EditorBrowsable(EditorBrowsableState.Never)]
185         public static void NotifyAgVoiceRecognitionState(bool enable)
186         {
187             BluetoothAudioImpl.Instance.NotifyAgVoiceRecognitionState(enable);
188         }
189     }
190 }