Skip to content

Commit

Permalink
Merge pull request #63 from PureSwift/feature/add-alert-level
Browse files Browse the repository at this point in the history
Added GATT Alert Level Characteristic
  • Loading branch information
colemancda authored Jun 13, 2018
2 parents a39c083 + b2cad3b commit c7ad6ac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Sources/GATTCharacteristic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,54 @@ extension GATTAerobicHeartRateUpperLimit: CustomStringConvertible {
}
}

/**
Alert Level
The level of an alert a device is to sound. If this level is changed while the alert is being sounded, the new level should take effect.
The value of the characteristic shall be an unsigned 8 bit integer that has a fixed point exponent of 0. The Alert Level characteristic defines the level of alert, and is one of the following three values:
• Value 0, meaning “No Alert”
• Value 1, meaning “Mild Alert”
• EValue 2, meaning “High Alert”
• Example:
The value 0x01 is interpreted as “Mild Alert”
- SeeAlso: [Alert Level](https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.alert_level.xml)
*/
public enum GATTAlertLevel: UInt8, GATTProfileCharacteristic {

public static var uuid: BluetoothUUID { return .alertLevel }

internal static let length = MemoryLayout<UInt8>.size

/// No alert.
case none = 0x00

/// Mild alert.
case mild = 0x01

/// High alert.
case high = 0x02

public init?(data: Data) {

guard data.count == type(of: self).length
else { return nil }

self.init(rawValue: data[0])
}

public var data: Data {

return Data([rawValue])
}
}

// MARK: - Supporting Types

public enum GATTBeatsPerMinute {
Expand Down
15 changes: 14 additions & 1 deletion Tests/BluetoothTests/GATTCharacteristicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ final class GATTCharacteristicTests: XCTestCase {
("testAlertNotificationControlPoint", testAlertNotificationControlPoint),
("testBloodPressureMeasurement", testBloodPressureMeasurement),
("testAerobicHeartRateLowerLimit", testAerobicHeartRateLowerLimit),
("testAerobicHeartRateUpperLimit", testAerobicHeartRateUpperLimit)
("testAerobicHeartRateUpperLimit", testAerobicHeartRateUpperLimit),
("testAlertLevel", testAlertLevel)
]

func testDateTime() {
Expand Down Expand Up @@ -282,4 +283,16 @@ final class GATTCharacteristicTests: XCTestCase {
XCTAssertEqual(GATTAerobicHeartRateUpperLimit.uuid, .aerobicHeartRateUpperLimit)
XCTAssertEqual(BeatsPerMinute.unitType, .beatsPerMinute)
}

func testAlertLevel() {

let data = Data([0x01])

guard let characteristic = GATTAlertLevel(data: data)
else { XCTFail("Could not decode from bytes"); return }

XCTAssertEqual(characteristic.data, data)
XCTAssertEqual(characteristic, .mild, "The value 0x01 should be interpreted as mild Alert")
XCTAssertEqual(GATTAlertLevel.uuid, .alertLevel)
}
}

0 comments on commit c7ad6ac

Please sign in to comment.