Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac.mm

Issue 1538173003: Implementing GATT connection/disconnect on OS X. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sorting methods between bluetooth_adapter_mac.h and bluetooth_adapter_mac.mm Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_adapter_mac.h" 5 #include "device/bluetooth/bluetooth_adapter_mac.h"
6 6
7 #import <IOBluetooth/objc/IOBluetoothDevice.h> 7 #import <IOBluetooth/objc/IOBluetoothDevice.h>
8 #import <IOBluetooth/objc/IOBluetoothHostController.h> 8 #import <IOBluetooth/objc/IOBluetoothHostController.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 low_energy_central_manager_.reset([[aClass alloc] 86 low_energy_central_manager_.reset([[aClass alloc]
87 initWithDelegate:low_energy_central_manager_delegate_.get() 87 initWithDelegate:low_energy_central_manager_delegate_.get()
88 queue:dispatch_get_main_queue()]); 88 queue:dispatch_get_main_queue()]);
89 low_energy_discovery_manager_->SetCentralManager( 89 low_energy_discovery_manager_->SetCentralManager(
90 low_energy_central_manager_.get()); 90 low_energy_central_manager_.get());
91 } 91 }
92 DCHECK(classic_discovery_manager_.get()); 92 DCHECK(classic_discovery_manager_.get());
93 } 93 }
94 94
95 BluetoothAdapterMac::~BluetoothAdapterMac() { 95 BluetoothAdapterMac::~BluetoothAdapterMac() {
96 // Need to make sure the devices are destroyed before CBCentralManage being
scheib 2016/02/10 18:59:45 CBCentralManager is released
jlebel 2016/02/10 21:20:07 Done.
97 // released.
98 devices_.clear();
96 } 99 }
97 100
98 std::string BluetoothAdapterMac::GetAddress() const { 101 std::string BluetoothAdapterMac::GetAddress() const {
99 return address_; 102 return address_;
100 } 103 }
101 104
102 std::string BluetoothAdapterMac::GetName() const { 105 std::string BluetoothAdapterMac::GetName() const {
103 return name_; 106 return name_;
104 } 107 }
105 108
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 DVLOG(1) << "Adapter registered a new connection from device with address: " 218 DVLOG(1) << "Adapter registered a new connection from device with address: "
216 << BluetoothClassicDeviceMac::GetDeviceAddress(device); 219 << BluetoothClassicDeviceMac::GetDeviceAddress(device);
217 ClassicDeviceAdded(device); 220 ClassicDeviceAdded(device);
218 } 221 }
219 222
220 // static 223 // static
221 bool BluetoothAdapterMac::IsLowEnergyAvailable() { 224 bool BluetoothAdapterMac::IsLowEnergyAvailable() {
222 return base::mac::IsOSYosemiteOrLater(); 225 return base::mac::IsOSYosemiteOrLater();
223 } 226 }
224 227
228 void BluetoothAdapterMac::RemovePairingDelegateInternal(
229 BluetoothDevice::PairingDelegate* pairing_delegate) {}
230
225 void BluetoothAdapterMac::SetCentralManagerForTesting( 231 void BluetoothAdapterMac::SetCentralManagerForTesting(
226 CBCentralManager* central_manager) { 232 CBCentralManager* central_manager) {
227 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); 233 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
228 [central_manager performSelector:@selector(setDelegate:) 234 [central_manager setDelegate:low_energy_central_manager_delegate_];
229 withObject:low_energy_central_manager_delegate_]; 235 low_energy_central_manager_.reset(central_manager,
230 low_energy_central_manager_.reset(central_manager); 236 base::scoped_policy::RETAIN);
231 low_energy_discovery_manager_->SetCentralManager( 237 low_energy_discovery_manager_->SetCentralManager(
232 low_energy_central_manager_.get()); 238 low_energy_central_manager_.get());
233 } 239 }
234 240
235 void BluetoothAdapterMac::RemovePairingDelegateInternal( 241 CBCentralManager* BluetoothAdapterMac::GetCentralManagerForTesting() {
236 BluetoothDevice::PairingDelegate* pairing_delegate) { 242 return low_energy_central_manager_;
237 } 243 }
238 244
239 void BluetoothAdapterMac::AddDiscoverySession( 245 void BluetoothAdapterMac::AddDiscoverySession(
240 BluetoothDiscoveryFilter* discovery_filter, 246 BluetoothDiscoveryFilter* discovery_filter,
241 const base::Closure& callback, 247 const base::Closure& callback,
242 const DiscoverySessionErrorCallback& error_callback) { 248 const DiscoverySessionErrorCallback& error_callback) {
243 DVLOG(1) << __func__; 249 DVLOG(1) << __func__;
244 if (num_discovery_sessions_ > 0) { 250 if (num_discovery_sessions_ > 0) {
245 DCHECK(IsDiscovering()); 251 DCHECK(IsDiscovering());
246 num_discovery_sessions_++; 252 num_discovery_sessions_++;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 } 536 }
531 } 537 }
532 538
533 void BluetoothAdapterMac::AddPairedDevices() { 539 void BluetoothAdapterMac::AddPairedDevices() {
534 // Add any new paired devices. 540 // Add any new paired devices.
535 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { 541 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) {
536 ClassicDeviceAdded(device); 542 ClassicDeviceAdded(device);
537 } 543 }
538 } 544 }
539 545
546 void BluetoothAdapterMac::CreateGattConnection(
547 BluetoothLowEnergyDeviceMac* device_mac) {
548 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_
549 options:nil];
550 }
551
552 void BluetoothAdapterMac::DisconnectGatt(
553 BluetoothLowEnergyDeviceMac* device_mac) {
554 [low_energy_central_manager_
555 cancelPeripheralConnection:device_mac->peripheral_];
556 }
557
558 void BluetoothAdapterMac::DidConnectPeripheral(CBPeripheral* peripheral) {
559 std::string device_address =
560 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
561 // Try to find device from |devices_| with key |device_address|,
562 // if has no entry in the map, disconnect the peripheral.
563 DevicesMap::const_iterator iter = devices_.find(device_address);
564 if (iter == devices_.end()) {
565 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
566 return;
567 }
568 BluetoothLowEnergyDeviceMac* device_mac =
569 static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
570 device_mac->GattConnected();
571 }
572
573 void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral,
574 NSError* error) {
575 std::string device_address =
576 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
577 // Try to find device from |devices_| with key |device_address|,
578 // if has no entry in the map, disconnect the peripheral.
579 DevicesMap::const_iterator iter = devices_.find(device_address);
580 if (iter == devices_.end()) {
581 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
582 return;
583 }
584 BluetoothLowEnergyDeviceMac* device_mac =
585 static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
586 // TODO(jlebel): Need to convert the error.
scheib 2016/02/10 18:59:45 Please make this a TODO(http://crbug.com/1111111)
jlebel 2016/02/10 21:20:07 Done.
587 device_mac->DidFailToConnectGatt(BluetoothClassicDeviceMac::ERROR_UNKNOWN);
588 }
589
590 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral,
591 NSError* error) {
592 std::string device_address =
scheib 2016/02/10 18:59:45 This code appears three times, refactor to a helpe
jlebel 2016/02/10 21:20:07 Done.
593 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
594 // Try to find device from |devices_| with key |device_address|,
595 // if has no entry in the map, disconnect the peripheral.
596 DevicesMap::const_iterator iter = devices_.find(device_address);
597 if (iter == devices_.end()) {
598 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
599 return;
600 }
601 BluetoothLowEnergyDeviceMac* device_mac =
602 static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
603 // TODO(jlebel): Need to pass the error.
scheib 2016/02/10 18:59:45 ditto
jlebel 2016/02/10 21:20:07 Done.
604 device_mac->DidDisconnectPeripheral();
605 }
606
540 } // namespace device 607 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698