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

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

Issue 2242833002: Bluetooth: mac: add connected BLE devices at startup Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 13 matching lines...) Expand all
24 #include "base/strings/sys_string_conversions.h" 24 #include "base/strings/sys_string_conversions.h"
25 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
26 #include "base/time/time.h" 26 #include "base/time/time.h"
27 #include "device/bluetooth/bluetooth_classic_device_mac.h" 27 #include "device/bluetooth/bluetooth_classic_device_mac.h"
28 #include "device/bluetooth/bluetooth_common.h" 28 #include "device/bluetooth/bluetooth_common.h"
29 #include "device/bluetooth/bluetooth_discovery_session.h" 29 #include "device/bluetooth/bluetooth_discovery_session.h"
30 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" 30 #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
31 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" 31 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h"
32 #include "device/bluetooth/bluetooth_socket_mac.h" 32 #include "device/bluetooth/bluetooth_socket_mac.h"
33 33
34 @interface CBCentralManager (PartialAvailability)
ortuno 2016/08/15 23:58:09 This doesn't seem to be a pattern we follow throug
jracle (use Gerrit) 2016/08/16 20:14:17 Actually, I think I've got it from a landed CL, bu
jlebel 2016/08/31 12:03:28 I think you can just #import <CoreBluetooth/CoreBl
jracle (use Gerrit) 2016/08/31 17:21:22 Indeed, as you state in other comment, that's a pa
35
36 - (NSArray<CBPeripheral*>*)retrieveConnectedPeripheralsWithServices:
37 (NSArray<CBUUID*>*)serviceUUIDs;
38
39 @end
40
34 namespace { 41 namespace {
35 42
36 // The frequency with which to poll the adapter for updates. 43 // The frequency with which to poll the adapter for updates.
37 const int kPollIntervalMs = 500; 44 const int kPollIntervalMs = 500;
38 45
39 } // namespace 46 } // namespace
40 47
41 namespace device { 48 namespace device {
42 49
43 // static 50 // static
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 // Begin a low energy discovery session or update it if one is already 375 // Begin a low energy discovery session or update it if one is already
369 // running. 376 // running.
370 if (IsLowEnergyAvailable()) 377 if (IsLowEnergyAvailable())
371 low_energy_discovery_manager_->StartDiscovery( 378 low_energy_discovery_manager_->StartDiscovery(
372 BluetoothDevice::UUIDList()); 379 BluetoothDevice::UUIDList());
373 } 380 }
374 return true; 381 return true;
375 } 382 }
376 383
377 void BluetoothAdapterMac::Init() { 384 void BluetoothAdapterMac::Init() {
385 if (IsLowEnergyAvailable())
386 AddConnectedLowEnergyDevices();
378 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 387 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
379 PollAdapter(); 388 PollAdapter();
380 } 389 }
381 390
382 void BluetoothAdapterMac::InitForTest( 391 void BluetoothAdapterMac::InitForTest(
383 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { 392 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
384 ui_task_runner_ = ui_task_runner; 393 ui_task_runner_ = ui_task_runner;
385 } 394 }
386 395
387 void BluetoothAdapterMac::PollAdapter() { 396 void BluetoothAdapterMac::PollAdapter() {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 // is fixed. 453 // is fixed.
445 tracked_objects::ScopedTracker tracking_profile5( 454 tracked_objects::ScopedTracker tracking_profile5(
446 FROM_HERE_WITH_EXPLICIT_FUNCTION( 455 FROM_HERE_WITH_EXPLICIT_FUNCTION(
447 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices")); 456 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices"));
448 RemoveTimedOutDevices(); 457 RemoveTimedOutDevices();
449 458
450 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 459 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181
451 // is fixed. 460 // is fixed.
452 tracked_objects::ScopedTracker tracking_profile6( 461 tracked_objects::ScopedTracker tracking_profile6(
453 FROM_HERE_WITH_EXPLICIT_FUNCTION( 462 FROM_HERE_WITH_EXPLICIT_FUNCTION(
454 "461181 BluetoothAdapterMac::PollAdapter::AddPairedDevices")); 463 "461181 BluetoothAdapterMac::PollAdapter::AddPairedClassicDevices"));
455 AddPairedDevices(); 464 AddPairedClassicDevices();
456 465
457 ui_task_runner_->PostDelayedTask( 466 ui_task_runner_->PostDelayedTask(
458 FROM_HERE, 467 FROM_HERE,
459 base::Bind(&BluetoothAdapterMac::PollAdapter, 468 base::Bind(&BluetoothAdapterMac::PollAdapter,
460 weak_ptr_factory_.GetWeakPtr()), 469 weak_ptr_factory_.GetWeakPtr()),
461 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); 470 base::TimeDelta::FromMilliseconds(kPollIntervalMs));
462 } 471 }
463 472
464 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { 473 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) {
465 std::string device_address = 474 std::string device_address =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 VLOG(2) << "LowEnergyDeviceUpdated"; 525 VLOG(2) << "LowEnergyDeviceUpdated";
517 device_mac->Update(advertisement_data, rssi); 526 device_mac->Update(advertisement_data, rssi);
518 // TODO(scheib): Call DeviceChanged only if UUIDs change. crbug.com/547106 527 // TODO(scheib): Call DeviceChanged only if UUIDs change. crbug.com/547106
519 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 528 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
520 DeviceChanged(this, device_mac)); 529 DeviceChanged(this, device_mac));
521 } 530 }
522 531
523 // TODO(krstnmnlsn): Implement. crbug.com/511025 532 // TODO(krstnmnlsn): Implement. crbug.com/511025
524 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} 533 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {}
525 534
526 void BluetoothAdapterMac::AddPairedDevices() { 535 void BluetoothAdapterMac::AddPairedClassicDevices() {
527 // Add any new paired devices.
528 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { 536 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) {
529 ClassicDeviceAdded(device); 537 IOBluetoothSDPServiceRecord* pnp_information =
538 [device getServiceRecordForUUID:
539 [IOBluetoothSDPUUID
540 uuid16:kBluetoothSDPUUID16ServiceClassPnPInformation]];
541
542 // IOBluetoothDevice instance is a Bluetooth Classic device if it has a PNP
ortuno 2016/08/15 23:58:09 Could you add a link to some documentation that st
jracle (use Gerrit) 2016/08/16 20:14:17 I need to cross-check with Logitech guys who point
jracle (use Gerrit) 2016/08/17 21:21:15 While checking for official documentation, here is
543 // information service SDP entry.
544 if (pnp_information) {
545 ClassicDeviceAdded(device);
546 }
530 } 547 }
531 } 548 }
532 549
550 void BluetoothAdapterMac::AddConnectedLowEnergyDevices() {
551 // Look for Device Information Service. BLE devices should advertise it.
ortuno 2016/08/15 23:58:09 nit: Well if it's connected it's not really advert
jracle (use Gerrit) 2016/08/16 20:14:17 Indeed.. does 'implement' look better? ("Most BLE
552 CBUUID* device_information_uuid = [CBUUID UUIDWithString:@"180A"];
553
554 // This list of devices contain both devices that are connected by other apps
555 // or by system. In that case, user will need to connect peripheral using
556 // CBCentralManager's connectPeripheral:options: method.
557 NSArray<CBPeripheral*>* connected_peripherals = [low_energy_central_manager_
558 retrieveConnectedPeripheralsWithServices:@[ device_information_uuid ]];
ortuno 2016/08/15 23:58:09 Just pass nil. There is no requirement that a devi
jracle (use Gerrit) 2016/08/16 20:14:17 Agree with you. Documentation says passing nil wil
559
560 for (CBPeripheral* peripheral in connected_peripherals) {
561 BluetoothLowEnergyDeviceMac* device_mac =
562 GetBluetoothLowEnergyDeviceMac(peripheral);
563
564 if (!device_mac) {
565 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral);
566 std::string device_address =
567 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
568 devices_.add(device_address,
569 std::unique_ptr<BluetoothDevice>(device_mac));
570 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
ortuno 2016/08/15 23:58:09 We probably want to also start a service discovery
jracle (use Gerrit) 2016/08/16 20:14:17 I don't do it here, cause user will have to connec
571 DeviceAdded(this, device_mac));
572 }
573 }
574 }
575
533 void BluetoothAdapterMac::CreateGattConnection( 576 void BluetoothAdapterMac::CreateGattConnection(
534 BluetoothLowEnergyDeviceMac* device_mac) { 577 BluetoothLowEnergyDeviceMac* device_mac) {
535 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ 578 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_
536 options:nil]; 579 options:nil];
537 } 580 }
538 581
539 void BluetoothAdapterMac::DisconnectGatt( 582 void BluetoothAdapterMac::DisconnectGatt(
540 BluetoothLowEnergyDeviceMac* device_mac) { 583 BluetoothLowEnergyDeviceMac* device_mac) {
541 [low_energy_central_manager_ 584 [low_energy_central_manager_
542 cancelPeripheralConnection:device_mac->peripheral_]; 585 cancelPeripheralConnection:device_mac->peripheral_];
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 std::string device_address = 634 std::string device_address =
592 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 635 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
593 DevicesMap::const_iterator iter = devices_.find(device_address); 636 DevicesMap::const_iterator iter = devices_.find(device_address);
594 if (iter == devices_.end()) { 637 if (iter == devices_.end()) {
595 return nil; 638 return nil;
596 } 639 }
597 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); 640 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
598 } 641 }
599 642
600 } // namespace device 643 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698