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