Release 4.0.0-preview1-00051
[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     public class BluetoothAudio : BluetoothProfile
27     {
28         internal BluetoothAudio()
29         {
30         }
31
32         /// <summary>
33         /// The AudioConnectionStateChanged event is called when the audio connection state is changed.
34         /// </summary>
35         public event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
36         {
37             add
38             {
39                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged += value;
40             }
41             remove
42             {
43                 BluetoothAudioImpl.Instance.AudioConnectionStateChanged -= value;
44             }
45         }
46
47         /// <summary>
48         /// Connects the remote device with the given audio profile.
49         /// </summary>
50         /// <remarks>
51         /// The device must be bonded with the remote device by CreateBond(). If connection request succeeds, the AudioConnectionStateChanged event will be invoked.
52         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br>
53         /// and AdvancedAudioDistribution is connected.
54         /// </remarks>
55         /// <param name="profileType">The type of the audio profile.</param>
56         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
57         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
58         /// or when the connection attempt fails.</exception>
59         public void Connect(BluetoothAudioProfileType profileType)
60         {
61             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
62             {
63                 int ret = BluetoothAudioImpl.Instance.Connect(RemoteAddress, profileType);
64                 if (ret != (int)BluetoothError.None)
65                 {
66                     Log.Error(Globals.LogTag, "Failed to Connect - " + (BluetoothError)ret);
67                     BluetoothErrorFactory.ThrowBluetoothException(ret);
68                 }
69             }
70             else
71             {
72                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
73             }
74         }
75
76         /// <summary>
77         /// Disconnects the remote device with the given audio profile.
78         /// </summary>
79         /// <remarks>
80         /// The device must be connected by Connect(). If the disconnection request succeeds, the AudioConnectionStateChanged event will be invoked.
81         /// If audio profile type is All and this request succeeds, then the AudioConnectionStateChanged event will be called twice when HspHfp <br>
82         /// and AdvancedAudioDistribution is disconnected.
83         /// </remarks>
84         /// <param name="type">The type of the audio profile.</param>
85         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
86         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
87         /// or when Disconnection attempt fails.</exception>
88         public void Disconnect(BluetoothAudioProfileType type)
89         {
90             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
91             {
92                 int ret = BluetoothAudioImpl.Instance.Disconnect(RemoteAddress, type);
93                 if (ret != (int)BluetoothError.None)
94                 {
95                     Log.Error(Globals.LogTag, "Failed to Disconnect - " + (BluetoothError)ret);
96                     BluetoothErrorFactory.ThrowBluetoothException(ret);
97                 }
98             }
99             else
100             {
101                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
102             }
103         }
104     }
105 }