Skip to main content

Implementation

Before running the example below, install the plugin and apply the native Android and iOS setup described in Installation.

Prerequisite for startDeviceScan

The startDeviceScan method requires a token. This token is temporary and must be generated on your backend by making a server-to-server request to our API before you call the SDK method.

Endpoint Details:

  • Method: POST
  • Path: /device_scan/token
  • Sandbox URL: https://d.sandbox.viewpkg.com/device_scan/token
  • Production URL: https://d.viewpkg.com/device_scan/token

Headers:

{
"Authorization": "YOUR_DEVICE_SCAN_API_KEY"
}

Body:

{
"session_id": "unique_session_id"
}

The successful response from this API will contain the token that you must pass to the startDeviceScan method.

note

The device scan method can be executed asynchronously. Therefore, there is no need to block the main thread to wait for this method to resolve. The user can interact normally with the app while the device scan is being processed in the background.

note

We recommend that the device scan method is executed as early as possible. Since it may need more time to collect all data, this early call is recommended so that the most complete device information is extracted.



import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:qitech_device_scan/qitech_device_scan.dart';

final _qitechDeviceScanPlugin = QitechDeviceScan();

// Step 1: Generate the temporary token via a server-to-server request
Future<String?> fetchDeviceScanToken(String sessionId) async {
final response = await http.post(
Uri.parse('<DEVICE_SCAN_API_URL>'),
headers: {
HttpHeaders.authorizationHeader: '<API_KEY>',
HttpHeaders.contentTypeHeader: 'application/json',
},
body: jsonEncode({'session_id': sessionId}),
);

if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return data['token'] as String?;
}
return null;
}

// Step 2: Initialize the SDK with the obtained token
final sessionId = '<SESSION_ID>';
final token = await fetchDeviceScanToken(sessionId);

if (token == null) {
print('Failed to fetch device scan token');
return;
}

final result = await _qitechDeviceScanPlugin.startDeviceScan(
token: token,
environment: CaaSEnvironment.sandbox,
sessionId: sessionId,
eventType: '<EVENT_TYPE>',
eventId: '<EVENT_ID>',
);

print('Device Scan result: $result');