diff --git a/Sources/BirdbrainBLE/ble/peripheral/BLEPeripheral.swift b/Sources/BirdbrainBLE/ble/peripheral/BLEPeripheral.swift index bf9a3b6..6567dda 100644 --- a/Sources/BirdbrainBLE/ble/peripheral/BLEPeripheral.swift +++ b/Sources/BirdbrainBLE/ble/peripheral/BLEPeripheral.swift @@ -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 } \ No newline at end of file diff --git a/Sources/BirdbrainBLE/ble/peripheral/StandardBLEPeripheral.swift b/Sources/BirdbrainBLE/ble/peripheral/StandardBLEPeripheral.swift index 8a9cf77..c1cd58c 100644 --- a/Sources/BirdbrainBLE/ble/peripheral/StandardBLEPeripheral.swift +++ b/Sources/BirdbrainBLE/ble/peripheral/StandardBLEPeripheral.swift @@ -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? { diff --git a/Sources/BirdbrainBLE/uart/device/BaseUARTDevice.swift b/Sources/BirdbrainBLE/uart/device/BaseUARTDevice.swift index 05ddb1b..c9f9e45 100644 --- a/Sources/BirdbrainBLE/uart/device/BaseUARTDevice.swift +++ b/Sources/BirdbrainBLE/uart/device/BaseUARTDevice.swift @@ -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 { diff --git a/Sources/BirdbrainBLE/uart/device/UARTDevice.swift b/Sources/BirdbrainBLE/uart/device/UARTDevice.swift index fe34785..9783edf 100644 --- a/Sources/BirdbrainBLE/uart/device/UARTDevice.swift +++ b/Sources/BirdbrainBLE/uart/device/UARTDevice.swift @@ -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 } \ No newline at end of file diff --git a/Tests/BirdbrainBLETests/ble/BLETests.swift b/Tests/BirdbrainBLETests/ble/BLETests.swift index 1b63480..65b37eb 100644 --- a/Tests/BirdbrainBLETests/ble/BLETests.swift +++ b/Tests/BirdbrainBLETests/ble/BLETests.swift @@ -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 @@ -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) diff --git a/Tests/BirdbrainBLETests/uart/UARTTests.swift b/Tests/BirdbrainBLETests/uart/UARTTests.swift index de5535e..f6f85ed 100644 --- a/Tests/BirdbrainBLETests/uart/UARTTests.swift +++ b/Tests/BirdbrainBLETests/uart/UARTTests.swift @@ -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)