Integration of Huawei Scan Kit, Ads Kit and Account Kit in Code Scanner Application

Sree Prasad
5 min readOct 13, 2021

--

Code Scanner App is an application that allows users to scan a QR code or barcode to retrieve information. In this article, the integration of Huawei Kits is used in the Code Scanner App such as Account Kit, Ads Kit and Scan Kit are used in developing this application.

Link to the Application

https://appgallery.huawei.com/app/C104800793

Preparation:

1. You should register an account for Huawei Developer and get verified by them before you develop an application. Here is the guideline you may refer to — Huawei ID Registration.

2. All required settings are necessary to add as mentioned in HMS Document.

3. Sign in to your AppGallery Connect and create a project.

4. Add an app to your project.

5. Generate an SHA-256 certificate fingerprint for your application.

6. Download the agconnect-services.json file and copy it to your app’s root directory.

7. In Manage APIs section, enable Account Kit.

Integration

1. In Android Studio, open the build.gradle file in the root directory and add the maven url inside the buildscript respositories.

2. Add the classpath inside the dependencies section of the project build.gradle file.

3. Add the maven url inside the respositories in the setting.gradle file.

4. In app-level build.gradle file, add the plugins configuration.

5. Add build dependency in the dependencies block with the latest version. You may refer to here https://developer.huawei.com/consumer/en/hms to get the latest version of Account Kit, Ads Kit and Scan Kit.

6. Sync the gradle file.

7. Declare the permissions and features in AndroidManifest.xml.

Code Implementation

1. Account Kit

Using the Account Kit,it enables users to have a quick sign-in authorization function by only click the “Sign in with HUAWEI ID” button. Users can sign in here via Silent Sign in. After sign in it will call dealWithResultOfSignIn method.

private AccountAuthService mAuthService;
private AccountAuthParams mAuthParam;
private static final int REQUEST_CODE_SIGN_IN = 1000;
private static final String TAG = "Scan Me";
HuaweiIdAuthButton huaweiIdAuthButton;
private ImageView logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
huaweiIdAuthButton = findViewById(R.id.btnLogin);
huaweiIdAuthButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
silentSignInByHwId();
}
});
logo = findViewById(R.id.logo);
}
private void silentSignInByHwId() {
mAuthParam = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setEmail()
.setIdToken()
.createParams();
mAuthService = AccountAuthManager.getService(this, mAuthParam);
Task<AuthAccount> task = mAuthService.silentSignIn();
task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
@Override
public void onSuccess(AuthAccount authAccount) {
dealWithResultOfSignIn(authAccount);
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
Intent signInIntent = mAuthService.getSignInIntent();
startActivityForResult(signInIntent, REQUEST_CODE_SIGN_IN);
}
}
});
}
private void dealWithResultOfSignIn(AuthAccount authAccount) {
Log.i(TAG, "idToken:" + authAccount.getIdToken());
Log.i(TAG, "Name:" + authAccount.getDisplayName());
Log.i(TAG, "Email:" + authAccount.getEmail());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SIGN_IN) {
Log.i(TAG, "onActivityResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN);
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
if (authAccountTask.isSuccessful()) {
AuthAccount authAccount = authAccountTask.getResult();
Log.i(TAG, "onActivityResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN);
Toast.makeText(getApplicationContext(),"Login success", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(SignInActivity.this, ScanActivity.class);
startActivity(myIntent);
} else {
Log.e(TAG, "sign in failed : " +((ApiException)authAccountTask.getException()).getStatusCode());
Toast.makeText(getApplicationContext(),"Login failed", Toast.LENGTH_SHORT).show();
}
}
}

2. Ads Kit

Huawei Ads Kit provide and display high-quality ad content in the application to the users. It also helps the user monetize traffic.

//Banner Ads Code Start here
BannerView bannerView = new BannerView(this);
bannerView.setAdId("testw6vs28auh3");
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57);
ConstraintLayout constraintLayout = findViewById(R.id.ad_frame);
constraintLayout.addView(bannerView);
bannerView.setBannerRefresh(30);
// Create an ad request to load an ad.
AdParam adParam = new AdParam.Builder().build();
bannerView.loadAd(adParam);
//Banner Ads Code End here

3. Scan Kit

Scan Kit can detect, magnifies, and recognizes QR codes and barcodes from a distance. It retrieves information and displays the results for the users.

public static final int REQUEST_CODE_SCAN = 111;
private static final int DEFAULT_VIEW = 222 ;
TextView result_url;
TextView result_title;
Button btnResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
result_url = findViewById(R.id.result);
result_title = findViewById(R.id.resultTitle);
btnResult = findViewById(R.id.button_result);
}
public void newViewBtnClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.requestPermissions(
new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
DEFAULT_VIEW);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissions == null || grantResults == null || grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
return;
}
if (requestCode == DEFAULT_VIEW) {
ScanUtil.startScan(ScanActivity.this, REQUEST_CODE_SCAN, new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create());
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//When the scan code page is over, process the scan code result
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK || data == null) {
return;
}

if (requestCode == REQUEST_CODE_SCAN) {
Object obj = data.getParcelableExtra(RESULT);
if (obj instanceof HmsScan) {
if (!TextUtils.isEmpty(((HmsScan) obj).getOriginalValue())) {
result_title.setVisibility(View.VISIBLE);
result_url.setVisibility(View.VISIBLE);
result_url.setTextColor(getColor(R.color.black));
result_url.setText(((HmsScan) obj).getOriginalValue());
btnResult.setVisibility(View.VISIBLE);
btnResult.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
Uri webpage = Uri.parse(((HmsScan) obj).getOriginalValue());
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
return;
}
}
}
}

Results

Conclusion

In conclusion, I have integrated Huawei Scan Kit, Account Kit amd Ads Kit in the application. I have gained a lot of experience by handling Huawei Kits in application development. There is still a lot of different type of kits which I need to learn and improve my skills to build a more user-friendly and a better application.

References

Scan Kit

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-default-view-0000001050043961

Account Kit

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-scenario-id-token-0000001116078504

Ads Kit

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-banner-0000001050066915

--

--