1 // Copyright 2014 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_discovery_manager_mac.h"
7 #import <IOBluetooth/objc/IOBluetoothDevice.h>
8 #import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/mac/sdk_forward_declarations.h"
12 #include "base/logging.h"
16 class BluetoothDiscoveryManagerMacClassic;
20 // IOBluetoothDeviceInquiryDelegate implementation.
21 @interface BluetoothDeviceInquiryDelegate
22 : NSObject<IOBluetoothDeviceInquiryDelegate> {
24 device::BluetoothDiscoveryManagerMacClassic* manager_; // weak
27 - (id)initWithManager:(device::BluetoothDiscoveryManagerMacClassic*)manager;
33 // ementation of BluetoothDiscoveryManagerMac for Bluetooth classic device
34 // discovery, using the IOBluetooth framework.
35 class BluetoothDiscoveryManagerMacClassic
36 : public BluetoothDiscoveryManagerMac {
38 explicit BluetoothDiscoveryManagerMacClassic(Observer* observer)
39 : BluetoothDiscoveryManagerMac(observer),
40 should_do_discovery_(false),
41 inquiry_running_(false),
43 [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]),
44 inquiry_([[IOBluetoothDeviceInquiry alloc]
45 initWithDelegate:inquiry_delegate_]) {}
47 virtual ~BluetoothDiscoveryManagerMacClassic() {}
49 // BluetoothDiscoveryManagerMac override.
50 virtual bool IsDiscovering() const OVERRIDE { return should_do_discovery_; }
52 // BluetoothDiscoveryManagerMac override.
53 virtual bool StartDiscovery() OVERRIDE {
54 DVLOG(1) << "Bluetooth Classic: StartDiscovery";
55 DCHECK(!should_do_discovery_);
57 DVLOG(1) << "Discovery requested";
58 should_do_discovery_ = true;
60 if (inquiry_running_) {
61 DVLOG(1) << "Device inquiry already running";
65 DVLOG(1) << "Requesting to start device inquiry";
66 if ([inquiry_ start] != kIOReturnSuccess) {
67 DVLOG(1) << "Failed to start device inquiry";
69 // Set |should_do_discovery_| to false here. Since we're reporting an
70 // error, we're indicating that the adapter call StartDiscovery again
72 should_do_discovery_ = false;
76 DVLOG(1) << "Device inquiry start was successful";
80 // BluetoothDiscoveryManagerMac override.
81 virtual bool StopDiscovery() OVERRIDE {
82 DVLOG(1) << "Bluetooth Classic: StopDiscovery";
83 DCHECK(should_do_discovery_);
85 should_do_discovery_ = false;
87 if (!inquiry_running_) {
88 DVLOG(1) << "No device inquiry running; discovery stopped";
92 DVLOG(1) << "Requesting to stop device inquiry";
93 IOReturn status = [inquiry_ stop];
94 if (status == kIOReturnSuccess) {
95 DVLOG(1) << "Device inquiry stop was successful";
99 if (status == kIOReturnNotPermitted) {
100 DVLOG(1) << "Device inquiry was already stopped";
104 LOG(WARNING) << "Failed to stop device inquiry";
108 // Called by BluetoothDeviceInquiryDelegate.
109 void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry) {
110 DCHECK(!inquiry_running_);
112 DVLOG(1) << "Device inquiry started!";
114 // If discovery was requested to stop in the mean time, stop the inquiry.
115 if (!should_do_discovery_) {
116 DVLOG(1) << "Discovery stop was requested earlier. Stopping inquiry";
121 inquiry_running_ = true;
124 void DeviceFound(IOBluetoothDeviceInquiry* inquiry,
125 IOBluetoothDevice* device) {
127 observer_->DeviceFound(device);
130 void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry,
133 DCHECK_EQ(inquiry_, inquiry);
135 DVLOG(1) << "Device inquiry complete";
136 inquiry_running_ = false;
138 // If discovery is no longer desired, notify observers that discovery
139 // has stopped and return.
140 if (!should_do_discovery_) {
141 observer_->DiscoveryStopped(false /* unexpected */);
145 // If discovery has stopped due to an unexpected reason, notify the
146 // observers and return.
147 if (error != kIOReturnSuccess) {
148 DVLOG(1) << "Inquiry has stopped with an error: " << error;
149 should_do_discovery_ = false;
150 observer_->DiscoveryStopped(true /* unexpected */);
154 DVLOG(1) << "Restarting device inquiry";
156 if ([inquiry_ start] == kIOReturnSuccess) {
157 DVLOG(1) << "Device inquiry restart was successful";
161 DVLOG(1) << "Failed to restart discovery";
162 should_do_discovery_ = false;
164 observer_->DiscoveryStopped(true /* unexpected */);
168 // The requested discovery state.
169 bool should_do_discovery_;
171 // The current inquiry state.
172 bool inquiry_running_;
174 // Objective-C objects for running and tracking device inquiry.
175 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_;
176 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_;
178 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic);
181 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac(
182 Observer* observer) : observer_(observer) {
186 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() {
190 BluetoothDiscoveryManagerMac* BluetoothDiscoveryManagerMac::CreateClassic(
191 Observer* observer) {
192 return new BluetoothDiscoveryManagerMacClassic(observer);
195 } // namespace device
197 @implementation BluetoothDeviceInquiryDelegate
199 - (id)initWithManager:
200 (device::BluetoothDiscoveryManagerMacClassic*)manager {
201 if ((self = [super init]))
207 - (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry*)sender {
208 manager_->DeviceInquiryStarted(sender);
211 - (void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender
212 device:(IOBluetoothDevice*)device {
213 manager_->DeviceFound(sender, device);
216 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
217 error:(IOReturn)error
218 aborted:(BOOL)aborted {
219 manager_->DeviceInquiryComplete(sender, error, aborted);