1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief The framework interface and callback function of a2dp sink are defined. 21 * 22 * @since 6 23 */ 24 25 #ifndef BLUETOOTH_A2DP_SNK_H 26 #define BLUETOOTH_A2DP_SNK_H 27 28 #include <cstdio> 29 #include "bluetooth_types.h" 30 #include "bluetooth_remote_device.h" 31 32 namespace OHOS { 33 namespace Bluetooth { 34 /** 35 * @brief A2dp sink API callback function. 36 * 37 * @since 6.0 38 */ 39 class A2dpSinkObserver { 40 public: 41 /** 42 * @brief A destructor used to delete the a2dp sink Observer instance. 43 * 44 * @since 6.0 45 */ 46 virtual ~A2dpSinkObserver() = default; 47 48 /** 49 * @brief ConnectionState Changed observer 50 * @param device bluetooth device address 51 * @param state Event Reported 52 * @since 6.0 53 */ OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state)54 virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) 55 {}; 56 }; 57 58 /** 59 * @brief A2dp sink API. 60 * 61 * @since 6.0 62 */ 63 class BLUETOOTH_API A2dpSink { 64 public: 65 /** 66 * @brief Get a2dp sink instance. 67 * 68 * @return Returns an instance of a2dp sink. 69 * @since 6.0 70 */ 71 static A2dpSink *GetProfile(); 72 73 /** 74 * @brief Get devices by connection states. 75 * 76 * @param states The connection states of the bluetooth device. 77 * @return Returns devices that match the connection states. 78 * @since 6.0 79 */ 80 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) const; 81 82 /** 83 * @brief Get device connection state by address. 84 * 85 * @param device The address of the peer bluetooth device. 86 * @return Returns <b>A2DP_DISCONNECTED</b> if device connect state is disconnected; 87 * Returns <b>A2DP_DISCONNECTING</b> if device connect state is disconnecting; 88 * Returns <b>A2DP_CONNECTED</b> if device connect state is connected; 89 * Returns <b>A2DP_CONNECTING</b> if device connect state is connecting; 90 * Returns <b>A2DP_INVALID_STATUS</b> if can not find peer device. 91 * @since 6.0 92 */ 93 int GetDeviceState(const BluetoothRemoteDevice &device) const; 94 95 /** 96 * @brief Get device playing state by address when peer device is on connected. 97 * 98 * @param device The address of the peer bluetooth device. 99 * @return Returns <b>1</b> if device is on playing; 100 * Returns <b>0</b> if device is not on playing. 101 * @since 6.0 102 */ 103 int GetPlayingState(const BluetoothRemoteDevice &device) const; 104 105 /** 106 * @brief Get device playing state by address when peer device is on connected. 107 * 108 * @param device The address of the peer bluetooth device. 109 * @param state The playing state of the peer bluetooth device. 110 * @return Returns <b>1</b> if device is on playing; 111 * Returns <b>0</b> if device is not on playing. 112 * @since 6.0 113 */ 114 int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const; 115 116 /** 117 * @brief Connect to the peer bluetooth device. 118 * 119 * @param device The address of the peer bluetooth device. 120 * @return Returns <b>true</b> Perform normal connection processing. 121 * Returns <b>false</b> Target device is on connected,or connecting, 122 or device is not allowed to connect,or the connection fails. 123 * @since 6.0 124 */ 125 bool Connect(const BluetoothRemoteDevice &device); 126 127 /** 128 * @brief Disconnect with the peer bluetooth service. 129 * 130 * @param device The address of the peer bluetooth device. 131 * @return Returns <b>true</b> if perform normal disconnection processing. 132 * Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails. 133 * @since 6.0 134 */ 135 bool Disconnect(const BluetoothRemoteDevice &device); 136 137 /** 138 * @brief Set connection strategy for peer bluetooth device. 139 * If peer device is connected and the policy is set not allowed,then perform disconnect operation. 140 * If peer device is disconnected and the policy is set allowed,then perform connect operation. 141 * 142 * @param device The address of the peer bluetooth device. 143 * @param strategy The device connect strategy. 144 * @return Returns <b>true</b> if the operation is successful. 145 * Returns <b>false</b> if the operation fails. 146 * @since 6.0 147 */ 148 bool SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy); 149 150 /** 151 * @brief Get connection strategy of peer bluetooth device. 152 * 153 * @param device The address of the peer bluetooth device. 154 * @return Returns <b>A2DP_CONNECT_POLICY_AGREE</b> if the peer device is allowed to connect. 155 * Returns <b>A2DP_CONNECT_POLICY_DISAGREE</b> if the peer device is not allowed to connect. 156 * @since 6.0 157 */ 158 int GetConnectStrategy(const BluetoothRemoteDevice &device) const; 159 160 /** 161 * @brief Send delay reporting. 162 * 163 * @param device The address of the peer bluetooth device. 164 * @param delayValue The delay value. 165 * @return Returns <b>true</b> Set delay reporting success. 166 * Returns <b>false</b> Set delay reporting failed, 167 */ 168 bool SendDelay(const BluetoothRemoteDevice &device, uint16_t delayValue); 169 170 /** 171 * @brief Register callback function of framework. 172 * 173 * @param observer Reference to the a2dp sink observer. 174 * @since 6.0 175 */ 176 void RegisterObserver(std::shared_ptr<A2dpSinkObserver> observer); 177 178 /** 179 * @brief Deregister callback function of framework. 180 * 181 * @param observer Reference to the a2dp sink observer. 182 * @since 6.0 183 */ 184 void DeregisterObserver(std::shared_ptr<A2dpSinkObserver> observer); 185 186 /** 187 * @brief The external process calls the A2dpSink profile interface before the Bluetooth process starts. At this 188 * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the 189 * A2dpSink proflie. 190 */ 191 void Init(); 192 193 private: 194 /** 195 * @brief A constructor used to create a a2dp sink instance. 196 * 197 * @since 6.0 198 */ 199 A2dpSink(void); 200 201 /** 202 * @brief A destructor used to delete the a2dp sink instance. 203 * 204 * @since 6.0 205 */ 206 ~A2dpSink(void); 207 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSink); 208 BLUETOOTH_DECLARE_IMPL(); 209 }; 210 } // namespace Bluetooth 211 } // namespace OHOS 212 #endif // BLUETOOTH_A2DP_SNK_H