Chromium Code Reviews| 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..85cc801f9b4f79462d5d665c1b7a2351a1297ba4 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,18 @@ std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| return BluetoothDevice::CanonicalizeAddress(hash); |
| } |
| +void BluetoothLowEnergyDeviceMac::DidConnectPeripheral() { |
| + VLOG(1) << *this << ": GATT connected."; |
| + const bool should_discover_services = !connected_; |
| + connected_ = true; |
| + DidConnectGatt(); |
|
ortuno
2017/05/04 01:26:11
I wonder if we should even call DidConnectGatt().
jlebel
2017/05/04 01:46:48
Done.
|
| + if (should_discover_services) { |
| + DiscoverPrimaryServices(); |
|
ortuno
2017/05/04 01:26:11
q: Any reason why we do this after DidConnectGatt(
jlebel
2017/05/04 01:46:48
It is better to first notify that the GATT is conn
|
| + } else { |
| + VLOG(1) << *this << ": Already connected, no need to discover services."; |
| + } |
| +} |
| + |
| void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { |
| VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " |
| << discovery_pending_count_; |
| @@ -435,6 +452,7 @@ BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptor( |
| } |
| void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| + connected_ = false; |
| VLOG(1) << *this << ": Disconnected from peripheral."; |
| if (error) { |
| VLOG(1) << *this |
| @@ -468,8 +486,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 |