CGA_Bluetooth_Peripheral

public class CGA_Bluetooth_Peripheral : NSObject, RVS_SequenceProtocol
extension CGA_Bluetooth_Peripheral: CGA_Class_Protocol_UpdateDescriptor
extension CGA_Bluetooth_Peripheral: CBPeripheralDelegate

This class is instantiated when a Peripheral is connected, and will handle discovery of Services, Characteristics and Descriptors.

Public types, properties and methods.

  • This is the type we’re aggregating.

    Declaration

    Swift

    public typealias Element = CGA_Bluetooth_Service
  • This is our main cache Array. It contains wrapped instances of our aggregate CB type.

    Declaration

    Swift

    public var sequence_contents: Array<Element>
  • This is used to reference an “owning instance” of this instance, and it should be a CGA_Bluetooth_Parent instance.

    Declaration

    Swift

    public weak var parent: CGA_Class_Protocol?
  • This holds the discovery data that applies to this instance.

    Declaration

    Swift

    public var discoveryData: RVS_BlueThoth.DiscoveryData!
  • This returns the parent Central Manager

    Declaration

    Swift

    public var central: RVS_BlueThoth? { get }
  • Returns true, if we are currently connected.

    Declaration

    Swift

    public var isConnected: Bool { get }
  • The Peripheral is capable of sending writes back (without response).

    Declaration

    Swift

    public var canSendWriteWithoutResponse: Bool { get }
  • This is the signal strength, at the time of discovery, in dBm. This is also updated, as we receive RSSI change notifications.

    Declaration

    Swift

    public var rssi: Int { get }
  • This will contain any required scan criteria. It simply passes on the Central criteria.

    Declaration

    Swift

    public var scanCriteria: RVS_BlueThoth.ScanCriteria! { get }
  • id

    This returns a unique UUID String for the instance.

    Declaration

    Swift

    public var id: String { get }
  • The required init, with a “primed” sequence.

    Declaration

    Swift

    public required init(sequence_contents inSequence_Contents: [Element])

    Parameters

    sequence_contents

    The initial value of the Array cache.

  • Forces a connect. If already connected, nothing happens.

    Declaration

    Swift

    public func connect()
  • Forces a disconnect. If not connected, nothing happens.

    Declaration

    Swift

    public func disconnect()

Internal types, properties and methods.

  • This holds our Service wrapper instances until we have received all the Characteristics for them.

    Declaration

    Swift

    internal var stagedServices: Array<Element>
  • This returns the instance of CBPeripheral that is used by this instance.

    Declaration

    Swift

    internal var cbElementInstance: CBPeripheral! { get }
  • This is set to true, if the Central is requesting a disconnection (so we know it’s expected).

    Declaration

    Swift

    internal var disconnectionRequested: Bool

Private types, properties and methods.

  • This holds a list of UUIDs, holding the IDs of Services we are looking for. It is initialized when the class is instantiated.

    Declaration

    Swift

    private var _discoveryFilter: [CBUUID]

Instance Methods

  • This eliminates all of the stored results, and asks the Bluetooth subsystem to start over from scratch.

    Declaration

    Swift

    public func startOver()
  • This is the init that should always be used.

    Declaration

    Swift

    internal convenience init(discoveryData inCBPeriperalDiscoveryData: RVS_BlueThoth.DiscoveryData, services inServices: [String] = [])

    Parameters

    discoveryData

    The discovery data of the Peripheral.

    services

    An optional parameter that is an Array, holding the String UUIDs of Services we are filtering for. If left out, all available Services are found. If specified, this overrides the scanCriteria.

  • Called to add a Service to our “keeper” Array.

    Declaration

    Swift

    internal func addService(_ inService: CGA_Bluetooth_Service)

    Parameters

    inService

    The Service to add.

  • Request that the RSSI be updated.

    Declaration

    Swift

    internal func updateRSSI()

Private Instance Methods

  • This registers us with the Central wrapper.

    Declaration

    Swift

    private func _registerWithCentral()

