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() {
41 // Can only start if the bluetooth power is turned on.
42 if (!central_manager_ ||
43 [central_manager_ state] != CBCentralManagerStatePoweredOn) {
47 // Converts the services UUIDs to a CoreBluetooth data structure.
48 NSMutableArray* services = nil;
49 if (!services_uuids_.empty()) {
50 services = [NSMutableArray array];
51 for (auto& service_uuid : services_uuids_) {
52 NSString* uuidString =
53 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
54 Class aClass = NSClassFromString(@"CBUUID");
55 CBUUID* uuid = [aClass UUIDWithString:uuidString];
56 [services addObject:uuid];
60 [central_manager_ scanForPeripheralsWithServices:services options:nil];
64 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
65 if (discovering_ && !pending_) {
66 [central_manager_ stopScan];
71 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager(
72 CBCentralManager* central_manager) {
73 central_manager_ = central_manager;
76 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral(
77 CBPeripheral* peripheral,
78 NSDictionary* advertisementData,
80 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi);
83 BluetoothLowEnergyDiscoveryManagerMac*
84 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) {
85 return new BluetoothLowEnergyDiscoveryManagerMac(observer);
88 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
90 : observer_(observer) {
91 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable());