1 // Copyright 2013 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_device_mac.h"
7 #include <IOBluetooth/Bluetooth.h>
8 #import <IOBluetooth/objc/IOBluetoothDevice.h>
9 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
10 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
14 #include "base/basictypes.h"
15 #include "base/hash.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
20 #include "device/bluetooth/bluetooth_profile_mac.h"
21 #include "device/bluetooth/bluetooth_service_record_mac.h"
22 #include "device/bluetooth/bluetooth_socket_mac.h"
24 // Replicate specific 10.7 SDK declarations for building with prior SDKs.
25 #if !defined(MAC_OS_X_VERSION_10_7) || \
26 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
28 @interface IOBluetoothDevice (LionSDKDeclarations)
29 - (NSString*)addressString;
31 - (unsigned int)classOfDevice;
35 #endif // MAC_OS_X_VERSION_10_7
39 // Converts |uuid| to a IOBluetoothSDPUUID instance.
41 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
42 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) {
43 DCHECK(uuid.size() == 36);
44 DCHECK(uuid[8] == '-');
45 DCHECK(uuid[13] == '-');
46 DCHECK(uuid[18] == '-');
47 DCHECK(uuid[23] == '-');
48 std::string numbers_only = uuid;
49 numbers_only.erase(23, 1);
50 numbers_only.erase(18, 1);
51 numbers_only.erase(13, 1);
52 numbers_only.erase(8, 1);
53 std::vector<uint8> uuid_bytes_vector;
54 base::HexStringToBytes(numbers_only, &uuid_bytes_vector);
55 DCHECK(uuid_bytes_vector.size() == 16);
57 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0]
58 length:uuid_bytes_vector.size()];
65 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device)
66 : BluetoothDevice(), device_([device retain]) {
69 BluetoothDeviceMac::~BluetoothDeviceMac() {
73 uint32 BluetoothDeviceMac::GetBluetoothClass() const {
74 return [device_ classOfDevice];
77 std::string BluetoothDeviceMac::GetDeviceName() const {
78 return base::SysNSStringToUTF8([device_ name]);
81 std::string BluetoothDeviceMac::GetAddress() const {
82 return base::SysNSStringToUTF8([device_ addressString]);
85 BluetoothDevice::VendorIDSource
86 BluetoothDeviceMac::GetVendorIDSource() const {
87 return VENDOR_ID_UNKNOWN;
90 uint16 BluetoothDeviceMac::GetVendorID() const {
94 uint16 BluetoothDeviceMac::GetProductID() const {
98 uint16 BluetoothDeviceMac::GetDeviceID() const {
102 bool BluetoothDeviceMac::IsPaired() const {
103 return [device_ isPaired];
106 bool BluetoothDeviceMac::IsConnected() const {
107 return [device_ isConnected];
110 bool BluetoothDeviceMac::IsConnectable() const {
114 bool BluetoothDeviceMac::IsConnecting() const {
118 // TODO(keybuk): BluetoothServiceRecord is deprecated; implement this method
119 // without using BluetoothServiceRecord.
120 BluetoothDevice::UUIDList BluetoothDeviceMac::GetUUIDs() const {
122 for (IOBluetoothSDPServiceRecord* service in [device_ services]) {
123 BluetoothServiceRecordMac service_record(service);
124 uuids.push_back(service_record.uuid());
129 bool BluetoothDeviceMac::ExpectingPinCode() const {
134 bool BluetoothDeviceMac::ExpectingPasskey() const {
139 bool BluetoothDeviceMac::ExpectingConfirmation() const {
144 void BluetoothDeviceMac::Connect(
145 PairingDelegate* pairing_delegate,
146 const base::Closure& callback,
147 const ConnectErrorCallback& error_callback) {
151 void BluetoothDeviceMac::SetPinCode(const std::string& pincode) {
155 void BluetoothDeviceMac::SetPasskey(uint32 passkey) {
159 void BluetoothDeviceMac::ConfirmPairing() {
163 void BluetoothDeviceMac::RejectPairing() {
167 void BluetoothDeviceMac::CancelPairing() {
171 void BluetoothDeviceMac::Disconnect(
172 const base::Closure& callback,
173 const ErrorCallback& error_callback) {
177 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) {
181 void BluetoothDeviceMac::ConnectToService(
182 const std::string& service_uuid,
183 const SocketCallback& callback) {
184 IOBluetoothSDPServiceRecord* record =
185 [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)];
187 BluetoothServiceRecordMac service_record(record);
188 scoped_refptr<BluetoothSocket> socket(
189 BluetoothSocketMac::CreateBluetoothSocket(service_record));
190 if (socket.get() != NULL)
191 callback.Run(socket);
195 void BluetoothDeviceMac::ConnectToProfile(
196 device::BluetoothProfile* profile,
197 const base::Closure& callback,
198 const ErrorCallback& error_callback) {
199 if (static_cast<BluetoothProfileMac*>(profile)->Connect(device_))
202 error_callback.Run();
205 void BluetoothDeviceMac::SetOutOfBandPairingData(
206 const BluetoothOutOfBandPairingData& data,
207 const base::Closure& callback,
208 const ErrorCallback& error_callback) {
212 void BluetoothDeviceMac::ClearOutOfBandPairingData(
213 const base::Closure& callback,
214 const ErrorCallback& error_callback) {
218 } // namespace device