"bt_ag_is_sco_opened() failed! "
"res[%d] msg[%s]", res, get_error_message(res));
}
- DLOG("BT SCO open state [%d]", isOpened);
+ DLOG("SCO state [%s]", isOpened ? "opened" : "closed");
return isOpened;
}
BluetoothManager::~BluetoothManager()
{
- unregisterAudioHandling();
+ unregisterScoStateHandling();
+ unregisterVolumeChangeHandling();
unregisterHeadsetConnectionHandling();
deinitializeBluetooth();
}
FAIL_RETURN(initializeBluetooth(),
"initializeBluetooth() failed!");
- FAIL_RETURN(registerAudioHandling(),
- "registerAudioHandling() failed");
+ FAIL_RETURN(registerVolumeChangeHandling(),
+ "registerVolumeChangeHandling() failed");
+
+ FAIL_RETURN(registerScoStateHandling(),
+ "registerScoStateHandling() failed");
FAIL_RETURN(registerHeadsetConnectionHandling(),
"registerHeadsetConnectionHandling() failed!");
int BluetoothManager::getVolume() const
{
auto vol = 0;
- auto res = bt_ag_get_speaker_gain(&vol);
- if (res != BT_ERROR_NONE) {
- LOG_RETURN_VALUE(RES_FAIL, -1, "bt_ag_get_speaker_gain() failed! "
- "res[%d] msg[%s]", res, get_error_message(res));
+ if (impl::isScoOpened() && impl::isCallCurrentSoundType()) {
+ auto res = bt_ag_get_speaker_gain(&vol);
+ if (res != BT_ERROR_NONE) {
+ LOG_RETURN_VALUE(RES_FAIL, -1,
+ "bt_ag_get_speaker_gain() failed! "
+ "res[%d] msg[%s]", res, get_error_message(res));
+ }
}
+
DLOG("BT Volume level [%d]", vol);
return vol;
}
Result BluetoothManager::setVolume(int volume)
{
- auto res = bt_ag_notify_speaker_gain(volume);
- if (res != BT_ERROR_NONE) {
- LOG_RETURN(RES_FAIL, "bt_ag_notify_speaker_gain() failed! "
- "res[%d] msg[%s]", res, get_error_message(res));
+ if (impl::isScoOpened() && impl::isCallCurrentSoundType()) {
+ auto res = bt_ag_notify_speaker_gain(volume);
+ if (res != BT_ERROR_NONE) {
+ LOG_RETURN(RES_FAIL, "bt_ag_notify_speaker_gain() failed! "
+ "res[%d] msg[%s]", res, get_error_message(res));
+ }
+ return RES_OK;
}
- return RES_OK;
+
+ return RES_FAIL;
}
- void BluetoothManager::setVolumeStateHandler(NotiHandler handler)
+ void BluetoothManager::setVolumeChangeHandler(NotiHandler handler)
{
m_btVolumeHandler = std::move(handler);
}
- Result BluetoothManager::registerAudioHandling()
+ Result BluetoothManager::registerVolumeChangeHandling()
{
auto res = bt_audio_initialize();
if (res != BT_ERROR_NONE) {
return RES_OK;
}
- void BluetoothManager::unregisterAudioHandling()
+ void BluetoothManager::unregisterVolumeChangeHandling()
{
bt_ag_unset_speaker_gain_changed_cb();
}
}
+ Result BluetoothManager::registerScoStateHandling()
+ {
+ auto res = bt_ag_set_sco_state_changed_cb(
+ CALLBACK_B(BluetoothManager::onScoChanged), this);
+ if (res != BT_ERROR_NONE) {
+ LOG_RETURN(RES_FAIL, "bt_ag_set_sco_state_changed_cb() failed! ",
+ "res[%d] msg[%s]", res, get_error_message(res));
+ }
+ return RES_OK;
+ }
+
+ void BluetoothManager::unregisterScoStateHandling()
+ {
+ bt_ag_unset_sco_state_changed_cb();
+ }
+
Result BluetoothManager::registerHeadsetConnectionHandling()
{
auto res = vconf_notify_key_changed(VCONFKEY_BT_DEVICE,
void BluetoothManager::unregisterHeadsetConnectionHandling()
{
- vconf_ignore_key_changed(
- VCONFKEY_BT_DEVICE,
+ vconf_ignore_key_changed(VCONFKEY_BT_DEVICE,
CALLBACK_B(BluetoothManager::onHeadsetConnectionChanged));
}
void BluetoothManager::onVolumeChanged(int volume)
+ {
+ DLOG();
+ notifyVolumeChange();
+ }
+
+ void BluetoothManager::onScoChanged(int result, bool opened)
+ {
+ DLOG("Result [%d] SCO state [%s]", result,
+ opened ? "opened" : "close");
+
+ if (result == BT_ERROR_NONE && opened) {
+ notifyVolumeChange();
+ }
+ }
+
+ void BluetoothManager::notifyVolumeChange()
{
if (impl::isScoOpened() && impl::isCallCurrentSoundType()) {
+ DLOG("Try call volume change handler...");
if (const auto handler = m_btVolumeHandler.lock()) {
+ DLOG("Call volume change handler");
handler();
}
}
ucl::Result setVolume(int volume);
/**
- * @brief Sets volume state change handler
+ * @brief Sets volume level change handler
* @param[in] handler Volume state change handler
*/
- void setVolumeStateHandler(NotiHandler handler);
+ void setVolumeChangeHandler(NotiHandler handler);
/**
* @brief Checks whether headset device is connected
ucl::Result initializeBluetooth();
void deinitializeBluetooth();
- ucl::Result registerAudioHandling();
- void unregisterAudioHandling();
+ ucl::Result registerVolumeChangeHandling();
+ void unregisterVolumeChangeHandling();
void onVolumeChanged(int volume);
+ ucl::Result registerScoStateHandling();
+ void unregisterScoStateHandling();
+ void onScoChanged(int result, bool opened);
+
+ void notifyVolumeChange();
+
ucl::Result registerHeadsetConnectionHandling();
void unregisterHeadsetConnectionHandling();