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 20694e4953b97d7ca5d4edc882a0a4b0aab06864..fefbada01948db6bd6680982b3ef65cb76ffbb42 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 { |
@@ -372,6 +377,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 |
+ // call twice, related to macOS bug. This second call should be ignored. See |
ortuno
2017/05/04 01:49:15
called twice because of a macOS bug.
jlebel
2017/05/04 16:12:00
Done.
|
+ // crbug.com/681414. |
+ VLOG(1) << *this << ": Already connected, ignoring event."; |
+ } |
+} |
+ |
void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { |
VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " |
<< discovery_pending_count_; |
@@ -435,6 +454,7 @@ BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptor( |
} |
void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
+ connected_ = false; |
VLOG(1) << *this << ": Disconnected from peripheral."; |
if (error) { |
VLOG(1) << *this |
@@ -468,8 +488,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 |