OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
6 | 6 |
7 #import <CoreFoundation/CoreFoundation.h> | 7 #import <CoreFoundation/CoreFoundation.h> |
8 | 8 |
| 9 #include "base/mac/mac_util.h" |
9 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
10 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/sdk_forward_declarations.h" |
11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_mac.h" |
12 | 14 |
13 using device::BluetoothDevice; | 15 using device::BluetoothDevice; |
14 using device::BluetoothLowEnergyDeviceMac; | 16 using device::BluetoothLowEnergyDeviceMac; |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 // Converts a CBUUID to a Cocoa string. | |
19 // | |
20 // The string representation can have the following formats: | |
21 // - 16 bit: xxxx | |
22 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
23 // CBUUID supports only 16 bits and 128 bits formats. | |
24 // | |
25 // In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we | |
26 // need to provide this function. | |
27 NSString* stringWithCBUUID(CBUUID* uuid) { | |
28 NSData* data = [uuid data]; | |
29 | |
30 NSUInteger bytesToConvert = [data length]; | |
31 const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; | |
32 NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; | |
33 | |
34 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; | |
35 currentByteIndex++) { | |
36 switch (currentByteIndex) { | |
37 case 3: | |
38 case 5: | |
39 case 7: | |
40 case 9: | |
41 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; | |
42 break; | |
43 default: | |
44 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; | |
45 } | |
46 } | |
47 return outputString; | |
48 } | |
49 | |
50 // Converts a CBUUID to a BluetoothUUID. | 20 // Converts a CBUUID to a BluetoothUUID. |
51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | 21 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
52 NSString* uuidString = nil; | 22 // UUIDString only available OS X >= 10.8. |
53 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10. | 23 DCHECK(base::mac::IsOSMountainLionOrLater()); |
54 if ([uuid respondsToSelector:@selector(UUIDString)]) { | 24 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]); |
55 uuidString = [uuid UUIDString]; | |
56 } else { | |
57 uuidString = stringWithCBUUID(uuid); | |
58 } | |
59 std::string uuid_c_string = base::SysNSStringToUTF8(uuidString); | |
60 return device::BluetoothUUID(uuid_c_string); | 25 return device::BluetoothUUID(uuid_c_string); |
61 } | 26 } |
62 | 27 |
63 } // namespace | 28 } // namespace |
64 | 29 |
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 30 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
66 CBPeripheral* peripheral, | 31 CBPeripheral* peripheral, |
67 NSDictionary* advertisementData, | 32 NSDictionary* advertisementData, |
68 int rssi) { | 33 int rssi) { |
| 34 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
69 Update(peripheral, advertisementData, rssi); | 35 Update(peripheral, advertisementData, rssi); |
70 } | 36 } |
71 | 37 |
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 38 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
73 } | 39 } |
74 | 40 |
75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | 41 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
76 NSDictionary* advertisementData, | 42 NSDictionary* advertisementData, |
77 int rssi) { | 43 int rssi) { |
78 last_update_time_.reset([[NSDate date] retain]); | 44 last_update_time_.reset([[NSDate date] retain]); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 } | 194 } |
229 | 195 |
230 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { | 196 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { |
231 return last_update_time_.get(); | 197 return last_update_time_.get(); |
232 } | 198 } |
233 | 199 |
234 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | 200 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
235 return base::SysNSStringToUTF8([peripheral_ name]); | 201 return base::SysNSStringToUTF8([peripheral_ name]); |
236 } | 202 } |
237 | 203 |
| 204 // static |
238 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 205 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
239 CBPeripheral* peripheral) { | 206 CBPeripheral* peripheral) { |
240 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9. | 207 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
241 if ([peripheral respondsToSelector:@selector(identifier)]) { | 208 NSUUID* uuid = [peripheral identifier]; |
242 // When -[CBPeripheral identifier] is available. | 209 NSString* uuidString = [uuid UUIDString]; |
243 NSUUID* uuid = [peripheral identifier]; | 210 return base::SysNSStringToUTF8(uuidString); |
244 NSString* uuidString = [uuid UUIDString]; | |
245 return base::SysNSStringToUTF8(uuidString); | |
246 } | |
247 | |
248 base::ScopedCFTypeRef<CFStringRef> str( | |
249 CFUUIDCreateString(NULL, [peripheral UUID])); | |
250 return SysCFStringRefToUTF8(str); | |
251 } | 211 } |
OLD | NEW |