1 // Copyright 2015 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_low_energy_discovery_manager_mac.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/sdk_forward_declarations.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "device/bluetooth/bluetooth_adapter_mac.h"
12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
16 BluetoothLowEnergyDiscoveryManagerMac::
17 ~BluetoothLowEnergyDiscoveryManagerMac() {
20 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const {
24 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery(
25 BluetoothDevice::UUIDList services_uuids) {
28 services_uuids_ = services_uuids;
32 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() {
34 VLOG(1) << "TryStartDiscovery !discovering_";
39 VLOG(1) << "TryStartDiscovery !pending_";
43 if (!central_manager_) {
44 VLOG(1) << "TryStartDiscovery !central_manager_";
48 if ([central_manager_ state] != CBCentralManagerStatePoweredOn) {
49 VLOG(1) << "TryStartDiscovery != CBCentralManagerStatePoweredOn";
53 // Converts the services UUIDs to a CoreBluetooth data structure.
54 NSMutableArray* services = nil;
55 if (!services_uuids_.empty()) {
56 services = [NSMutableArray array];
57 for (auto& service_uuid : services_uuids_) {
58 NSString* uuidString =
59 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
60 Class aClass = NSClassFromString(@"CBUUID");
61 CBUUID* uuid = [aClass UUIDWithString:uuidString];
62 [services addObject:uuid];
66 VLOG(1) << "TryStartDiscovery scanForPeripheralsWithServices";
67 [central_manager_ scanForPeripheralsWithServices:services options:nil];
71 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
72 VLOG(1) << "StopDiscovery";
73 if (discovering_ && !pending_) {
74 [central_manager_ stopScan];
79 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager(
80 CBCentralManager* central_manager) {
81 central_manager_ = central_manager;
84 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral(
85 CBPeripheral* peripheral,
86 NSDictionary* advertisementData,
88 VLOG(1) << "DiscoveredPeripheral";
89 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi);
92 BluetoothLowEnergyDiscoveryManagerMac*
93 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) {
94 return new BluetoothLowEnergyDiscoveryManagerMac(observer);
97 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
99 : observer_(observer) {
100 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
101 discovering_ = false;
104 } // namespace device