Skip to main content

Documentation

Device Manager

The main class responsible for managing BLE device interactions, including pairing, configuration, barcode reading, and streaming device data.

This class offers:

  • Device pairing with dynamic barcode generation.
  • Reading barcodes from connected devices.
  • Applying device configurations via XML files.
  • Getting and setting single configuration values.
  • Streaming device info and battery stats.
  • Graceful handling of disconnection, errors, and device unlinking.

Initializers

init()

Initializes a new instance of DeviceManager.

func setDelegate(DeviceManagerDelegate?)

Sets the delegate to receive device events.

let manager = DeviceManager()
manager.setDelegate(self)

Instance Methods

Pairing

func startPairing()

Begins the pairing process with the BLE device.

func stopPairing()

Stops the pairing process.

manager.startPairing()   // Begin pairing process
manager.stopPairing() // Cancel pairing

func appMovedToForeground()

Call this method to notify that the hosting app moved to foreground. It is important to call this method whenever the app moves to foreground otherwise the SDK could have problems linking with a device previously linked and unlinked.

struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase

let deviceManager = DeviceManager()

var body: some View {
VStack {
Text("Your app")
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
deviceManager.appMovedToForeground()
}
}
}
}

Barcode read

func startReadingBarcode()

Sends a command to begin barcode scanning on the device.

func stopReadingBarcode()

Sends a command to stop barcode scanning on the device.

manager.startReadingBarcode()
manager.stopReadingBarcode()

Configuration

func applyConfig(from: URL)

Applies configuration to the device from a local XML file.

func restoreDefaultConfig()

Restores the device to its default configuration.

func setConfigValue(for: String, value: String)

Sets a configuration value on the device.

func getConfigValue(for: String)

Restores the device to its default configuration.

manager.applyConfig(from: configURL)                      // Apply configuration from XML
manager.setConfigValue(for: "code", value: "value") // Set single value
manager.getConfigValue(for: "code") // Read config value
manager.restoreDefaultConfig() // Reset config

Commands

func findMyDevice()

Sends a “find my device” command to the device.

func unlinkDevice()

Starts the unlinking process to disconnect and unlink the device.

manager.findMyDevice()      // Triggers device indicator
manager.unlinkDevice() // Unlinks the device

DeviceManagerDelegate

A protocol that defines the delegate methods for DeviceManager. Use this to respond to key events like connection, disconnection, barcode reads, and errors.

func didConnect()

Called when a device is connected.

func didDisconnect()

Called when the device disconnects.

func didFailWith(error: DeviceManagerError)

Called when a device-related error occurs.

func didGeneratePairingBarcode(UIImage)

Called when a pairing barcode is generated.

func didGetConfigData([ConfigValue])

Called when configuration values are successfully get from the device.

func didReadBarcodeData(BarcodeData)

Called when a barcode is read from the device.

func didRestoreConnection()

Called when the connection to a previously linked device is restored.

func didSetConfigData([ConfigValue])

Called when configuration values are successfully set on the device.

func didUnlink()

Called when the device is unlinked.

func didUpdateBatteryData(BatteryData)

Called when new battery data is received.

func didUpdateDeviceDetails(DeviceDetails)

Called when updated device details are received.

BarcodeData

Represents the data of a barcode.

Instance Properties

let barcodeID: String

Represents barcode type.

let data: String

Represents raw data converted to a string.

let rawData: [UInt8]

Represents raw data in bytes.

ConfigValue

Represents a codiscan configuration value

Instance Properties

var code: String

The code of the configuration property

var data: String

The value of the configuration property

DeviceDetails

A simple struct represeting information of a codiscan device

Instance Properties

let model: String?

Codiscan device model name

let serialNumber: String?

Codiscan device serial number

let swRevision: String?

Codiscan device firmware version.

BatteryDataField

Represents the key of a battery data field

Enumeration Cases

case batteryCurrent

Battery latest current

case batteryCycleCount

Battery cycle count

case batterySoC

Battery state of charge

case batterySoH

Battery state of health

case batteryTemp

Battery latest temperature (in tenths of a celsius degree)

Instance Properties

var description: String

Human readable description of a battery data field.

BleManagerError

Represents an error in the BLE layer

Enumeration Cases

case connectionFailed(Error)

The connection has failed with the associated error value

case disconnectedPeripheral

The codiscan device has been disconnected.

DeviceManagerError

Represents errors that may occur while using the DeviceManager.

Enumeration Cases

case alreadyPaired

The device is already paired and it should be manually unpaired from the iPhone settings to be connected again.

case barcodeGenerationFailed

Barcode generation failed.

case ble(BleManagerError)

An error from the BLEManager layer.

case configParserInitFailed

Failed to initialize the configuration parser.

case configParsingFailed

Failed to parse the configuration file.