Introduction:
Huawei Mobile Services (HMS) brings users many advanced features like Huawei ID, push notifications, payments and other services for its device ecosystem. Developers of HMS Core capabilities and services only need to integrate the HMS Software Development Kit in order to use Huawei’s open capabilities.
In this article, using HMS core services, we will develop an app to store the scan kit result in server using cloud DB SDK.
Accessing Cloud DB
Since Cloud DB is in beta stage, developers need to send a mail to activate this service in their app. Developer needs to create Huawei developer account. Once account created, developers need to create an app in App Gallery connect. After completing all the necessary steps, developer have to request the activation of the service by sending an e-mail to agconnect@huawei.com with the subject as “[Cloud DB]-[Company name]-[Developer account ID]-[App ID]”. To access the app ID, see Querying App Information. Usually it takes 1 to 3 days for activation, once activated , developer can integrate cloud DB service in their app.
Steps:
- Log in to AppGallery Connect and select My apps.
- Select Application for which you want to create object types.
- Click on Develop. Select build > Cloud DB from left panel.
- Click on Enable now.

5. Click Add to create object type.

6. Set Object Type Name to ScanKit, and click Next.
7. Click on Add fields , add the fields and click Next.

8. Click Add Index, set Index Name to Name and Indexed Field to Name, and click Next.
9. Add permissions as follows and click Next. (For Everyone , upsert and delete access is not allowed).
10. Click Ok. Object types are created and displayed in th object type list.
11. Click Export. Select JAVA as export file name. Enter the package name of JAVA file. package name can contain Letters A–Z or a–z, Digits 0–9, Special characters _ and . .

