Skip to content

Commit

Permalink
#119 Fixed GATT notifications
Browse files Browse the repository at this point in the history
Only notify connected client if write is from server and not client
write request
  • Loading branch information
colemancda committed Jan 21, 2019
1 parent ab33985 commit a14044a
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions Sources/GATTServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,35 +280,39 @@ public final class GATTServer {
let characteristic = service.characteristics.first(where: { $0.uuid == attribute.uuid })
else { return }

// inform delegate
if isLocalWrite == false {
didWrite?(attribute.uuid, attribute.handle, attribute.value)
}

// Client configuration
if let clientConfigurationDescriptor = characteristic.descriptors.first(where: { $0.uuid == .clientCharacteristicConfiguration }) {

guard let descriptor = GATTClientCharacteristicConfiguration(data: clientConfigurationDescriptor.value)
else { return }
// notify connected client if write is from server and not client write request
if isLocalWrite {

// notify
if descriptor.configuration.contains(.notify) {
// Client configuration
if let clientConfigurationDescriptor = characteristic.descriptors.first(where: { $0.uuid == .clientCharacteristicConfiguration }) {

let notification = ATTHandleValueNotification(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)
guard let descriptor = GATTClientCharacteristicConfiguration(data: clientConfigurationDescriptor.value)
else { return }

send(notification)
}

// indicate
if descriptor.configuration.contains(.indicate) {

let indication = ATTHandleValueIndication(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)
// notify
if descriptor.configuration.contains(.notify) {
let notification = ATTHandleValueNotification(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)
send(notification)
}

send(indication) { [unowned self] (confirmation) in
// indicate
if descriptor.configuration.contains(.indicate) {

self.log?("Confirmation: \(confirmation)")
let indication = ATTHandleValueIndication(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)

send(indication) { [unowned self] (confirmation) in

self.log?("Confirmation: \(confirmation)")
}
}
}

} else {

// writes from central should not notify clients (at least not this connected central)
didWrite?(attribute.uuid, attribute.handle, attribute.value)
}
}

Expand Down

0 comments on commit a14044a

Please sign in to comment.