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

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: Rebased - work in progress / no tests Created 4 years, 3 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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 // Begin a low energy discovery session or update it if one is already 372 // Begin a low energy discovery session or update it if one is already
373 // running. 373 // running.
374 if (IsLowEnergyAvailable()) 374 if (IsLowEnergyAvailable())
375 low_energy_discovery_manager_->StartDiscovery( 375 low_energy_discovery_manager_->StartDiscovery(
376 BluetoothDevice::UUIDList()); 376 BluetoothDevice::UUIDList());
377 } 377 }
378 return true; 378 return true;
379 } 379 }
380 380
381 void BluetoothAdapterMac::Init() { 381 void BluetoothAdapterMac::Init() {
382 if (IsLowEnergyAvailable())
383 AddConnectedLowEnergyDevices();
382 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 384 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
383 PollAdapter(); 385 PollAdapter();
384 } 386 }
385 387
386 void BluetoothAdapterMac::InitForTest( 388 void BluetoothAdapterMac::InitForTest(
387 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { 389 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
388 ui_task_runner_ = ui_task_runner; 390 ui_task_runner_ = ui_task_runner;
389 } 391 }
390 392
391 void BluetoothAdapterMac::PollAdapter() { 393 void BluetoothAdapterMac::PollAdapter() {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 // is fixed. 450 // is fixed.
449 tracked_objects::ScopedTracker tracking_profile5( 451 tracked_objects::ScopedTracker tracking_profile5(
450 FROM_HERE_WITH_EXPLICIT_FUNCTION( 452 FROM_HERE_WITH_EXPLICIT_FUNCTION(
451 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices")); 453 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices"));
452 RemoveTimedOutDevices(); 454 RemoveTimedOutDevices();
453 455
454 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 456 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181
455 // is fixed. 457 // is fixed.
456 tracked_objects::ScopedTracker tracking_profile6( 458 tracked_objects::ScopedTracker tracking_profile6(
457 FROM_HERE_WITH_EXPLICIT_FUNCTION( 459 FROM_HERE_WITH_EXPLICIT_FUNCTION(
458 "461181 BluetoothAdapterMac::PollAdapter::AddPairedDevices")); 460 "461181 BluetoothAdapterMac::PollAdapter::AddPairedClassicDevices"));
459 AddPairedDevices(); 461 AddPairedClassicDevices();
460 462
461 ui_task_runner_->PostDelayedTask( 463 ui_task_runner_->PostDelayedTask(
462 FROM_HERE, 464 FROM_HERE,
463 base::Bind(&BluetoothAdapterMac::PollAdapter, 465 base::Bind(&BluetoothAdapterMac::PollAdapter,
464 weak_ptr_factory_.GetWeakPtr()), 466 weak_ptr_factory_.GetWeakPtr()),
465 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); 467 base::TimeDelta::FromMilliseconds(kPollIntervalMs));
466 } 468 }
467 469
468 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { 470 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) {
469 std::string device_address = 471 std::string device_address =
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 DeviceAdded(this, device_mac)); 559 DeviceAdded(this, device_mac));
558 } else { 560 } else {
559 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 561 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
560 DeviceChanged(this, device_mac)); 562 DeviceChanged(this, device_mac));
561 } 563 }
562 } 564 }
563 565
564 // TODO(krstnmnlsn): Implement. crbug.com/511025 566 // TODO(krstnmnlsn): Implement. crbug.com/511025
565 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} 567 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {}
566 568
567 void BluetoothAdapterMac::AddPairedDevices() { 569 void BluetoothAdapterMac::AddPairedClassicDevices() {
568 // Add any new paired devices.
569 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { 570 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) {
570 ClassicDeviceAdded(device); 571 IOBluetoothSDPServiceRecord* pnp_information =
jracle (use Gerrit) 2016/09/02 14:23:10 Still need to confirm this logic.
572 [device getServiceRecordForUUID:
573 [IOBluetoothSDPUUID
574 uuid16:kBluetoothSDPUUID16ServiceClassPnPInformation]];
575
576 // IOBluetoothDevice instance is a Bluetooth Classic device if it has a PNP
577 // information service SDP entry.
578 if (pnp_information) {
579 ClassicDeviceAdded(device);
580 }
571 } 581 }
572 } 582 }
573 583
584 void BluetoothAdapterMac::AddConnectedLowEnergyDevices() {
585 // Look for Device Information Service. BLE devices should implement it.
586 CBUUID* device_information_uuid = [CBUUID UUIDWithString:@"180A"];
587
588 #pragma clang diagnostic push
589 #pragma clang diagnostic ignored "-Wpartial-availability"
jracle (use Gerrit) 2016/09/02 14:23:10 Passing nil in following line causes -Wnonnull to
590 // This list of devices contain both devices that are connected by other apps
591 // or by system. In that case, user will need to connect peripheral using
592 // CBCentralManager's connectPeripheral:options: method.
593 NSArray<CBPeripheral*>* connected_peripherals = [low_energy_central_manager_
594 retrieveConnectedPeripheralsWithServices:@[ device_information_uuid ]];
595 #pragma clang diagnostic pop
596
597 for (CBPeripheral* peripheral in connected_peripherals) {
598 BluetoothLowEnergyDeviceMac* device_mac =
599 GetBluetoothLowEnergyDeviceMac(peripheral);
600
601 if (!device_mac) {
602 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral);
603 std::string device_address =
604 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
605 devices_.add(device_address,
606 std::unique_ptr<BluetoothDevice>(device_mac));
607 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
608 DeviceAdded(this, device_mac));
609 }
610 }
611 }
612
574 void BluetoothAdapterMac::CreateGattConnection( 613 void BluetoothAdapterMac::CreateGattConnection(
575 BluetoothLowEnergyDeviceMac* device_mac) { 614 BluetoothLowEnergyDeviceMac* device_mac) {
576 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ 615 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_
577 options:nil]; 616 options:nil];
578 } 617 }
579 618
580 void BluetoothAdapterMac::DisconnectGatt( 619 void BluetoothAdapterMac::DisconnectGatt(
581 BluetoothLowEnergyDeviceMac* device_mac) { 620 BluetoothLowEnergyDeviceMac* device_mac) {
582 [low_energy_central_manager_ 621 [low_energy_central_manager_
583 cancelPeripheralConnection:device_mac->peripheral_]; 622 cancelPeripheralConnection:device_mac->peripheral_];
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 std::string device_address = 674 std::string device_address =
636 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 675 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
637 DevicesMap::const_iterator iter = devices_.find(device_address); 676 DevicesMap::const_iterator iter = devices_.find(device_address);
638 if (iter == devices_.end()) { 677 if (iter == devices_.end()) {
639 return nil; 678 return nil;
640 } 679 }
641 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); 680 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
642 } 681 }
643 682
644 } // namespace device 683 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698