CGA_Class_UpdateDescriptor Conformance

  • This eliminates all of the stored and staged results.

    Declaration

    Swift

    public func clear()

CGA_Class_Protocol Conformance

  • This searches the hierarchy, and will return any instance that has an ID that matches the string passed in. This could be a Peripheral, Service, Characteristic or Descriptor. The response will need to be cast.

    Declaration

    Swift

    public func findEntityByUUIDString(_ inUUIDString: String) -> CGA_Class_Protocol?

    Parameters

    inUUIDString

    The String for the UUID for which we are searching.

    Return Value

    Any element in the hierarchy with a UUID that matches the one passed in, or nil.

CBPeripheralDelegate Conformance

  • Called when the RSSI changes.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didReadRSSI inRSSI: NSNumber, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has discovered Services.

    didReadRSSI

    The new RSSI value, in dBm.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called when the Peripheral has discovered its Services. We treat discovery as “atomic.” We ask for all the Services at once, so this callback is complete for this Peripheral.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didDiscoverServices inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has discovered Services.

    didDiscoverServices

    Any error that may have occured. Hopefully, it is nil.

  • Called when a Service discovers Characteristics. We treat discovery as “atomic.” We ask for all the Characteristics at once, so this callback is complete for this Service.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didDiscoverCharacteristicsFor inService: CBService, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has discovered Characteristics.

    didDiscoverCharacteristicsFor

    The Service instance to which the Characteristics apply.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called when a Characteristic discovers all of its Descriptors. We treat discovery as “atomic.” We ask for all the Descriptors at once, so this callback is complete for this Characteristic.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didDiscoverDescriptorsFor inCharacteristic: CBCharacteristic, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has discovered Descriptors.

    didDiscoverDescriptorsFor

    The Characteristic instance to which the Descriptors apply.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called when any Services have been modified.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didModifyServices inInvalidatedServices: [CBService])

    Parameters

    inPeripheral

    The CBPeripheral that has modified Services.

    didModifyServices

    An Array of CBService instances that were modified.

  • Called when the notification state for a Characteristic changes.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didUpdateNotificationStateFor inCharacteristic: CBCharacteristic, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has the modified Characteristic.

    didUpdateNotificationStateFor

    The Characteristic that had its notification state changed.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called to update a Characteristic value (either in response to a read request, or a notify).

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didUpdateValueFor inCharacteristic: CBCharacteristic, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has the updated Characteristic.

    didUpdateValueFor

    The Characteristic instance to which the Update applies.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called to update a descriptor.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didUpdateValueFor inDescriptor: CBDescriptor, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has the updated Descriptor.

    didUpdateValueFor

    The Descriptor instance to which the Update applies.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called to indicate that a write to the a Characteristic was successful. MAJOR CAVEAT: The value property of the Characteristic will not necessarily have the newly-written value. This method is only called to act as a semaphore to let us know that the last write attempt succeeded.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didWriteValueFor inCharacteristic: CBCharacteristic, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has the updated Characteristic.

    didWriteValueFor

    The Characteristic instance to which the write applies.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called to indicate that a write to the a Descriptor was successful. MAJOR CAVEAT: The value property of the Descriptor will not necessarily have the newly-written value. This method is only called to act as a semaphore to let us know that the last write attempt succeeded.

    Declaration

    Swift

    public func peripheral(_ inPeripheral: CBPeripheral, didWriteValueFor inDescriptor: CBDescriptor, error inError: Error?)

    Parameters

    inPeripheral

    The CBPeripheral that has the updated Descriptor.

    didWriteValueFor

    The Descriptor instance to which the write applies.

    error

    Any error that may have occured. Hopefully, it is nil.

  • Called when the Peripheral is ready to listen to our advice. This can come some time after a failed write attempt.

    Declaration

    Swift

    public func peripheralIsReady(toSendWriteWithoutResponse inPeripheral: CBPeripheral)

    Parameters

    toSendWriteWithoutResponse

    The CBPeripheral that is now ready.