Add more logs for BT.

PiperOrigin-RevId: 576329921
This commit is contained in:
Hai Shang
2023-10-24 17:01:23 -07:00
committed by Copybara-Service
parent d706984330
commit f93959fd15
+33 -3
View File
@@ -20,23 +20,28 @@
namespace nearby {
BluetoothClassicMedium::~BluetoothClassicMedium() {
NEARBY_LOG(INFO, "~BluetoothClassicMedium: observer_list_ size: %d",
observer_list_.size());
if (!observer_list_.empty()) {
impl_->RemoveObserver(this);
}
StopDiscovery();
NEARBY_LOG(INFO, "eof ~BluetoothClassicMedium");
}
BluetoothSocket BluetoothClassicMedium::ConnectToService(
BluetoothDevice& remote_device, const std::string& service_uuid,
CancellationFlag* cancellation_flag) {
NEARBY_LOG(INFO,
"BluetoothClassicMedium::ConnectToService: device=%p [impl=%p]",
&remote_device, &remote_device.GetImpl());
"BluetoothClassicMedium::ConnectToService: service_uuid=%p, "
"device=%p, [impl=%p]",
service_uuid.c_str(), &remote_device, &remote_device.GetImpl());
return BluetoothSocket(impl_->ConnectToService(
remote_device.GetImpl(), service_uuid, cancellation_flag));
}
bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
NEARBY_LOG(INFO, "BluetoothClassicMedium::StartDiscovery");
MutexLock lock(&mutex_);
if (discovery_enabled_) {
NEARBY_LOG(INFO, "BT Discovery already enabled; impl=%p", &GetImpl());
@@ -45,6 +50,8 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
bool success = impl_->StartDiscovery({
.device_discovered_cb =
[this](api::BluetoothDevice& device) {
NEARBY_LOG(VERBOSE, "BT .device_discovered_cb for %p",
device.GetName().c_str());
MutexLock lock(&mutex_);
auto pair = devices_.emplace(
&device, absl::make_unique<DeviceDiscoveryInfo>());
@@ -62,6 +69,8 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
},
.device_name_changed_cb =
[this](api::BluetoothDevice& device) {
NEARBY_LOG(VERBOSE, "BT .device_name_changed_cb for %p",
device.GetName().c_str());
MutexLock lock(&mutex_);
// If the device is not already in devices_, we should not be able
// to change its name.
@@ -74,6 +83,8 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
},
.device_lost_cb =
[this](api::BluetoothDevice& device) {
NEARBY_LOG(VERBOSE, "BT .device_lost_cb for %p",
device.GetName().c_str());
MutexLock lock(&mutex_);
auto item = devices_.extract(&device);
if (!item) {
@@ -92,12 +103,13 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
discovery_callback_ = std::move(callback);
devices_.clear();
discovery_enabled_ = true;
NEARBY_LOG(INFO, "BT Discovery enabled; impl=%p", &GetImpl());
}
NEARBY_LOG(INFO, "BT StartDiscovery result:%d; impl=%p", success, &GetImpl());
return success;
}
bool BluetoothClassicMedium::StopDiscovery() {
NEARBY_LOG(INFO, "BT StopDiscovery; impl=%p", &GetImpl());
MutexLock lock(&mutex_);
if (!discovery_enabled_) return true;
discovery_enabled_ = false;
@@ -108,28 +120,36 @@ bool BluetoothClassicMedium::StopDiscovery() {
}
void BluetoothClassicMedium::AddObserver(Observer* observer) {
NEARBY_LOG(INFO, "BT AddObserver; impl=%p", &GetImpl());
MutexLock lock(&mutex_);
if (observer_list_.empty()) {
impl_->AddObserver(this);
}
observer_list_.AddObserver(observer);
NEARBY_LOG(INFO, "BT AddObserver done");
}
void BluetoothClassicMedium::RemoveObserver(Observer* observer) {
NEARBY_LOG(INFO, "BT RemoveObserver; impl=%p", &GetImpl());
MutexLock lock(&mutex_);
observer_list_.RemoveObserver(observer);
if (observer_list_.empty()) {
impl_->RemoveObserver(this);
}
NEARBY_LOG(INFO, "BT RemoveObserver done");
}
// api::BluetoothClassicMedium::Observer methods
void BluetoothClassicMedium::DeviceAdded(api::BluetoothDevice& device) {
NEARBY_LOG(VERBOSE, "BT DeviceAdded; name=%p, address=%p",
device.GetName().c_str(), device.GetMacAddress().c_str());
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceAdded(bt_device);
}
}
void BluetoothClassicMedium::DeviceRemoved(api::BluetoothDevice& device) {
NEARBY_LOG(VERBOSE, "BT DeviceRemoved; name=%p, address=%p",
device.GetName().c_str(), device.GetMacAddress().c_str());
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceRemoved(bt_device);
@@ -137,6 +157,9 @@ void BluetoothClassicMedium::DeviceRemoved(api::BluetoothDevice& device) {
}
void BluetoothClassicMedium::DeviceAddressChanged(
api::BluetoothDevice& device, absl::string_view old_address) {
NEARBY_LOG(
VERBOSE, "BT DeviceAddressChanged; name=%p, address=%p, old_address=%p",
device.GetName().c_str(), device.GetMacAddress().c_str(), old_address);
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceAddressChanged(bt_device, old_address);
@@ -144,6 +167,9 @@ void BluetoothClassicMedium::DeviceAddressChanged(
}
void BluetoothClassicMedium::DevicePairedChanged(api::BluetoothDevice& device,
bool new_paired_status) {
NEARBY_LOG(VERBOSE, "BT DevicePairedChanged; name=%p, address=%p, status=%d",
device.GetName().c_str(), device.GetMacAddress().c_str(),
new_paired_status);
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DevicePairedChanged(bt_device, new_paired_status);
@@ -151,6 +177,10 @@ void BluetoothClassicMedium::DevicePairedChanged(api::BluetoothDevice& device,
}
void BluetoothClassicMedium::DeviceConnectedStateChanged(
api::BluetoothDevice& device, bool connected) {
NEARBY_LOG(
VERBOSE,
"BT DeviceConnectedStateChanged: name=%p, address=%p, connected=%d",
device.GetName().c_str(), device.GetMacAddress().c_str(), connected);
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceConnectedStateChanged(bt_device, connected);