Index: device/bluetooth/bluetooth_low_energy_device_mac.mm |
diff --git a/device/bluetooth/bluetooth_low_energy_device_mac.mm b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
index 8e624e2d0386b2d52179e57af90c820fa3284558..b8afdfdaf7db96a49a18e6a0305b09780214b20e 100644 |
--- a/device/bluetooth/bluetooth_low_energy_device_mac.mm |
+++ b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
@@ -28,6 +28,7 @@ BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
CBPeripheral* peripheral) |
: BluetoothDeviceMac(adapter), |
peripheral_(peripheral, base::scoped_policy::RETAIN), |
+ connected_(false), |
discovery_pending_count_(0) { |
DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
DCHECK(peripheral_.get()); |
@@ -96,7 +97,11 @@ bool BluetoothLowEnergyDeviceMac::IsConnected() const { |
} |
bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { |
- return ([peripheral_ state] == CBPeripheralStateConnected); |
+ // |connected_| can be false while |[peripheral_ state]| is |
+ // |CBPeripheralStateConnected|. This happens |
+ // BluetoothAdapterMac::DidConnectPeripheral() is called and |
+ // BluetoothLowEnergyDeviceMac::DidConnectGatt() has not been called yet. |
+ return connected_; |
} |
bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
@@ -388,6 +393,20 @@ std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
return BluetoothDevice::CanonicalizeAddress(hash); |
} |
+void BluetoothLowEnergyDeviceMac::DidConnectPeripheral() { |
+ VLOG(1) << *this << ": GATT connected."; |
+ if (!connected_) { |
+ connected_ = true; |
+ DidConnectGatt(); |
+ DiscoverPrimaryServices(); |
+ } else { |
+ // -[<CBCentralManagerDelegate> centralManager:didConnectPeripheral:] can be |
+ // called twice because of a macOS bug. This second call should be ignored. |
+ // See crbug.com/681414. |
+ VLOG(1) << *this << ": Already connected, ignoring event."; |
+ } |
+} |
+ |
void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { |
VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " |
<< discovery_pending_count_; |
@@ -452,6 +471,7 @@ BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptor( |
} |
void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
+ connected_ = false; |
VLOG(1) << *this << ": Disconnected from peripheral."; |
if (error) { |
VLOG(1) << *this |
@@ -485,8 +505,10 @@ std::ostream& operator<<(std::ostream& out, |
// TODO(crbug.com/703878): Should use |
// BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
base::Optional<std::string> name = device.GetName(); |
- const char* name_cstr = name ? name->c_str() : ""; |
+ const char* is_gatt_connected = |
+ device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
- << &device << ", \"" << name_cstr << "\">"; |
+ << &device << ", " << is_gatt_connected << ", \"" |
+ << name.value_or("Unnamed device") << "\">"; |
} |
} // namespace device |