Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/SwiftySensors/CoreBluetooth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ extension CBCharacteristic {
- parameter enabled: Notification Flag
*/
public func notify(_ enabled: Bool) {
service.peripheral.setNotifyValue(enabled, for: self)
service?.peripheral?.setNotifyValue(enabled, for: self)
}

/// Read the value of the Characteristic
public func read() {
service.peripheral.readValue(for: self)
service?.peripheral?.readValue(for: self)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftySensors/Sensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ extension Sensor: CBPeripheralDelegate {

/// :nodoc:
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let service = services[characteristic.service.uuid.uuidString] else { return }
guard let service = services[characteristic.service?.uuid.uuidString ?? ""] else { return }
Copy link
Copy Markdown

@jlevine22 jlevine22 Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought but would it logically make more sense to use guard to check for the service/uuidString instead of defaulting to an empty string?

guard let uuidString = characteristic.service?.uuid.uuidString, let service = services[uuidString] else { return }

Functionally I think they're about the same but it seems weird you might try to access a service with the uuid of ""

Copy link
Copy Markdown
Contributor

@netizen01 netizen01 Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the service param change from an optional? you can't access a map value with an optional key, hence the ""

Edit: ... I think the service param here became an optional, so the "" is necessary.

... this might be weird...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a callback from CoreBluetooth. You're already indexing services by uuid. If for some reason CB sends you an update for a service without a UUID, you're basically screwed, so, I'd say ignore it.

        guard let serviceUUID = characteristic.service?.uuid.uuidString else { return }
        guard let service = services[serviceUUID] else { return }

guard let char = service.characteristics[characteristic.uuid.uuidString] else { return }
if char.cbCharacteristic !== characteristic {
char.cbCharacteristic = characteristic
Expand All @@ -286,7 +286,7 @@ extension Sensor: CBPeripheralDelegate {

/// :nodoc:
public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
guard let service = services[characteristic.service.uuid.uuidString] else { return }
guard let service = services[characteristic.service?.uuid.uuidString ?? ""] else { return }
guard let char = service.characteristics[characteristic.uuid.uuidString] else { return }
if char.cbCharacteristic !== characteristic {
char.cbCharacteristic = characteristic
Expand Down