Skip to content

How to initiate a scanning event

Set the Scanner Delegate

When a scanner starts connecting, set the delegate so you can receive barcodes as soon as they arrive (barcodes may arrive during discovery when the central is restoring).

To set the scanner delegate, confirm the PGPeripheralDelegate protocol and set the delegate property.


@interface ViewController () <PGPeripheralDelegate>

- (void)centralManager:(PGCentralManager *)centralManager connectingToScanner:(PGPeripheral *)scanner {
  scanner.delegate = self;
}

class ViewController: UIViewController, PGPeripheralDelegate {

func centralManager(_ centralManager: PGCentralManager, connectingToScanner scanner: PGPeripheral) {
  scanner.delegate = self
}


Initiate a scan

When you scan a barcode, the PGPeripheralDelegate delegate method peripheral:didScanBarcodeWithResult: is called and the PGScannedBarcodeResult object, containing the scan content and scan symbology, is retrieved:

- (void)peripheral:(nonnull PGPeripheral *)peripheral didScanBarcodeWithResult:(nonnull PGScannedBarcodeResult *)result {
  NSLog(@"Barcode scanned: %@", result.barcodeContent);
  if (result.barcodeSymbology){
    NSLog(@"Barcode symbology: %@", result.barcodeSymbology);
  }
}
func peripheral(_ peripheral: PGPeripheral, didScanBarcodeWith data: PGScannedBarcodeResult) {
  print("Barcode scanned: \(data.barcodeContent)")
  if let symbology = data.barcodeSymbology {
    print("Barcode symbology: \(symbology)")
  }
}