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_profile_mac.h"
7 #import <IOBluetooth/objc/IOBluetoothDevice.h>
8 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
9 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
14 #include "base/basictypes.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "device/bluetooth/bluetooth_device_mac.h"
19 #include "device/bluetooth/bluetooth_socket_mac.h"
23 // Converts |uuid| to a IOBluetoothSDPUUID instance.
25 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
26 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) {
27 DCHECK(uuid.size() == 36);
28 DCHECK(uuid[8] == '-');
29 DCHECK(uuid[13] == '-');
30 DCHECK(uuid[18] == '-');
31 DCHECK(uuid[23] == '-');
32 std::string numbers_only = uuid;
33 numbers_only.erase(23, 1);
34 numbers_only.erase(18, 1);
35 numbers_only.erase(13, 1);
36 numbers_only.erase(8, 1);
37 std::vector<uint8> uuid_bytes_vector;
38 base::HexStringToBytes(numbers_only, &uuid_bytes_vector);
39 DCHECK(uuid_bytes_vector.size() == 16);
41 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0]
42 length:uuid_bytes_vector.size()];
49 BluetoothProfileMac::BluetoothProfileMac(const std::string& uuid,
50 const std::string& name)
51 : BluetoothProfile(), uuid_(uuid), name_(name) {
54 BluetoothProfileMac::~BluetoothProfileMac() {
57 void BluetoothProfileMac::Unregister() {
61 void BluetoothProfileMac::SetConnectionCallback(
62 const ConnectionCallback& callback) {
63 connection_callback_ = callback;
66 bool BluetoothProfileMac::Connect(IOBluetoothDevice* device) {
67 if (connection_callback_.is_null())
70 IOBluetoothSDPServiceRecord* record =
71 [device getServiceRecordForUUID:GetIOBluetoothSDPUUID(uuid_)];
73 scoped_refptr<BluetoothSocket> socket(
74 BluetoothSocketMac::CreateBluetoothSocket(record));
75 if (socket.get() != NULL) {
76 BluetoothDeviceMac device_mac(device);
77 connection_callback_.Run(&device_mac, socket);