Bluetooth Operations
Native iOS ==> Bluetooth framework
Pair
Certain peripherals require pairing with the mobile device before a reading can be taken. You can check the requiresPairing
property on the VLDBluetoothPeripheral
object to know if it must be paired.
To pair a peripheral with the mobile device, call the pairPeripheral:
method on VLDBluetoothPeripheralController
.
let peripheral: VLDBluetoothPeripheral
let controller = VLDBluetoothPeripheralController()
controller.delegate = self
controller.pairPeripheral(peripheral)
To know if a peripheral was successfully paired you will need to implement the pairing methods of the VLDBluetoothPeripheralControllerDelegate
protocol.
func bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, didPairPeripheral peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata) {
// Peripheral paired successfully
}
func bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, didNotPairPeripheral peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata, error: (any Error)?) {
// Peripheral did not pair
}
Read
Once you are ready to read from a peripheral, the process is fairly similar to the pairing process. You should initially show the peripheral's instructions
, and then show the readingInstructions
once bluetoothPeripheralController:isReadyToReadFromPeripheral:metadata:
is called..
let peripheral: VLDBluetoothPeripheral = // A peripheral from the supportedPeripherals list
let controller = VLDBluetoothPeripheralController()
controller.delegate = self
controller.read(from: peripheral)
You must also implement the reading methods of the VLDBluetoothPeripheralControllerDelegate
protocol.
func bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, isReadyToReadFrom peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata) {
// Time to present the readingInstructions to the user
}
func bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, shouldSubmitReadings records: [VLDInformRecord], from peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata) -> Bool {
// To have the reading automatically added to the session and uploaded, return true from this method.
// To first have the reading validated by the user, you can return false from this method. Once the user has validated the reading
// you must manually submit the record by calling VLDSession.sharedInstance().submitInformRecord(record)
return true
}
bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, readingFailedFor peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata, error: (any Error)?) {
// Reading failed
}
Read With A Filter Applied
readFromPeripheral:
will connect to and read from the first peripheral that it finds that matches the peripheral
parameter.
To limit reads to specific instance of a peripheral, you can use readFromPeripheral:withOptions:
. This may be useful for situations in which more than one of the same peripheral might be in use at the same time.
The options parameter accepts an instance of VLDBluetoothOperationOptions
. Set options.allowedPeripheralUUIDs
to an array of peripheral UUIDs that you want to allow reads from; peripherals with UUIDs not on the list will be ignored. If the list is nil
, no peripherals will be ignored; if the list is an empty array, all peripherals will be ignored. Peripheral UUIDs can be retrieved from the pair and read delegate callback metadata
object, e.g. metadata.peripheralUUID
.
Here's an example that captures the peripheral UUID from a successful pairing and then uses it in a read call.
// Capture `peripheralUUID` in pairing delegate method
func bluetoothPeripheralController(_ controller: VLDBluetoothPeripheralController, didPairPeripheral peripheral: VLDBluetoothPeripheral, metadata: VLDBluetoothOperationMetadata) {
if let peripheralUUID = metadata.peripheralUUID {
// store `peripheralUUID` somewhere
self.recentPeripheralUUID = peripheralUUID
}
}
// Later, to read from that specific peripheral
let peripheral = // a VLDBluetoothPeripheral
let options = VLDBluetoothOperationOptions()
if let peripheralUUID = self.recentPeripheralUUID {
options.allowedPeripheralUUIDs = [peripheralUUID]
}
controller.read(from: peripheral, with: options)
Updated about 1 month ago