Skip to content

Commit

Permalink
Added maximumWriteWithResponseDataLength() and maximumWriteWithoutRes…
Browse files Browse the repository at this point in the history
…ponseDataLength() methods
  • Loading branch information
chrisbartley committed Aug 29, 2020
1 parent 6ea0cab commit 1f048c7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Sources/BirdbrainBLE/ble/peripheral/BLEPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ public protocol BLEPeripheral: class {

func read(fromCharacteristic uuid: CBUUID) -> Bool

// Tries to write the given data to the specified characteristic. Returns true if the characteristic exists and
// supports writing with response; false otherwise.
/// Tries to write the given data to the specified characteristic. Returns true if the characteristic exists and
/// supports writing with response; false otherwise.
func writeWithResponse(bytes: [UInt8], toCharacteristic uuid: CBUUID) -> Bool
func writeWithResponse(data: Data, toCharacteristic uuid: CBUUID) -> Bool

// Tries to write the given data to the specified characteristic. Returns true if the characteristic exists and
// supports writing without response; false otherwise.
/// Tries to write the given data to the specified characteristic. Returns true if the characteristic exists and
/// supports writing without response; false otherwise.
func writeWithoutResponse(bytes: [UInt8], toCharacteristic uuid: CBUUID) -> Bool
func writeWithoutResponse(data: Data, toCharacteristic uuid: CBUUID) -> Bool

/// Returns the maximum amount of data, in bytes, you can send to a characteristic in a single write-with-response.
func maximumWriteWithResponseDataLength() -> Int

/// Returns the maximum amount of data, in bytes, you can send to a characteristic in a single write-without-response.
func maximumWriteWithoutResponseDataLength() -> Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ open class StandardBLEPeripheral: NSObject, BLEPeripheral {
return write(data: data, toCharacteristic: uuid, writeType: .withoutResponse)
}

public func maximumWriteWithResponseDataLength() -> Int {
peripheral.maximumWriteValueLength(for: .withResponse)
}

public func maximumWriteWithoutResponseDataLength() -> Int {
peripheral.maximumWriteValueLength(for: .withoutResponse)
}

//MARK: - Private methods

private func findCharacteristic(havingUUID uuid: CBUUID) -> CBCharacteristic? {
Expand Down
4 changes: 4 additions & 0 deletions Sources/BirdbrainBLE/uart/device/BaseUARTDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ open class BaseUARTDevice: UARTDevice {
public func writeWithoutResponse(data: Data) {
let _ = blePeripheral.writeWithoutResponse(data: data, toCharacteristic: BaseUARTDevice.txUUID)
}

public func maximumWriteWithResponseDataLength() -> Int { blePeripheral.maximumWriteWithResponseDataLength() }

public func maximumWriteWithoutResponseDataLength() -> Int { blePeripheral.maximumWriteWithoutResponseDataLength() }
}

extension BaseUARTDevice: BLEPeripheralDelegate {
Expand Down
6 changes: 6 additions & 0 deletions Sources/BirdbrainBLE/uart/device/UARTDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public protocol UARTDevice: UARTDeviceIdentifier {
func writeWithResponse(data: Data)
func writeWithoutResponse(bytes: [UInt8])
func writeWithoutResponse(data: Data)

/// Returns the maximum amount of data, in bytes, you can send in a single write-with-response.
func maximumWriteWithResponseDataLength() -> Int

/// Returns the maximum amount of data, in bytes, you can send in a single write-without-response.
func maximumWriteWithoutResponseDataLength() -> Int
}
5 changes: 3 additions & 2 deletions Tests/BirdbrainBLETests/ble/BLETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ final class BLETests: XCTestCase {
XCTAssertFalse(centralManager.connectToPeripheral(havingUUID: uuid), "Redundant connect while connected should return false")

// sleep for a second
print("Sleeping for 10 seconds...")
print("Sleeping for 1 second...")
do {
sleep(10)
sleep(1)
}

// now disconnect
Expand Down Expand Up @@ -250,6 +250,7 @@ final class BLETests: XCTestCase {
print("END WAITING FOR connectSuccessExpectation \(Date().timeIntervalSince1970)")

if let connectedPeripheral = delegate.connectedPeripheral {
print("Peripheral has a max write-with-response size of [\(connectedPeripheral.maximumWriteWithResponseDataLength())] and a max write-without-response size of [\(connectedPeripheral.maximumWriteWithoutResponseDataLength())]")
print("[\(Date().timeIntervalSince1970)]: Start running tests...")
let additionalTestsDoneExpectation = expectation(description: "Additional tests are done")
additionalTests(connectedPeripheral, additionalTestsDoneExpectation)
Expand Down
1 change: 1 addition & 0 deletions Tests/BirdbrainBLETests/uart/UARTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ final class UARTTests: XCTestCase {
if let connectedUUID = delegate.connectedUUID {
if let uart = deviceManager.getDevice(uuid: connectedUUID) {
print("Connected to UART \(uart.uuid)")
print("UART has a max write-with-response size of [\(uart.maximumWriteWithResponseDataLength())] and a max write-without-response size of [\(uart.maximumWriteWithoutResponseDataLength())]")
print("[\(Date().timeIntervalSince1970)]: Start running tests...")
let additionalTestsDoneExpectation = expectation(description: "Additional tests are done")
additionalTests(uart, additionalTestsDoneExpectation)
Expand Down

0 comments on commit 1f048c7

Please sign in to comment.