Init, Enable, Discover, Connect, Print

Initialization must be run once each time the app starts.
You can call the method directly on app start or before you call any other method from the CIDPrint Plugin.

The sample code below shows you a simple initialize method and associated callback you can use in your code.

the initialization method

				
					async initialize() {
        CIDPrint.addListener(
            CIDPrinterListenerTypes.PRINTER_LIBRARY,
            this.handlePrinterLibraryEvents.bind(this)
        );
        let event: PrinterLibraryEvent = await CIDPrint.initCIDPrinterLib();
        if(event.type === EventType.SUCCESS) {
            if(event.action === PrinterLibraryActionType.INITIALIZE) {
                this.enableBluetooth();
                // initCIDPrinterLib returns the required permissions as a string list
                let data: InitResult = event.data as InitResult;
                for(let i = 0; i < data.permissions.length; i++) {
                    // do something with the permissions
                    console.log(data.permissions[i]);
                }
            }
        }
}

				
			

the callback method to handle all events from CaptureID CIDPrint Plugin.

				
					  handlePrinterLibraryEvents(event: PrinterLibraryEvent) {
    switch(event.action) {
      case PrinterLibraryActionType.BLUETOOTH_DISABLE:
        this.statuslist.push('Bluetooth disabled: - ' + event.type + ' - ' + event.message);
        break;
      case PrinterLibraryActionType.BLUETOOTH_ENABLE:
        this.statuslist.push('Bluetooth enabled: - ' + event.type + ' - ' + event.message);
        this.setState({ enabled: event.type === EventType.SUCCESS});
        break;
      case PrinterLibraryActionType.BLUETOOTH_INITIALIZE:
        let data: BluetoothResult = event.data as BluetoothResult;
        console.log(data);
        break;
      case PrinterLibraryActionType.DISCOVER_START:
        // this event is received immediate after a call to discoverDevices
        break;
      case PrinterLibraryActionType.DISCOVER_DETECT:
        // a new device is detected and the bluetooth information for the device is delivered in data part of the event.
      case PrinterLibraryActionType.DISCOVER_FINISH:
        // the discovery process has completed and the discovered printer devices are delivered in the data part of the event
        break;
      case PrinterLibraryActionType.CONNECT:
        this.isConnecting = false;
//        CIDPlugin.enableScanCommandButton({enable: true});
        if(event.type === EventType.SUCCESS || event.type === EventType.NOTIFY) {
          let data: BluetoothResult = event.data as BluetoothResult;
          this.statuslist.push('device connected: - ' + data.connecteddevice?.name);
          this.setState({connected: true});
        } else {
          this.statuslist.push('device not connected: - ' + event.type + ' - ' + event.message);
        }
        break;
      case PrinterLibraryActionType.DISCONNECT:
        this.statuslist.push('device disconnected:');
        this.setState({connected: false});
        break;
      case PrinterLibraryActionType.PRINT:
        let result: PrinterResult = event.data as PrinterResult;
        this.statuslist.push('Printer status - ' + result.status);
        break;
      default:
        this.statuslist.push(event.action + ' - ' + event.type + ' - ' + event.message);
    }
    this.setState({ statuslist: this.statuslist });
  }

				
			

enable bluetooth printing

				
					  async enableBluetooth() {
    await CIDPrint.enableBluetoothPrinting({enable: true});
  }

				
			

start device discovery and retrieve the result.

				
					  discover() {
    CIDPrint.discoverDevices();
  }
				
			

the discover process will run in background.
the process invokes the defined callback method with one of three action types in the PrinterLibraryEvent struct.
in discover mode the data field of PrinterLibraryEvent id of type BluetoothResult.
the events are fired in the following order
immediately after calling discoverDevices an event with action type DISCOVER_START is fired once.

Each time a bluetooth printer device is discovered an event with action type of DISCOVER_DETECT is fired and the discovereddevice field of the BluetoothResult class contains a device class with the following fields:
address – the bluetooth mac address
name – the name of the device
bluetoothclass – the class id
type – the type id

on process completion an event of type DISCOVER_FINISH is called once.
the discovereddevices field from BluetoothResult class contains a list of all devices dicovered by this run.

Was this article helpful?

previous article

Get Started