Commit ce596e54 authored by Curt Clemens's avatar Curt Clemens Committed by Commit Bot

[NearbyShare] Reimplement BTMedium's FindRemoteDevice as GetRemoteDevice

BluetoothMediumClassic::GetRemoteDevice should now return a new device
with the given mac address if one hasn't been added yet.

Bug: 1131981
Test: unit_tests
Change-Id: I5a8f19469799074e667bfa05f3ae667d789d8572
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2435821
Commit-Queue: Curt Clemens <cclem@google.com>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811411}
parent e3762251
......@@ -73,15 +73,7 @@ bool BluetoothClassicMedium::StopDiscovery() {
std::unique_ptr<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
api::BluetoothDevice& remote_device,
const std::string& service_uuid) {
// TODO(hansberry): This currently assumes that the device was discovered via
// Bluetooth Classic (the remote device is in high visibility mode), meaning
// this address is the expected permanent BT MAC address. Once an
// implementation is in place to scan for devices over BLE, a new mechanism
// to query for the remote device's permanent BT MAC address from stored
// certificates will be needed.
// We provided this |remote_device|, so we can safely downcast it.
const std::string& address =
static_cast<chrome::BluetoothDevice&>(remote_device).GetAddress();
const std::string& address = remote_device.GetMacAddress();
bluetooth::mojom::ConnectToServiceResultPtr result;
bool success = adapter_->ConnectToServiceInsecurely(
......@@ -114,7 +106,18 @@ BluetoothClassicMedium::ListenForService(const std::string& service_name,
BluetoothDevice* BluetoothClassicMedium::GetRemoteDevice(
const std::string& mac_address) {
auto it = discovered_bluetooth_devices_map_.find(mac_address);
return it == discovered_bluetooth_devices_map_.end() ? nullptr : &it->second;
if (it != discovered_bluetooth_devices_map_.end())
return &it->second;
// If a device with |mac_address| has not been found, Nearby Connections
// is attempting to connect to a device with |mac_adress| which is not
// discoverable. Create a placeholder BluetoothDevice to be used by
// ConnectToService().
bluetooth::mojom::DeviceInfoPtr device = bluetooth::mojom::DeviceInfo::New();
device->address = mac_address;
return &discovered_bluetooth_devices_map_
.emplace(mac_address, std::move(device))
.first->second;
}
void BluetoothClassicMedium::PresentChanged(bool present) {
......
......@@ -156,11 +156,17 @@ TEST_F(BluetoothClassicMediumTest, TestDiscovery_StartDiscoveryError) {
}
TEST_F(BluetoothClassicMediumTest,
TestDiscovery_DeviceDiscovered_BluetoothClassicDevice) {
TestDiscovery_GetRemoteDevice_GetUndiscovered) {
StartDiscovery();
ASSERT_FALSE(bluetooth_classic_medium_->GetRemoteDevice(kDeviceAddress1));
ASSERT_FALSE(bluetooth_classic_medium_->GetRemoteDevice(kDeviceAddress2));
EXPECT_TRUE(bluetooth_classic_medium_->GetRemoteDevice(kDeviceAddress1));
StopDiscovery();
}
TEST_F(BluetoothClassicMediumTest,
TestDiscovery_DeviceDiscovered_BluetoothClassicDevice) {
StartDiscovery();
NotifyDeviceAdded(kDeviceAddress1, kDeviceName1);
EXPECT_TRUE(bluetooth_classic_medium_->GetRemoteDevice(kDeviceAddress1));
......
......@@ -18,11 +18,6 @@ std::string BluetoothDevice::GetName() const {
}
std::string BluetoothDevice::GetMacAddress() const {
// TODO(hansberry): Implement.
return std::string();
}
std::string BluetoothDevice::GetAddress() const {
return device_info_->address;
}
......
......@@ -27,7 +27,6 @@ class BluetoothDevice : public api::BluetoothDevice {
std::string GetName() const override;
std::string GetMacAddress() const override;
std::string GetAddress() const;
void UpdateDeviceInfo(bluetooth::mojom::DeviceInfoPtr device_info);
private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment