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" |
12 | 13 |
13 using device::BluetoothDevice; | 14 using device::BluetoothDevice; |
14 using device::BluetoothLowEnergyDeviceMac; | 15 using device::BluetoothLowEnergyDeviceMac; |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
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. | 19 // Converts a CBUUID to a BluetoothUUID. |
51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | 20 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
52 NSString* uuidString = nil; | 21 // UUIDString only available OS X >= 10.8. |
53 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10. | 22 CHECK(base::mac::IsOSMountainLionOrLater()); |
54 if ([uuid respondsToSelector:@selector(UUIDString)]) { | 23 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); | 24 return device::BluetoothUUID(uuid_c_string); |
61 } | 25 } |
62 | 26 |
63 } // namespace | 27 } // namespace |
64 | 28 |
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 29 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
66 CBPeripheral* peripheral, | 30 CBPeripheral* peripheral, |
67 NSDictionary* advertisementData, | 31 NSDictionary* advertisementData, |
68 int rssi) { | 32 int rssi) { |
33 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus | |
34 // BluetoothLowEnergyDeviceMac should only be instantiated if on >= 10.10. | |
35 CHECK(base::mac::IsOSYosemiteOrLater()); | |
69 Update(peripheral, advertisementData, rssi); | 36 Update(peripheral, advertisementData, rssi); |
70 } | 37 } |
71 | 38 |
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 39 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
73 } | 40 } |
74 | 41 |
75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | 42 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
76 NSDictionary* advertisementData, | 43 NSDictionary* advertisementData, |
77 int rssi) { | 44 int rssi) { |
78 last_update_time_.reset([[NSDate date] retain]); | 45 last_update_time_.reset([[NSDate date] retain]); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 } | 195 } |
229 | 196 |
230 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { | 197 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { |
231 return last_update_time_.get(); | 198 return last_update_time_.get(); |
232 } | 199 } |
233 | 200 |
234 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | 201 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
235 return base::SysNSStringToUTF8([peripheral_ name]); | 202 return base::SysNSStringToUTF8([peripheral_ name]); |
236 } | 203 } |
237 | 204 |
238 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 205 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
scheib
2015/07/09 19:51:23
Add a "// static" comment line above this method.
krstnmnlsn
2015/07/10 17:17:46
Done.
| |
239 CBPeripheral* peripheral) { | 206 CBPeripheral* peripheral) { |
240 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9. | 207 // For stability we only use CoreBluetooth on OS X >= 10.10. |
241 if ([peripheral respondsToSelector:@selector(identifier)]) { | 208 CHECK(base::mac::IsOSYosemiteOrLater()); |
242 // When -[CBPeripheral identifier] is available. | 209 NSUUID* uuid = [peripheral identifier]; |
243 NSUUID* uuid = [peripheral identifier]; | 210 NSString* uuidString = [uuid UUIDString]; |
244 NSString* uuidString = [uuid UUIDString]; | 211 return base::SysNSStringToUTF8(uuidString); |
245 return base::SysNSStringToUTF8(uuidString); | |
246 } | |
247 | |
248 base::ScopedCFTypeRef<CFStringRef> str( | |
249 CFUUIDCreateString(NULL, [peripheral UUID])); | |
250 return SysCFStringRefToUTF8(str); | |
251 } | 212 } |
OLD | NEW |