Index: device/bluetooth/bluetooth_low_energy_device_mac.mm |
diff --git a/device/bluetooth/bluetooth_low_energy_device_mac.mm b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..207c1babb14100c2dedab1b5b7de536e9c584fcf |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
@@ -0,0 +1,232 @@ |
+// Copyright 2015 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 "device/bluetooth/bluetooth_low_energy_device_mac.h" |
+ |
+#import <CoreFoundation/CoreFoundation.h> |
+ |
+#include "base/mac/scoped_cftyperef.h" |
+ |
+using namespace device; |
Avi (use Gerrit)
2015/01/29 05:25:02
This doesn't work, and if you tried to compile you
dvh
2015/01/30 22:45:45
I'm not sure to understand what you mean here.
Cou
Avi (use Gerrit)
2015/01/30 23:18:31
Do not use "using" in this way.
First, it's forbi
|
+ |
+namespace { |
+ |
+// Converts a CBUUID to a Cocoa string. |
+// |
+// The string representation can have the following formats: |
+// - 16 bit: xxxx |
+// - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
+// CBUUID supports only 16 bits and 128 bits formats. |
+// |
+// In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we |
+// need to provide this function. |
+NSString* stringWithCBUUID(CBUUID* uuid) { |
+ NSData* data = [uuid data]; |
+ |
+ NSUInteger bytesToConvert = [data length]; |
+ const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; |
+ NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; |
+ |
+ for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; |
+ currentByteIndex++) { |
+ switch (currentByteIndex) { |
+ case 3: |
+ case 5: |
+ case 7: |
+ case 9: |
+ [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; |
+ break; |
+ default: |
+ [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; |
+ } |
+ } |
+ return outputString; |
+} |
+ |
+// Converts a CBUUID to a BluetoothUUID. |
+device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
+ NSString* uuidString = nil; |
+ if ([uuid respondsToSelector:@selector(UUIDString)]) { |
+ uuidString = [uuid performSelector:@selector(UUIDString)]; |
+ } else { |
+ uuidString = stringWithCBUUID(uuid); |
+ } |
+ std::string uuid_c_string([uuidString UTF8String]); |
+ return BluetoothUUID(uuid_c_string); |
+} |
+ |
+} // namespace device |
Avi (use Gerrit)
2015/01/29 05:25:02
this does not end a namespace "device"; it ends an
dvh
2015/01/30 22:45:45
Done.
|
+ |
+BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
+ CBPeripheral* peripheral, |
+ NSDictionary* advertisementData, |
+ int rssi) { |
+ Update(peripheral, advertisementData, rssi); |
+} |
+ |
+BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
+ NSDictionary* advertisementData, |
+ int rssi) { |
+ peripheral_.reset([peripheral retain]); |
+ rssi_ = rssi; |
+ ClearServiceData(); |
+ NSNumber* nbConnectable = |
+ [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; |
+ connectable_ = [nbConnectable boolValue]; |
+ NSDictionary* serviceData = |
+ [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; |
+ for (CBUUID* uuid in serviceData) { |
+ NSData* data = [serviceData objectForKey:uuid]; |
+ BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); |
+ SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); |
+ } |
+} |
+ |
+std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { |
+ if ([peripheral_ respondsToSelector:@selector(identifier)]) { |
+ // When -[CBPeripheral identifier] is available. |
+ NSUUID* uuid = [peripheral_ performSelector:@selector(identifier)]; |
+ NSString* uuidString = [uuid UUIDString]; |
+ return std::string([uuidString UTF8String]); |
Avi (use Gerrit)
2015/01/29 05:25:02
SysNSStringToUTF8
dvh
2015/01/30 22:45:45
Done.
|
+ } |
+ |
+ base::ScopedCFTypeRef<CFStringRef> str( |
+ CFUUIDCreateString(NULL, [peripheral_ UUID])); |
+ base::ScopedCFTypeRef<CFDataRef> data(CFStringCreateExternalRepresentation( |
+ NULL, str, kCFStringEncodingASCII, 0)); |
+ return std::string((const char*)CFDataGetBytePtr(data), |
+ CFDataGetLength(data)); |
Avi (use Gerrit)
2015/01/29 05:25:02
Huh? SysCFStringRefToUTF8.
dvh
2015/01/30 22:45:45
Done.
|
+} |
+ |
+uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { |
+ return 0; |
+} |
+ |
+std::string BluetoothLowEnergyDeviceMac::GetAddress() const { |
+ return std::string(); |
+} |
+ |
+BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() |
+ const { |
+ return VENDOR_ID_UNKNOWN; |
+} |
+ |
+uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { |
+ return 0; |
+} |
+ |
+uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { |
+ return 0; |
+} |
+ |
+uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { |
+ return 0; |
+} |
+ |
+int BluetoothLowEnergyDeviceMac::GetRSSI() const { |
+ return rssi_; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::IsPaired() const { |
+ return false; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::IsConnected() const { |
+ return [peripheral_ isConnected]; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
+ return connectable_; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
+ return false; |
+} |
+ |
+BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { |
+ return std::vector<device::BluetoothUUID>(); |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { |
+ return false; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const { |
+ return false; |
+} |
+ |
+bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const { |
+ return false; |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::GetConnectionInfo( |
+ const ConnectionInfoCallback& callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::Connect( |
+ PairingDelegate* pairing_delegate, |
+ const base::Closure& callback, |
+ const ConnectErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::ConfirmPairing() { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::RejectPairing() { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::CancelPairing() { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::Disconnect( |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::ConnectToService( |
+ const BluetoothUUID& uuid, |
+ const ConnectToServiceCallback& callback, |
+ const ConnectToServiceErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( |
+ const device::BluetoothUUID& uuid, |
+ const ConnectToServiceCallback& callback, |
+ const ConnectToServiceErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+void BluetoothLowEnergyDeviceMac::CreateGattConnection( |
+ const GattConnectionCallback& callback, |
+ const ConnectErrorCallback& error_callback) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
+ NSData* data = [[peripheral_ name] dataUsingEncoding:NSUTF8StringEncoding |
+ allowLossyConversion:YES]; |
+ return std::string((const char*)[data bytes], [data length]); |
Avi (use Gerrit)
2015/01/29 05:25:02
SysNSStringToUTF8
dvh
2015/01/30 22:45:45
Done.
|
+} |