Connect to our products with CaptureSDK¶
First initialize Capture Helper. Please refer to Capture Helper initialization.
Then the steps to connect to Bluetooth Low Energy (BLE) devices like NFC reader/writer and barcode scanner are the following:
Implement the
CaptureHelperDeviceManagerPresenceDelegate
protocol to be notified when a Device Manager is available.Implement the
CaptureHelperDeviceManagerDiscoveryDelegate
protocol to be notified when the discovery has ended and has discovered Bluetooth LE devices.Then you can adapt the connection flow to your own UI as the discovery provides devices in the vicinity.
A full connection flow is available in our Single Entry sample app.
Add Bluetooth Low Energy or Bluetooth Classic devices Discovery¶
You can chose whether Bluetooth Classic or Bluetooth LE Discovery.
The major difference between Bluetooth Classic and Bluetooth LE is that Bluetooth Classic uses the iOS native system picker to select the device to connect to, while Bluetooth LE Discovery provides the list of devices in the vicinity and lets you build an UI that fits your app to chose which device(s) to connect to.
As Apple does not provide the same kind of experience for both discoveries, it’s hard to provide a unified User Experience.
Bluetooth LE Discovery¶
Discovery and adding a device
Launch the discovery for Bluetooth LE devices in your own UI or directly once the Device Manager is available:
func didNotifyArrivalForDeviceManager(_ device: CaptureHelperDeviceManager, withResult result: SKTResult) {
print("didNotifyArrivalForDeviceManager: \(String(describing: device.deviceInfo.name))")
// Retain the device manager for later purpose
self.deviceManager = deviceManager
// Here we start the discovery with 'addBluetoothDevice' and the parameter '.bluetoothLowEnergy'
captureHelper.addBluetoothDevice(.bluetoothLowEnergy , withCompletionHandler: { result in
print("addBluetoothDevice - Bluetooth Low Energy: \(result.rawValue)")
})
// Here you can show your own UI to show the discovered devices
showMyDiscoveryUI()
}
Then for each discovered device the didDiscoverDevice
delegate is called. We advise you to keep an array of discovered devices to show them in your own UI:
Here device
is a CaptureHelperDeviceManager
instance.
func didDiscoverDevice(_ device: SKTCaptureDiscoveredDeviceInfo, fromDeviceManager deviceManager: CaptureHelperDeviceManager) {
print("didDiscoverDevice: \(device.name) - \(device.identifierUuid)")
// Keep the discovered device in an array to show them in your own UI
self.discoveredDevices.append(device)
self.myDiscoveryTableView.reloadData()
}
Here device
is a SKTCaptureDiscoveredDeviceInfo
instance.
The device discovery ends once the time out has elapsed and notifies your app by calling the didDiscoveryEndWithResult
delegate:
func didEndDiscoveryWithResult(_ result: SKTResult, fromDeviceManager deviceManager: CaptureHelperDeviceManager) {
print("end discovery with result: \(result.rawValue)")
}
To connect to a discovered device, use the Capture Helper API connectToDiscoveredDevice
by using the Device Manager retained earlier:
self.deviceManager.connectToDiscoveredDevice(selectedDiscoveredDevice) { result in
print("connectToDiscoveredDevice: \(result.rawValue)")
// Keep the connected device in an array to show them in your own UI
self.connectedDevices.append(device)
self.myConnectedDevicesTableView.reloadData()
}
Note
Then the device will connect automatically and you will be notified in the didNotifyArrivalForDevice
delegate of the CaptureHelperDevicePresenceDelegate
protocol.
The next time you open your application, the Device Manager will automatically connect to this connected device as it becomes a favorite device.
Removing a device
To remove favorite devices, use the Capture Helper API removeFavoriteDevices
. Pass the device’s GUID to remove it:
if let deviceGuid = device.deviceInfo.guid {
captureHelper.removeBluetoothDevice(deviceGuid) { result in
print("removeBluetoothDevice: \(result.rawValue)")
// Update the connected devices array to update your own UI
if result == .E_NOERROR {
if let index = self.connectedDevices.firstIndex(of: device) {
self.connectedDevices.remove(at: index)
self.myConnectedDevicesTableView.reloadData()
}
}
}
}
Here device
is a CaptureHelperDevice
instance.
Bluetooth Classic Discovery¶
Launch the discovery for Bluetooth Classic devices. This will show the iOS native system picker to select the device(s) to connect to:
Note
The Bluetooth Classic discovery can be launched right after the opening of the CaptureSDK as it does not need the Device Manager (which is only for Bluetooth LE devices).
Devices will connect automatically and you will be notified in the didNotifyArrivalForDevice
delegate of the CaptureHelperDevicePresenceDelegate
protocol.
The next time you open your application, CAptureSDK will get the notificaiton for the iOS External Accessory framework and the device will automatically connect.
// Here we start the discovery with 'addBluetoothDevice' and the parameter '.bluetoothClassic'
captureHelper.addBluetoothDevice(.bluetoothClassic , withCompletionHandler: { result in
print("addBluetoothDevice - Bluetooth Classic: \(result.rawValue)")
})
Additional Context¶
Note
This code shown above updates the UI from the didNotifyArrivalForDevice
and from the didNotifyRemovalForDevice
which requires it to be executed in
the main thread. This could be accomplished in different ways, but Capture
Helper provides a property to simplify this process as shown here:
capture.dispatchQueue = DispatchQueue.main
This is usually done before opening Capture. The same logic applies for each
device received in the didNotifyArrivalForDevice
if the callback of a
device function needs to update the UI.
Note
For a combo device like the S370 which has 2 devices, there will be two didNotifyArrivalForDevice
and two didNotifyRemovalForDevice
notifications.
The following code shows how you can distinghuish and handle them:
func didNotifyArrivalForDevice(_ device: CaptureHelperDevice, withResult result: SKTResult) {
print("didNotifyArrivalForDevice: \(String(describing: device.deviceInfo.name))")
if device.deviceInfo.deviceType == .NFCS370 {
// handle the NFC reader of the S370
} else if device.deviceInfo.deviceType == .scannerS370 {
// handle the Barcode scanner of the S370
}
}
You can also check if a device is barcode reader or a NFC reader when you get the didNotifyArrivalForDevice
:
func didNotifyArrivalForDevice(_ device: CaptureHelperDevice, withResult result: SKTResult) {
print("didNotifyArrivalForDevice: \(String(describing: device.deviceInfo.name))")
if device.isNFCReader {
// handle the NFC reader of the S370 or another NFC reader like a S550
} else if device.isBarcodeReader {
// handle the Barcode scanner of the S370 or another barcode scanner like a S320 or S740
}
}