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 int16 BluetoothLowEnergyDeviceMac::GetInquiryRSSI() const {
147 return kUnknownPower;
150 int16 BluetoothLowEnergyDeviceMac::GetInquiryTxPower() const {
152 return kUnknownPower;
155 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
159 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
163 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
167 void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
168 const ConnectionInfoCallback& callback) {
172 void BluetoothLowEnergyDeviceMac::Connect(
173 PairingDelegate* pairing_delegate,
174 const base::Closure& callback,
175 const ConnectErrorCallback& error_callback) {
179 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
183 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) {
187 void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
191 void BluetoothLowEnergyDeviceMac::RejectPairing() {
195 void BluetoothLowEnergyDeviceMac::CancelPairing() {
199 void BluetoothLowEnergyDeviceMac::Disconnect(
200 const base::Closure& callback,
201 const ErrorCallback& error_callback) {
205 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) {
209 void BluetoothLowEnergyDeviceMac::ConnectToService(
210 const BluetoothUUID& uuid,
211 const ConnectToServiceCallback& callback,
212 const ConnectToServiceErrorCallback& error_callback) {
216 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
217 const device::BluetoothUUID& uuid,
218 const ConnectToServiceCallback& callback,
219 const ConnectToServiceErrorCallback& error_callback) {
223 void BluetoothLowEnergyDeviceMac::CreateGattConnection(
224 const GattConnectionCallback& callback,
225 const ConnectErrorCallback& error_callback) {
229 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const {
230 return base::SysNSStringToUTF8([peripheral_ name]);
233 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(
234 CBPeripheral* peripheral) {
235 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9.
236 if ([peripheral respondsToSelector:@selector(identifier)]) {
237 // When -[CBPeripheral identifier] is available.
238 NSUUID* uuid = [peripheral identifier];
239 NSString* uuidString = [uuid UUIDString];
240 return base::SysNSStringToUTF8(uuidString);
243 base::ScopedCFTypeRef<CFStringRef> str(
244 CFUUIDCreateString(NULL, [peripheral UUID]));
245 return SysCFStringRefToUTF8(str);