Index: device/bluetooth/bluetooth_adapter_mac.mm |
diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm |
index 72554ed21b458129818ec0f12243a6be8a58cd7a..add5018bae8ca4c8045a28c2bec76e743c90debf 100644 |
--- a/device/bluetooth/bluetooth_adapter_mac.mm |
+++ b/device/bluetooth/bluetooth_adapter_mac.mm |
@@ -93,6 +93,9 @@ BluetoothAdapterMac::BluetoothAdapterMac() |
} |
BluetoothAdapterMac::~BluetoothAdapterMac() { |
+ // Need to make sure the devices are destroyed before CBCentralManage being |
scheib
2016/02/10 18:59:45
CBCentralManager is released
jlebel
2016/02/10 21:20:07
Done.
|
+ // released. |
+ devices_.clear(); |
} |
std::string BluetoothAdapterMac::GetAddress() const { |
@@ -222,18 +225,21 @@ bool BluetoothAdapterMac::IsLowEnergyAvailable() { |
return base::mac::IsOSYosemiteOrLater(); |
} |
+void BluetoothAdapterMac::RemovePairingDelegateInternal( |
+ BluetoothDevice::PairingDelegate* pairing_delegate) {} |
+ |
void BluetoothAdapterMac::SetCentralManagerForTesting( |
CBCentralManager* central_manager) { |
CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
- [central_manager performSelector:@selector(setDelegate:) |
- withObject:low_energy_central_manager_delegate_]; |
- low_energy_central_manager_.reset(central_manager); |
+ [central_manager setDelegate:low_energy_central_manager_delegate_]; |
+ low_energy_central_manager_.reset(central_manager, |
+ base::scoped_policy::RETAIN); |
low_energy_discovery_manager_->SetCentralManager( |
low_energy_central_manager_.get()); |
} |
-void BluetoothAdapterMac::RemovePairingDelegateInternal( |
- BluetoothDevice::PairingDelegate* pairing_delegate) { |
+CBCentralManager* BluetoothAdapterMac::GetCentralManagerForTesting() { |
+ return low_energy_central_manager_; |
} |
void BluetoothAdapterMac::AddDiscoverySession( |
@@ -537,4 +543,65 @@ void BluetoothAdapterMac::AddPairedDevices() { |
} |
} |
+void BluetoothAdapterMac::CreateGattConnection( |
+ BluetoothLowEnergyDeviceMac* device_mac) { |
+ [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ |
+ options:nil]; |
+} |
+ |
+void BluetoothAdapterMac::DisconnectGatt( |
+ BluetoothLowEnergyDeviceMac* device_mac) { |
+ [low_energy_central_manager_ |
+ cancelPeripheralConnection:device_mac->peripheral_]; |
+} |
+ |
+void BluetoothAdapterMac::DidConnectPeripheral(CBPeripheral* peripheral) { |
+ std::string device_address = |
+ BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
+ // Try to find device from |devices_| with key |device_address|, |
+ // if has no entry in the map, disconnect the peripheral. |
+ DevicesMap::const_iterator iter = devices_.find(device_address); |
+ if (iter == devices_.end()) { |
+ [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; |
+ return; |
+ } |
+ BluetoothLowEnergyDeviceMac* device_mac = |
+ static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
+ device_mac->GattConnected(); |
+} |
+ |
+void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral, |
+ NSError* error) { |
+ std::string device_address = |
+ BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
+ // Try to find device from |devices_| with key |device_address|, |
+ // if has no entry in the map, disconnect the peripheral. |
+ DevicesMap::const_iterator iter = devices_.find(device_address); |
+ if (iter == devices_.end()) { |
+ [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; |
+ return; |
+ } |
+ BluetoothLowEnergyDeviceMac* device_mac = |
+ static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
+ // TODO(jlebel): Need to convert the error. |
scheib
2016/02/10 18:59:45
Please make this a TODO(http://crbug.com/1111111)
jlebel
2016/02/10 21:20:07
Done.
|
+ device_mac->DidFailToConnectGatt(BluetoothClassicDeviceMac::ERROR_UNKNOWN); |
+} |
+ |
+void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral, |
+ NSError* error) { |
+ std::string device_address = |
scheib
2016/02/10 18:59:45
This code appears three times, refactor to a helpe
jlebel
2016/02/10 21:20:07
Done.
|
+ BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
+ // Try to find device from |devices_| with key |device_address|, |
+ // if has no entry in the map, disconnect the peripheral. |
+ DevicesMap::const_iterator iter = devices_.find(device_address); |
+ if (iter == devices_.end()) { |
+ [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; |
+ return; |
+ } |
+ BluetoothLowEnergyDeviceMac* device_mac = |
+ static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
+ // TODO(jlebel): Need to pass the error. |
scheib
2016/02/10 18:59:45
ditto
jlebel
2016/02/10 21:20:07
Done.
|
+ device_mac->DidDisconnectPeripheral(); |
+} |
+ |
} // namespace device |