CodeService

The IAPMiniProgram SDK calls the CodeService SPI to launch the QR code or barcode scanner. For details on how the SPI functions, refer to the CodeService workflow.

This SPI provides the scan method. The method details are as follows:

scan

IAPMiniprogram SDK calls the scan method to launch the QR code or barcode scanner.

Method signature

copy
Future<ScannerResult> scan(ScannerOption option, APIContext apiContext);

Request parameters

Field

Data type

Required

Description

option

ScannerOption

Yes

An object that specifies the details of the scan request from the mini program. When this parameter is not specified, the default values of this parameter are used.

apiContext

APIContext

Yes

A context object, which carries the mini program runtime metadata.

Response parameters

Field

Data type

Required

Description

result

Future<ScannerResult>

Yes

The result of the scan request.

Error codes

Error code

Error message

10

ERROR_USER_CANCEL

Users cancel the scan action.

2001

ERROR_OPERATION_FAILED

Failed to scan the code.

Samples

The following samples show how to implement the CodeService SPI to enable QR code and barcode scanning capabilities.

  1. Create a class that implements the CodeService interface.
  2. Implement the scan method.

The samples differ depending on whether to open a Flutter page during the SPI call:

Open a Flutter page

copy
class TestCodeService implements CodeService {
  @override
  Future<ScannerResult> scan(
    ScannerOption option, APIContext apiContext) async {
    ScannerResult result = ScannerResult('');
    result.showFlutterPage = true;
    result.flutterPageRoute = '/code';
    return result;
  }
}

Remain inside the mini program

copy
class TestCodeService implements CodeService {
  @override
  Future<ScannerResult> scan(
    ScannerOption option, APIContext apiContext) async {
    ScannerResult result = ScannerResult('xxx');
    return result;
  }
}