Activation
- iOS: ActivationModel.swift
- Android: MainActivity.java
Before using the SDK's scanning features, you must activate the license on the device that will use the SDK. Activation is only required when the SDK is used on a device for the first time, and it requires an online environment.
- Android: If the device undergoes a factory reset, activation will be required again and the device will be registered as a new device.
- iOS: If the app is deleted and reinstalled, activation will be required again and the device will be registered as a new device.
If you use more than one app that includes the EdgeOCR SDK on the same Android device, install EdgeOCR Device Core and select the device ID to use before activation. For details, see Share a Device ID Across Multiple Apps (Android).
1. Initializing NefrockLicenseAPI
Initialize NefrockLicenseAPI with your license key.
- iOS (Swift)
- Android (Java)
import EdgeOCRSwift
let licenseAPI = try LicenseBuilder()
.withLicenseKey("YOUR_LICENSE_KEY")
.build()
import com.nefrock.edgeocr.NefrockLicenseAPI;
import com.nefrock.edgeocr.EdgeError;
NefrockLicenseAPI licenseAPI;
try {
licenseAPI = new NefrockLicenseAPI.Builder(context)
.withLicenseKey("YOUR_LICENSE_KEY")
.build();
} catch (EdgeError e) {
// Handle initialization failure
Log.e("EdgeOCR", e.getMessage());
}
- Set
"YOUR_LICENSE_KEY"to your license key. - On Android, pass the app's
Contextas the first argument.
2. Performing Activation
Call the activate method to register the device.
- iOS (Swift)
- Android (Java)
do {
let license = try await licenseAPI.activate()
// Activation successful
} catch {
// Activation failed
print(error.localizedDescription)
}
import com.nefrock.edgeocr.License;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
ListenableFuture<License> future = licenseAPI.activate();
Futures.addCallback(
future,
new FutureCallback<License>() {
@Override
public void onSuccess(License license) {
// Activation successful
}
@Override
public void onFailure(Throwable t) {
// Activation failed
}
},
Runnable::run
);
Once activation is performed, the license information is saved to a file, so there is no need to activate again on subsequent app launches.
3. Checking Activation Status
You can use the isActivated method to check whether the device has already been activated.
- iOS (Swift)
- Android (Java)
do {
let license = try await licenseAPI.isActivated()
// Already activated
} catch {
// Not yet activated
}
ListenableFuture<License> future = licenseAPI.isActivated();
Futures.addCallback(
future,
new FutureCallback<License>() {
@Override
public void onSuccess(License license) {
// Already activated
}
@Override
public void onFailure(Throwable t) {
// Not yet activated
}
},
Runnable::run
);
This method first checks the local license file. If not found, it queries the server. It will not register the device as a new device.
Checking License Information
When isActivated or activate succeeds, it returns a License.
You can use License to check details such as the license expiration date.
| Information | iOS (Swift) | Android (Java) |
|---|---|---|
| License key | licenseKey: String | getLicenseKey(): String |
| Device ID | deviceId: String | getDeviceId(): String |
| Start date | startDate: Date? | getStartDate(): LocalDate |
| Expiration date | expirationDate: Date? | getExpirationDate(): LocalDate |
If the license has no start date or expiration date, the value is nil on iOS and null on Android.
- iOS (Swift)
- Android (Java)
do {
let license = try await licenseAPI.isActivated()
if let expirationDate = license.expirationDate {
// The date is returned as 00:00 UTC, so format it in UTC
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(identifier: "UTC")
print("Expiration date: \(formatter.string(from: expirationDate))")
} else {
print("Expiration date: none")
}
} catch {
// Not yet activated
}
import java.time.LocalDate;
ListenableFuture<License> future = licenseAPI.isActivated();
Futures.addCallback(
future,
new FutureCallback<License>() {
@Override
public void onSuccess(License license) {
LocalDate expirationDate = license.getExpirationDate();
Log.i("EdgeOCR", "Expiration date: "
+ (expirationDate != null ? expirationDate : "none"));
}
@Override
public void onFailure(Throwable t) {
// Not yet activated
}
},
Runnable::run
);
When the license file is valid, isActivated and activate return its contents immediately. They then fetch the latest license from the server in the background and update the file.
As a result, the first call after the device goes online returns the expiration date from before the update.
To confirm that the expiration date has been extended before returning the device to an offline environment, follow these steps:
- Connect the device to the internet.
- Quit the app completely, then start it again. The update from the server runs on the first call after the app starts.
- Call
isActivated. - Wait about 10 to 20 seconds, call
isActivatedagain, and check the expiration date in the returnedLicense.
If the expiration date has not changed, either communication with the server failed or the license has not yet been renewed on the server.
On app launch, first check with isActivated, and only call activate if the device is not yet activated.
- iOS (Swift)
- Android (Java)
func tryActivation(key: String) async -> String? {
let licenseAPI: NefrockLicenseAPI
do {
licenseAPI = try LicenseBuilder().withLicenseKey(key).build()
} catch {
return error.localizedDescription
}
do {
_ = try await licenseAPI.isActivated()
} catch {
// Not yet activated, so perform activation
do {
_ = try await licenseAPI.activate()
} catch {
return error.localizedDescription
}
}
return nil // Success
}
In the sample app, this is implemented in EdgeOCRSample/Models/Activation/ActivationModel.swift.
// Check activation status and perform activation if needed
ListenableFuture<License> future = licenseAPI.isActivated();
Futures.addCallback(
future,
new FutureCallback<License>() {
@Override
public void onSuccess(License license) {
// Already activated
activationButton.setText("Activated");
}
@Override
public void onFailure(Throwable t) {
// Not yet activated — activate on button click
activationButton.setText("Activate");
activationButton.setOnClickListener(v -> {
licenseAPI.activate();
});
}
},
Runnable::run
);
In the sample app, this is implemented in app/src/main/java/com/nefrock/edgeocr_example/MainActivity.java.
4. Deactivation
You can deactivate the license using the deactivate method.
The license information stored on the device will be deleted, and the server-side registration will also be removed.
- iOS (Swift)
- Android (Java)
do {
try await licenseAPI.deactivate()
// Deactivation successful
} catch {
// Deactivation failed
print(error.localizedDescription)
}
ListenableFuture<Void> future = licenseAPI.deactivate();
Futures.addCallback(
future,
new FutureCallback<Void>() {
@Override
public void onSuccess(Void result) {
// Deactivation successful
}
@Override
public void onFailure(Throwable t) {
// Deactivation failed
}
},
Runnable::run
);