Index: device/bluetooth/bluetooth_device_mac.mm |
diff --git a/device/bluetooth/bluetooth_device_mac.mm b/device/bluetooth/bluetooth_device_mac.mm |
index df0b0af43c8da5b753db3c425f8f6fd01c437b6a..eb080daa4e26245b8b14813461645113c5ace581 100644 |
--- a/device/bluetooth/bluetooth_device_mac.mm |
+++ b/device/bluetooth/bluetooth_device_mac.mm |
@@ -7,11 +7,13 @@ |
#include <IOBluetooth/Bluetooth.h> |
#import <IOBluetooth/objc/IOBluetoothDevice.h> |
#import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> |
+#import <IOBluetooth/objc/IOBluetoothSDPUUID.h> |
#include <string> |
#include "base/basictypes.h" |
#include "base/hash.h" |
+#include "base/string_number_conversions.h" |
#include "base/stringprintf.h" |
#include "base/strings/sys_string_conversions.h" |
#include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
@@ -31,46 +33,57 @@ MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
#endif // MAC_OS_X_VERSION_10_7 |
-namespace device { |
+namespace { |
-BluetoothDeviceMac::BluetoothDeviceMac(const IOBluetoothDevice* device) |
- : BluetoothDevice(), |
- device_fingerprint_(ComputeDeviceFingerprint(device)) { |
- name_ = base::SysNSStringToUTF8([device name]); |
- address_ = base::SysNSStringToUTF8([device addressString]); |
- bluetooth_class_ = [device classOfDevice]; |
- connected_ = [device isConnected]; |
- paired_ = [device isPaired]; |
+// Converts |uuid| to a IOBluetoothSDPUUID instance. |
+// |
+// |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. |
+IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) { |
+ std::string numbers_only = uuid; |
Mark Mentovai
2013/04/11 21:48:58
DCHECK that the string has the right length, and t
youngki
2013/04/11 22:14:29
Done.
|
+ numbers_only.erase(8, 1); |
Mark Mentovai
2013/04/11 21:48:58
It’s a tiny and pointless bit less memmoving if yo
youngki
2013/04/11 22:14:29
Done.
|
+ numbers_only.erase(12, 1); |
+ numbers_only.erase(16, 1); |
+ numbers_only.erase(20, 1); |
+ std::vector<uint8> uuid_bytes_vector; |
+ base::HexStringToBytes(numbers_only, &uuid_bytes_vector); |
Mark Mentovai
2013/04/11 21:48:58
…DCHECK that uuid_bytes_vector is the expected siz
youngki
2013/04/11 22:14:29
Done.
|
+ uint8 uuid_bytes[16]; |
Mark Mentovai
2013/04/11 21:48:58
Why the separate array and the copy? Why not uuidW
youngki
2013/04/11 22:14:29
Done.
|
+ std::copy(uuid_bytes_vector.begin(), |
+ uuid_bytes_vector.end(), |
+ uuid_bytes); |
- for (IOBluetoothSDPServiceRecord* service in [device services]) { |
- BluetoothServiceRecord* service_record = |
- new BluetoothServiceRecordMac(service); |
- service_record_list_.push_back(service_record); |
- service_uuids_.push_back(service_record->uuid()); |
- } |
+ return [IOBluetoothSDPUUID uuidWithBytes:uuid_bytes length:16]; |
+} |
+ |
+} // namespace |
+ |
+namespace device { |
+ |
+BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device) |
+ : BluetoothDevice(), device_([device retain]) { |
} |
BluetoothDeviceMac::~BluetoothDeviceMac() { |
+ [device_ release]; |
} |
uint32 BluetoothDeviceMac::GetBluetoothClass() const { |
- return bluetooth_class_; |
+ return [device_ classOfDevice]; |
} |
std::string BluetoothDeviceMac::GetDeviceName() const { |
- return name_; |
+ return base::SysNSStringToUTF8([device_ name]); |
} |
std::string BluetoothDeviceMac::GetAddress() const { |
- return address_; |
+ return base::SysNSStringToUTF8([device_ addressString]); |
} |
bool BluetoothDeviceMac::IsPaired() const { |
- return paired_; |
+ return [device_ isPaired]; |
} |
bool BluetoothDeviceMac::IsConnected() const { |
- return connected_; |
+ return [device_ isConnected]; |
} |
bool BluetoothDeviceMac::IsConnectable() const { |
@@ -81,27 +94,36 @@ bool BluetoothDeviceMac::IsConnecting() const { |
return false; |
} |
+// TODO(youngki): BluetoothServiceRecord is deprecated; implement it without |
Mark Mentovai
2013/04/11 21:48:58
“it” seems to refer to BluetoothServiceRecord here
youngki
2013/04/11 22:14:29
Changed 'it' to 'this method'.
|
+// using BluetoothServiceRecord. |
BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const { |
- return service_uuids_; |
+ ServiceList service_uuids; |
+ for (IOBluetoothSDPServiceRecord* service in [device_ services]) { |
+ BluetoothServiceRecordMac service_record(service); |
+ service_uuids.push_back(service_record.uuid()); |
+ } |
+ return service_uuids; |
} |
+// NOTE(youngki): This method is deprecated; it will be removed soon. |
void BluetoothDeviceMac::GetServiceRecords( |
const ServiceRecordsCallback& callback, |
const ErrorCallback& error_callback) { |
- callback.Run(service_record_list_); |
+ ServiceRecordList service_record_list; |
+ for (IOBluetoothSDPServiceRecord* service in [device_ services]) { |
+ BluetoothServiceRecord* service_record = |
+ new BluetoothServiceRecordMac(service); |
+ service_record_list.push_back(service_record); |
+ } |
+ |
+ callback.Run(service_record_list); |
} |
+// NOTE(youngki): This method is deprecated; it will be removed soon. |
void BluetoothDeviceMac::ProvidesServiceWithName( |
const std::string& name, |
const ProvidesServiceCallback& callback) { |
- for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
- iter != service_record_list_.end(); |
- ++iter) { |
- if ((*iter)->name() == name) { |
- callback.Run(true); |
- return; |
- } |
- } |
+ NOTIMPLEMENTED(); |
callback.Run(false); |
} |
@@ -160,18 +182,14 @@ void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { |
void BluetoothDeviceMac::ConnectToService( |
const std::string& service_uuid, |
const SocketCallback& callback) { |
- for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
- iter != service_record_list_.end(); |
- ++iter) { |
- if ((*iter)->uuid() == service_uuid) { |
- // If multiple service records are found, use the first one that works. |
- scoped_refptr<BluetoothSocket> socket( |
- BluetoothSocketMac::CreateBluetoothSocket(**iter)); |
- if (socket.get() != NULL) { |
- callback.Run(socket); |
- return; |
- } |
- } |
+ IOBluetoothSDPServiceRecord* record = |
+ [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)]; |
+ if (record != nil) { |
+ BluetoothServiceRecordMac service_record(record); |
+ scoped_refptr<BluetoothSocket> socket( |
+ BluetoothSocketMac::CreateBluetoothSocket(service_record)); |
+ if (socket.get() != NULL) |
+ callback.Run(socket); |
} |
} |
@@ -188,25 +206,4 @@ void BluetoothDeviceMac::ClearOutOfBandPairingData( |
NOTIMPLEMENTED(); |
} |
-// static |
-uint32 BluetoothDeviceMac::ComputeDeviceFingerprint( |
- const IOBluetoothDevice* device) { |
- std::string device_string = base::StringPrintf("%s|%s|%u|%d|%d", |
- base::SysNSStringToUTF8([device name]).c_str(), |
- base::SysNSStringToUTF8([device addressString]).c_str(), |
- [device classOfDevice], |
- [device isConnected], |
- [device isPaired]); |
- |
- for (IOBluetoothSDPServiceRecord* record in [device services]) { |
- base::StringAppendF( |
- &device_string, |
- "|%s|%lu", |
- base::SysNSStringToUTF8([record getServiceName]).c_str(), |
- static_cast<unsigned long>([[record attributes] count])); |
- } |
- |
- return base::Hash(device_string); |
-} |
- |
} // namespace device |