Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_mac.mm |
| diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm |
| index 61d169e2c21937557362c21eb9b3d2f77e15d2f5..e885e9c9428b7b0f3d183d2bb2481d4476efe00e 100644 |
| --- a/device/bluetooth/bluetooth_adapter_mac.mm |
| +++ b/device/bluetooth/bluetooth_adapter_mac.mm |
| @@ -170,6 +170,40 @@ bool BluetoothAdapterMac::IsDiscovering() const { |
| return is_discovering; |
| } |
| +void BluetoothAdapterMac::RetrieveUnknownGattConnectedDevices() { |
| + // It is not possible to ask for all connected peripherals with |
| + // -[CBCentralManager retrieveConnectedPeripheralsWithServices:] by passing |
| + // nil. To try to get most of the peripherals, the search is done with |
| + // Generic Access service. |
| + CBUUID* genericAccessServiceUUID = [CBUUID UUIDWithString:@"1800"]; |
| + NSArray* connectedServices = @[ genericAccessServiceUUID ]; |
| +#pragma clang diagnostic push |
| +#pragma clang diagnostic ignored "-Wpartial-availability" |
| + // Can remove ignore -Wpartial-availability when 10.8 will not be supported |
| + // anymore. |
| + // TODO(crbug.com/653056) |
| + NSArray* peripherals = [low_energy_central_manager_ |
| + retrieveConnectedPeripheralsWithServices:connectedServices]; |
| +#pragma clang diagnostic pop |
| + for (CBPeripheral* peripheral in peripherals) { |
| + BluetoothLowEnergyDeviceMac* device_mac = |
| + GetBluetoothLowEnergyDeviceMac(peripheral); |
| + const bool is_new_device = device_mac == nullptr; |
| + |
| + if (!is_new_device) { |
| + DoesCollideWithKnownDevice(peripheral, device_mac); |
| + return; |
| + } |
| + |
| + device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| + std::string device_address = |
| + BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| + devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); |
| + FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| + DeviceAdded(this, device_mac)); |
| + } |
| +} |
| + |
| BluetoothAdapter::UUIDList BluetoothAdapterMac::GetUUIDs() const { |
| NOTIMPLEMENTED(); |
| return UUIDList(); |
| @@ -491,62 +525,48 @@ void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| VLOG(1) << "LowEnergyDeviceUpdated new device"; |
| // A new device has been found. |
| device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| - } else { |
| - // Check that there are no collisions. |
| - std::string stored_device_id = device_mac->GetIdentifier(); |
| - std::string updated_device_id = |
| - BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
| - if (stored_device_id != updated_device_id) { |
| - VLOG(1) |
| - << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
| - << std::endl |
| - << " " << stored_device_id << std::endl |
| - << " " << updated_device_id; |
| - // Collision, two identifiers map to the same hash address. With a 48 bit |
| - // hash the probability of this occuring with 10,000 devices |
| - // simultaneously present is 1e-6 (see |
| - // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| - // ignore the second device by returning. |
| - return; |
| - } |
| + } else if (DoesCollideWithKnownDevice(peripheral, device_mac)) { |
| + return; |
| } |
| DCHECK(device_mac); |
| - // Get Advertised UUIDs |
| - BluetoothDevice::UUIDList advertised_uuids; |
| - NSArray* service_uuids = |
| - [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; |
| - for (CBUUID* uuid in service_uuids) { |
| - advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| - } |
| - NSArray* overflow_service_uuids = [advertisement_data |
| - objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; |
| - for (CBUUID* uuid in overflow_service_uuids) { |
| - advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| - } |
| + if (advertisement_data) { |
|
ortuno
2016/10/14 03:03:03
I might be missing something but I don't think you
|
| + // Get Advertised UUIDs |
| + BluetoothDevice::UUIDList advertised_uuids; |
| + NSArray* service_uuids = |
| + [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; |
| + for (CBUUID* uuid in service_uuids) { |
| + advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| + } |
| + NSArray* overflow_service_uuids = [advertisement_data |
| + objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; |
| + for (CBUUID* uuid in overflow_service_uuids) { |
| + advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| + } |
| - // Get Service Data. |
| - BluetoothDevice::ServiceDataMap service_data_map; |
| - NSDictionary* service_data = |
| - [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; |
| - for (CBUUID* uuid in service_data) { |
| - NSData* data = [service_data objectForKey:uuid]; |
| - const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]); |
| - size_t length = [data length]; |
| - service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]), |
| - std::vector<uint8_t>(bytes, bytes + length)); |
| - } |
| + // Get Service Data. |
| + BluetoothDevice::ServiceDataMap service_data_map; |
| + NSDictionary* service_data = |
| + [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; |
| + for (CBUUID* uuid in service_data) { |
| + NSData* data = [service_data objectForKey:uuid]; |
| + const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]); |
| + size_t length = [data length]; |
| + service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]), |
| + std::vector<uint8_t>(bytes, bytes + length)); |
| + } |
| - // Get Tx Power. |
| - NSNumber* tx_power = |
| - [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey]; |
| - int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); |
| + // Get Tx Power. |
| + NSNumber* tx_power = |
| + [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey]; |
| + int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); |
| - device_mac->UpdateAdvertisementData( |
| - BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), |
| - std::move(service_data_map), |
| - tx_power == nil ? nullptr : &clamped_tx_power); |
| + device_mac->UpdateAdvertisementData( |
| + BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), |
| + std::move(service_data_map), |
| + tx_power == nil ? nullptr : &clamped_tx_power); |
| + } |
| if (is_new_device) { |
| std::string device_address = |
| @@ -644,4 +664,26 @@ BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { |
| return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
| } |
| +bool BluetoothAdapterMac::DoesCollideWithKnownDevice( |
| + CBPeripheral* peripheral, |
| + BluetoothLowEnergyDeviceMac* device_mac) { |
| + // Check that there are no collisions. |
| + std::string stored_device_id = device_mac->GetIdentifier(); |
| + std::string updated_device_id = |
| + BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
| + if (stored_device_id != updated_device_id) { |
| + VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
| + << std::endl |
| + << " " << stored_device_id << std::endl |
| + << " " << updated_device_id; |
| + // Collision, two identifiers map to the same hash address. With a 48 bit |
| + // hash the probability of this occuring with 10,000 devices |
| + // simultaneously present is 1e-6 (see |
| + // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| + // ignore the second device by returning. |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| } // namespace device |