Bluetooth Logo

Writing an SDK With Core Bluetooth – 09 – Protocols

This entry is part 12 of 24 in the series Writing an SDK With Core Bluetooth

FIRST DRAFT OF THE PROTOCOLS FOR THE SDK

I have taken the liberty of creating a Swift file that will define the SDK.

I have removed the [extensive] comments, in order to simplify the visual impact. The original file will contain the comments.

Here is the tag for this step. We can consider this our “starting point.” At this point, all four apps are complete, and use a “dummy” mocked version of the SDK (no actual Bluetooth going on). We also have the first full draft of the SDK ready.

MAIN SDK INTERFACE

The main SDK will be an instance that will conform to a specialization of the ITCB_SDK_Protocol protocol.

public protocol ITCB_SDK_Protocol {
    static func createInstance() -> ITCB_SDK_Protocol?
    var error: ITCB_Errors? { get }
    var isCoreBluetoothPoweredOn: Bool { get }
    var localName: String { get set }
    var observers: [ITCB_Observer_Protocol] { get set }
    func addObserver(_ observer: ITCB_Observer_Protocol) -> UUID!
    func removeObserver(_ observer: ITCB_Observer_Protocol)
    func isObserving(_ observer: ITCB_Observer_Protocol) -> Bool
}

DEVICES

Each device will conform to a specialization of the ITCB_Device_Protocol protocol.

public protocol ITCB_Device_Protocol {
    var name: String { get }
    var error: ITCB_Errors! { get }
    func amIThisDevice(_ inDevice: ITCB_Device_Protocol) -> Bool
    func rejectConnectionBecause(_ reason: ITCB_RejectionReason!)
}

public protocol ITCB_Device_Central_Protocol: ITCB_Device_Protocol {
    func sendAnswer(_ answer: String, toQuestion: String)
}

public protocol ITCB_Device_Peripheral_Protocol: ITCB_Device_Protocol {
    var question: String! { get }
    var answer: String! { get }
    func sendQuestion(_ question: String)
}

OBSERVERS

Observers are protocols to which the user must be bound. Note the uuid property. This is used internally by the SDK to identify the observer instance. It needs to be defined by the user, but the user doesn’t actually do anything with it.

public protocol ITCB_Observer_Protocol {
    var uuid: UUID { get set }
    func errorOccurred(_ error: ITCB_Errors, sdk: ITCB_SDK_Protocol)
}

public protocol ITCB_Observer_Central_Protocol: ITCB_Observer_Protocol {
    func questionAnsweredByDevice(_ device: ITCB_Device_Peripheral_Protocol)
    func questionAskedOfDevice(_ device: ITCB_Device_Peripheral_Protocol)
    func deviceDiscovered(_ device: ITCB_Device_Peripheral_Protocol)
}

public protocol ITCB_Observer_Peripheral_Protocol: ITCB_Observer_Protocol {
    func questionAskedByDevice(_ device: ITCB_Device_Central_Protocol, question: String)
    func answerSentToDevice(_ device: ITCB_Device_Central_Protocol, answer: String, toQuestion: String)
}

ENUMERATIONS

Currently, the only enumerations that we have, are various error and rejection codes. We may associate values with enumeration cases, so we can pass up more complete error information.

public enum ITCB_Errors: Error {
    case coreBluetooth(Error?)
    case sendFailed(Error?)
    case unknown(Error?)

    var localizedDescription: String {
        var ret: String
        
        switch self {
            case .coreBluetooth:
                ret = "ITCB-SDK-ERROR-BLUETOOTH"
            
            case .sendFailed:
                ret = "ITCB-SDK-ERROR-SEND-FAILURE"

            case .unknown:
                ret = "ITCB-SDK-ERROR-UNKNOWN"
        }
        
        return ret
    }

    var associatedValue: Error? {
        var ret: Error!
        
        switch self {
        case .coreBluetooth(let val):
            ret = val
            
        case .sendFailed(let val):
            ret = val
            
        case .unknown(let val):
            ret = val
        }
        
        return ret
    }
}

THIS CODE WILL REMAIN STABLE

It is expected that this code won’t need to change much (if at all) during the project. One of the nice things about developing an SDK, is that you can define a “choke point,” and work around that.

THE APPS ARE DONE

One thing that you will notice, is that the apps won’t change at all during this series. They’re done. All the changes will happen in the SDK-src directory. The Apps-src directory will be exactly the same at the last tag, as it was at the first tag.

We are now ready to start working on the Bluetooth implementation.