Cloud DB Zone
- Log in to AppGallery Connect and select My apps.
- Select Application for which you want to create object types.
- Click on Develop. Select build > Cloud DB from left panel.
- Click Cloud DB Zones tab.
- Click Add to go to the Cloud DB zone creation page.
- Enter QuickStartDemo in the Cloud DB Zone Name text box.
Prerequisites
1. Developer has created an application on App Gallery Connect. Follow Creating an App.
2. Developer has integrated Huawei Account kit. Follow this tutorial to integrate account kit.
3. The AppGallery Connect authentication service has been enabled. For details, see Authentication Service.
4. Developer has implemented Huawei Scan Kit. For details, see this tutorial.
Development
1. Integrate AGC SDK . Follow Integrating the AppGallery Connect SDK.
2. Add cloud DB SDK dependecy in app- level build.gradle.
dependencies {
implementation 'com.huawei.agconnect:agconnect-database:1.2.1.301'
}
3. Add compatibilty mode to JDK 1.8 in app- level build.gradle
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
4. Initialize AGConnectCloudDB in Application class.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initAGConnectCloudDB(this);
}
@Override
public void onTerminate() {
super.onTerminate();
}
/**
* Init AGConnectCloudDB in Application
*
* @param context application context
*/
public static void initAGConnectCloudDB(Context context) {
AGConnectCloudDB.initialize(context);
}
}
5. Get the instance of AGConnectCloudDB and create object types.
mCloudDB = AGConnectCloudDB.getInstance();
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
6. Create the Cloud DB zone configuration file and open the Cloud DB zone.
mConfig = new CloudDBZoneConfig("QuickStartDemo",
CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
try {
mCloudDBZone = mCloudDB.openCloudDBZone(mConfig, true);
} catch (AGConnectCloudDBException e) {
Log.w(TAG, "openCloudDBZone: " + e.getMessage());
}
7. Listening Data Changes on cloud
To listen data changes on cloud, we need to implement OnSnapshotlistner.
/**
* Monitor data change from database. Update results info list if data have changed
*/
private OnSnapshotListener<Scankit> mSnapshotListener = new OnSnapshotListener<Scankit>() {
@Override
public void onSnapshot(CloudDBZoneSnapshot<Scankit> cloudDBZoneSnapshot, AGConnectCloudDBException e) {
if (e != null) {
Log.w(TAG, "onSnapshot: " + e.getMessage());
return;
}
CloudDBZoneObjectList<Scankit> snapshotObjects = cloudDBZoneSnapshot.getSnapshotObjects();
infoList.clear();
try {
if (snapshotObjects != null) {
while (snapshotObjects.hasNext()) {
Scankit info = snapshotObjects.next();
infoList.add(info);
runOnUiThread(new Runnable() {
@Override
public void run() {
// refreshing recycler view
tvDisplay.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
mAdapter.notifyDataSetChanged();
}
});
}
if(snapshotObjects.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
tvDisplay.setVisibility(View.VISIBLE);
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
tvDisplay.setVisibility(View.VISIBLE);
}
});
}
} catch (Exception snapshotException) {
Log.e(TAG, "onSnapshot:(getObject) " + snapshotException.getMessage());
} finally {
cloudDBZoneSnapshot.release();
}
}
};
8. We need to register this data change listener.
/**
* Add mSnapshotListener to monitor data changes from storage
*/
public void addSubscription() {
if (mCloudDBZone == null) {
Log.e(TAG, "CloudDBZone is null, try re-open it");
return;
}
try {
CloudDBZoneQuery<EmployeeInfo> snapshotQuery = CloudDBZoneQuery.where(EmployeeInfo.class);
mRegister = mCloudDBZone.subscribeSnapshot(snapshotQuery,
CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY, mSnapshotListener);
} catch (AGConnectCloudDBException e) {
Log.e(TAG, "subscribeSnapshot: " + e.getMessage());
}
}
To Deregister a listner :
mRegister.remove();
9. Writing Data
Data can be entered either manually from App Gallery Connect or programmtically by using cloud DB sdk , if user has the permission. if same value in primary key is used, data will be modified.
private void addInfoToCloudDB(HmsScan hmsScan) {
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}
String format = HmsScanUtils.getScanFormat(hmsScan.getScanType());
String type = HmsScanUtils.getScanType(hmsScan) ;
String description = hmsScan.getOriginalValue();
long time= System.currentTimeMillis();
Scankit scankit = new Scankit();
scankit.setType(type);
scankit.setFormat(format);
scankit.setResult(description);
scankit.setTime(time);
scankit.setTypeCode(hmsScan.getScanTypeForm());
CloudDBZoneTask<Integer> upsertTask = mCloudDBZone.executeUpsert(scankit);
upsertTask.addOnSuccessListener(new OnSuccessListener<Integer>() {
@Override
public void onSuccess(Integer cloudDBZoneResult) {
Log.d(TAG, "insert " + cloudDBZoneResult + " records");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.w(TAG, "Insertion failed");
e.printStackTrace();
}
});
}
Note : You can query object data only when the Cloud DB zone is open. Otherwise, the query operation may fail.
10. Deleting data
Successful deletion of objects return count of deleted object.
public void deleteItemAsync(List<Scankit> list) {
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}
CloudDBZoneTask<Integer> deleteTask = mCloudDBZone.executeDelete(list);
deleteTask.addOnSuccessListener(new OnSuccessListener<Integer>() {
@Override
public void onSuccess(Integer integer) {
Log.d(TAG , "Item Deleted");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(HistoryActivity.this, "Successfully Deleted" , Toast.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG , e.getMessage());
}
});
}
Note : You can invoke this method to delete object data only when Cloud DB zone is opened. Otherwise, the deletion may fail.
Permissions Required:
// <!-- permission for WiFI post processing,not for scankit itself -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- camera permission -->
<uses-permission android:name="android.permission.CAMERA" /> <!-- read permission for Bitmap Mode -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- write permission for save QRCODE Bitmap,not for scankit itself -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" /><!-- vibrate permission to vibrate device for 300 ms once scan is successfull -->
<uses-permission android:name="android.permission.INTERNET" />
To Access Data Entries in AGC Console:
- Log in to AppGallery Connect and select My apps.
- Select Application for which you want to create object types.
- Click on Develop. Select build > Cloud DB from left panel.
- Select Data Entries.
- Enter ‘QuickStartDemo’ in Cloud DB Zone Name textbox and ‘Scankit’ in ObjectTypes textbox.
- Click Search.

7. Developer can view, modify and delete Entries.
8. To Add new entries, click on Add Button. Enter values and click Ok.

Screenshots


