diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm index 5fb53a38f00d..fe5f88824d1e 100644 --- a/device/bluetooth/bluetooth_adapter_mac.mm +++ b/device/bluetooth/bluetooth_adapter_mac.mm @@ -31,10 +31,6 @@ - (NSString*)nameAsString; - (BluetoothHCIPowerState)powerState; @end -@interface IOBluetoothDevice (LionSDKDeclarations) -- (NSString*)addressString; -@end - @protocol IOBluetoothDeviceInquiryDelegate - (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry*)sender; - (void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender @@ -232,9 +228,8 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender if (controller != nil) { name = base::SysNSStringToUTF8([controller nameAsString]); - // TODO(isherman): Convert the address format to XX:XX:XX:XX:XX:XX rather - // than xx-xx-xx-xx-xx-xx. - address = base::SysNSStringToUTF8([controller addressAsString]); + address = BluetoothDeviceMac::NormalizeAddress( + base::SysNSStringToUTF8([controller addressAsString])); powered = ([controller powerState] == kBluetoothHCIPowerStateON); } @@ -273,8 +268,7 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender void BluetoothAdapterMac::UpdateDevices(NSArray* devices) { STLDeleteValues(&devices_); for (IOBluetoothDevice* device in devices) { - std::string device_address = - base::SysNSStringToUTF8([device addressString]); + std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); devices_[device_address] = new BluetoothDeviceMac(device); } } @@ -298,7 +292,7 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender void BluetoothAdapterMac::DeviceFound(IOBluetoothDeviceInquiry* inquiry, IOBluetoothDevice* device) { DCHECK_EQ(device_inquiry_, inquiry); - std::string device_address = base::SysNSStringToUTF8([device addressString]); + std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); if (discovered_devices_.find(device_address) == discovered_devices_.end()) { BluetoothDeviceMac device_mac(device); FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, diff --git a/device/bluetooth/bluetooth_device_mac.h b/device/bluetooth/bluetooth_device_mac.h index 1503f1dc1277..7bb7206dc077 100644 --- a/device/bluetooth/bluetooth_device_mac.h +++ b/device/bluetooth/bluetooth_device_mac.h @@ -70,6 +70,16 @@ class BluetoothDeviceMac : public BluetoothDevice { const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; + // Returns the Bluetooth address for the |device|. The returned address has a + // normalized format (see below). + static std::string GetDeviceAddress(IOBluetoothDevice* device); + + // Returns the address formatted according to Chrome's expectations rather + // than per the system convention: octets are separated by colons rather than + // by dashes. That is, the returned format is XX:XX:XX:XX:XX:XX rather than + // xx-xx-xx-xx-xx-xx. + static std::string NormalizeAddress(const std::string& address); + protected: // BluetoothDevice override virtual std::string GetDeviceName() const OVERRIDE; diff --git a/device/bluetooth/bluetooth_device_mac.mm b/device/bluetooth/bluetooth_device_mac.mm index aaeffb8fa632..73dc8bf9ea32 100644 --- a/device/bluetooth/bluetooth_device_mac.mm +++ b/device/bluetooth/bluetooth_device_mac.mm @@ -10,6 +10,7 @@ #include "base/hash.h" #include "base/sequenced_task_runner.h" #include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" @@ -70,7 +71,7 @@ @interface IOBluetoothHostController (UndocumentedAPI) } std::string BluetoothDeviceMac::GetAddress() const { - return base::SysNSStringToUTF8([device_ addressString]); + return GetDeviceAddress(device_); } BluetoothDevice::VendorIDSource BluetoothDeviceMac::GetVendorIDSource() const { @@ -234,4 +235,17 @@ @interface IOBluetoothHostController (UndocumentedAPI) return power_level; } +// static +std::string BluetoothDeviceMac::GetDeviceAddress(IOBluetoothDevice* device) { + return NormalizeAddress(base::SysNSStringToUTF8([device addressString])); +} + +// static +std::string BluetoothDeviceMac::NormalizeAddress(const std::string& address) { + std::string normalized; + base::ReplaceChars(address, "-", ":", &normalized); + StringToUpperASCII(&normalized); + return normalized; +} + } // namespace device diff --git a/device/bluetooth/bluetooth_profile_mac.mm b/device/bluetooth/bluetooth_profile_mac.mm index 6ef1601151b0..f6d5ee72fcea 100644 --- a/device/bluetooth/bluetooth_profile_mac.mm +++ b/device/bluetooth/bluetooth_profile_mac.mm @@ -18,16 +18,6 @@ #include "device/bluetooth/bluetooth_device_mac.h" #include "device/bluetooth/bluetooth_socket_mac.h" -// Replicate specific 10.7 SDK declarations for building with prior SDKs. -#if !defined(MAC_OS_X_VERSION_10_7) || \ - MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 - -@interface IOBluetoothDevice (LionSDKDeclarations) -- (NSString*)addressString; -@end - -#endif // MAC_OS_X_VERSION_10_7 - namespace device { namespace { @@ -281,7 +271,7 @@ - (void)rfcommChannelOpened:(IOBluetoothUserNotification*)notification return; } - std::string device_address = base::SysNSStringToUTF8([device addressString]); + std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); BluetoothSocketMac::Connect( record, base::Bind(OnConnectSuccess, @@ -295,7 +285,7 @@ - (void)rfcommChannelOpened:(IOBluetoothUserNotification*)notification IOBluetoothRFCOMMChannel* rfcomm_channel) { DCHECK_EQ([rfcomm_channel getChannelID], rfcomm_channel_id_); std::string device_address = - base::SysNSStringToUTF8([[rfcomm_channel getDevice] addressString]); + BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel getDevice]); BluetoothSocketMac::AcceptConnection( rfcomm_channel, base::Bind(OnConnectSuccess, diff --git a/device/bluetooth/bluetooth_socket_mac.mm b/device/bluetooth/bluetooth_socket_mac.mm index c33243040bcb..cdbc8dd4c69f 100644 --- a/device/bluetooth/bluetooth_socket_mac.mm +++ b/device/bluetooth/bluetooth_socket_mac.mm @@ -18,20 +18,11 @@ #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "base/threading/thread_restrictions.h" +#include "device/bluetooth/bluetooth_device_mac.h" #include "device/bluetooth/bluetooth_service_record.h" #include "device/bluetooth/bluetooth_service_record_mac.h" #include "net/base/io_buffer.h" -// Replicate specific 10.7 SDK declarations for building with prior SDKs. -#if !defined(MAC_OS_X_VERSION_10_7) || \ - MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 - -@interface IOBluetoothDevice (LionSDKDeclarations) -- (NSString*)addressString; -@end - -#endif // MAC_OS_X_VERSION_10_7 - @interface BluetoothRFCOMMChannelDelegate : NSObject { @private @@ -181,7 +172,7 @@ void empty_queue(std::queue& queue) { if (status != kIOReturnSuccess) { std::stringstream error; error << "Failed to connect bluetooth socket (" - << base::SysNSStringToUTF8([device addressString]) << "): (" << status + << BluetoothDeviceMac::GetDeviceAddress(device) << "): (" << status << ")"; error_callback.Run(error.str()); return; @@ -218,9 +209,8 @@ void empty_queue(std::queue& queue) { ReleaseChannel(); std::stringstream error; error << "Failed to connect bluetooth socket (" - << base::SysNSStringToUTF8( - [[rfcomm_channel_ getDevice] addressString]) << "): (" - << status << ")"; + << BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice]) + << "): (" << status << ")"; temp->error_callback.Run(error.str()); return; } @@ -339,8 +329,7 @@ void empty_queue(std::queue& queue) { if (status != kIOReturnSuccess) { std::stringstream error; error << "Failed to connect bluetooth socket (" - << base::SysNSStringToUTF8( - [[rfcomm_channel_ getDevice] addressString]) + << BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice]) << "): (" << status << ")"; // Remember the first error only if (request->status == kIOReturnSuccess) @@ -395,8 +384,7 @@ void empty_queue(std::queue& queue) { if (!request->error_signaled) { std::stringstream error; error << "Failed to connect bluetooth socket (" - << base::SysNSStringToUTF8( - [[rfcomm_channel_ getDevice] addressString]) + << BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice]) << "): (" << status << ")"; request->error_signaled = true; request->error_callback.Run(error.str());