Index: chrome/browser/mac/hardware_utility.mm |
diff --git a/chrome/browser/mac/hardware_utility.mm b/chrome/browser/mac/hardware_utility.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..57b3bc3415b96c28168003c47298166cc4b6e099 |
--- /dev/null |
+++ b/chrome/browser/mac/hardware_utility.mm |
@@ -0,0 +1,79 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/mac/hardware_utility.h" |
+ |
+#import <Foundation/Foundation.h> |
+#include <IOKit/IOKitLib.h> |
+ |
+#include "base/mac/foundation_util.h" |
+#include "base/mac/scoped_ioobject.h" |
+ |
+namespace hardware_utility { |
+ |
+// Returns the Bluetooth driver's Link Management Protocol version. |
+// Returns -1 if no Bluetooth driver is present. |
Mark Mentovai
2014/07/09 17:53:00
Use constants for these.
erikchen
2014/07/09 20:14:42
Done.
|
+// Returns -2 on an unexpected error. |
+int GetBluetoothLMPVersion() { |
Mark Mentovai
2014/07/09 17:53:00
Put this in an unnamed namespace.
erikchen
2014/07/09 20:14:42
Done.
|
+ base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( |
+ IOServiceMatching("IOBluetoothHCIController")); |
+ if (!matching_dict) |
+ return -2; |
+ |
+ // IOServiceGetMatchingServices takes ownership of matching_dict. |
+ io_iterator_t iter; |
+ int kr = IOServiceGetMatchingServices( |
+ kIOMasterPortDefault, matching_dict.release(), &iter); |
+ if (kr != KERN_SUCCESS) |
+ return -1; |
+ base::mac::ScopedIOObject<io_iterator_t> scoped_iter(iter); |
+ |
+ int version = 0; |
+ bool success = false; |
+ base::mac::ScopedIOObject<io_service_t> device; |
+ while (device.reset(IOIteratorNext(scoped_iter.get())), device) { |
+ CFMutableDictionaryRef dict; |
+ kr = IORegistryEntryCreateCFProperties( |
+ device, &dict, kCFAllocatorDefault, kNilOptions); |
+ if (kr != KERN_SUCCESS) |
+ continue; |
+ base::ScopedCFTypeRef<CFMutableDictionaryRef> scoped_dict(dict); |
+ |
+ // Check the LMP version. |
+ // See the following link for the relationship between LMP version and |
+ // Bluetooth Core Specification version. |
+ // https://www.bluetooth.org/en-us/specification/assigned-numbers/link-manager |
+ // Bluetooth Low Energy is available in Bluetooth Core Specification |
+ // Version 4.0+. |
Mark Mentovai
2014/07/09 17:53:00
As I understand it, Bluetooth 4.0 allows devices t
erikchen
2014/07/09 20:14:42
Hm. Right you are.
I spent some time looking int
|
+ // http://en.wikipedia.org/wiki/Bluetooth_low_energy |
+ NSDictionary* objc_dict = base::mac::CFToNSCast(scoped_dict.get()); |
+ NSNumber* lmp_version = |
+ base::mac::ObjCCast<NSNumber>([objc_dict objectForKey:@"LMPVersion"]); |
+ if (!lmp_version) |
+ continue; |
+ |
+ // A bluetooth device with an LMP version has been found. Record the |
+ // highest version among all devices. |
+ success = true; |
+ version = std::max(version, [lmp_version intValue]); |
+ } |
+ |
+ if (!success) |
+ return -2; |
+ |
+ return version; |
+} |
+ |
+BluetoothAvailability GetBluetoothAvailability() { |
+ static int result = GetBluetoothLMPVersion(); |
+ if (result == -2) |
+ return BLUETOOTH_AVAILABILITY_ERROR; |
+ if (result == -1) |
+ return BLUETOOTH_NOT_AVAILABLE; |
+ if (result < 6) |
+ return BLUETOOTH_AVAILABLE_WITHOUT_LE; |
+ return BLUETOOTH_AVAILABLE_WITH_LE; |
+} |
+ |
+} // namespace hardware_utility |