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. 4 5 #include "device/bluetooth/bluetooth_profile_mac.h" 6 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> 8 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> 9 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h> 10 11 #include <string> 12 #include <vector> 13 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" 20 21 namespace { 22 23 // Converts |uuid| to a IOBluetoothSDPUUID instance. 24 // 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); 40 41 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0] 42 length:uuid_bytes_vector.size()]; 43 } 44 45 } // namespace 46 47 namespace device { 48 49 BluetoothProfileMac::BluetoothProfileMac(const std::string& uuid, 50 const std::string& name) 51 : BluetoothProfile(), uuid_(uuid), name_(name) { 52 } 53 54 BluetoothProfileMac::~BluetoothProfileMac() { 55 } 56 57 void BluetoothProfileMac::Unregister() { 58 delete this; 59 } 60 61 void BluetoothProfileMac::SetConnectionCallback( 62 const ConnectionCallback& callback) { 63 connection_callback_ = callback; 64 } 65 66 bool BluetoothProfileMac::Connect(IOBluetoothDevice* device) { 67 if (connection_callback_.is_null()) 68 return false; 69 70 IOBluetoothSDPServiceRecord* record = 71 [device getServiceRecordForUUID:GetIOBluetoothSDPUUID(uuid_)]; 72 if (record != nil) { 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); 78 return true; 79 } 80 } 81 return false; 82 } 83 84 } // namespace device 85