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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
7 #import <CoreFoundation/CoreFoundation.h>
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/sdk_forward_declarations.h"
11 #include "base/strings/sys_string_conversions.h"
13 using device::BluetoothDevice;
14 using device::BluetoothLowEnergyDeviceMac;
18 // Converts a CBUUID to a Cocoa string.
20 // The string representation can have the following formats:
22 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
23 // CBUUID supports only 16 bits and 128 bits formats.
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];
30 NSUInteger bytesToConvert = [data length];
31 const unsigned char* uuidBytes = (const unsigned char*)[data bytes];
32 NSMutableString* outputString = [NSMutableString stringWithCapacity:16];
34 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert;
36 switch (currentByteIndex) {
41 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]];
44 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]];
50 // Converts a CBUUID to a BluetoothUUID.
51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) {
52 NSString* uuidString = nil;
53 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10.
54 if ([uuid respondsToSelector:@selector(UUIDString)]) {
55 uuidString = [uuid UUIDString];
57 uuidString = stringWithCBUUID(uuid);
59 std::string uuid_c_string = base::SysNSStringToUTF8(uuidString);
60 return device::BluetoothUUID(uuid_c_string);
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac(
66 CBPeripheral* peripheral,
67 NSDictionary* advertisementData,
69 Update(peripheral, advertisementData, rssi);
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {
75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral,
76 NSDictionary* advertisementData,
78 peripheral_.reset([peripheral retain]);
81 NSNumber* nbConnectable =
82 [advertisementData objectForKey:CBAdvertisementDataIsConnectable];
83 connectable_ = [nbConnectable boolValue];
84 NSDictionary* serviceData =
85 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey];
86 for (CBUUID* uuid in serviceData) {
87 NSData* data = [serviceData objectForKey:uuid];
88 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid);
89 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]);
93 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const {
94 return GetPeripheralIdentifier(peripheral_);
97 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const {
101 std::string BluetoothLowEnergyDeviceMac::GetAddress() const {
102 return std::string();
105 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource()
107 return VENDOR_ID_UNKNOWN;
110 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const {
114 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const {
118 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const {
122 int BluetoothLowEnergyDeviceMac::GetRSSI() const {
126 bool BluetoothLowEnergyDeviceMac::IsPaired() const {
130 bool BluetoothLowEnergyDeviceMac::IsConnected() const {
131 return [peripheral_ isConnected];
134 bool BluetoothLowEnergyDeviceMac::IsConnectable() const {
138 bool BluetoothLowEnergyDeviceMac::IsConnecting() const {
142 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const {
143 return std::vector<device::BluetoothUUID>();
146 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
150 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
154 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
158 void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
159 const ConnectionInfoCallback& callback) {
163 void BluetoothLowEnergyDeviceMac::Connect(
164 PairingDelegate* pairing_delegate,
165 const base::Closure& callback,
166 const ConnectErrorCallback& error_callback) {
170 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
174 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) {
178 void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
182 void BluetoothLowEnergyDeviceMac::RejectPairing() {
186 void BluetoothLowEnergyDeviceMac::CancelPairing() {
190 void BluetoothLowEnergyDeviceMac::Disconnect(
191 const base::Closure& callback,
192 const ErrorCallback& error_callback) {
196 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) {
200 void BluetoothLowEnergyDeviceMac::ConnectToService(
201 const BluetoothUUID& uuid,
202 const ConnectToServiceCallback& callback,
203 const ConnectToServiceErrorCallback& error_callback) {
207 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
208 const device::BluetoothUUID& uuid,
209 const ConnectToServiceCallback& callback,
210 const ConnectToServiceErrorCallback& error_callback) {
214 void BluetoothLowEnergyDeviceMac::CreateGattConnection(
215 const GattConnectionCallback& callback,
216 const ConnectErrorCallback& error_callback) {
220 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const {
221 return base::SysNSStringToUTF8([peripheral_ name]);
224 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(
225 CBPeripheral* peripheral) {
226 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9.
227 if ([peripheral respondsToSelector:@selector(identifier)]) {
228 // When -[CBPeripheral identifier] is available.
229 NSUUID* uuid = [peripheral identifier];
230 NSString* uuidString = [uuid UUIDString];
231 return base::SysNSStringToUTF8(uuidString);
234 base::ScopedCFTypeRef<CFStringRef> str(
235 CFUUIDCreateString(NULL, [peripheral UUID]));
236 return SysCFStringRefToUTF8(str);