HUAWEI OneHop is a core part of Huawei's device distributed technology. Based on the traditional NFC technology, HUAWEI OneHop integrates more short-distance technologies to enable an efficient interaction and collaboration between a mobile phone and its peripheral devices with a simple tap.
This section describes how to use HUAWEI OneHop to enable cross-device app synchronization.
In this CodeLab, you are supposed to create a demo project and integrate the HUAWEI OneHop SDK into the demo to sync the app status between a mobile phone and a tablet.
Basic development capability of the Android apps.
Currently, cross-device synchronization supports the integration and adaptation of the same app between the mobile phones and tablets.
Phone app: Functions as the service initiator and the data source to send the data to be synced.
Tablet app: Functions as the service successor to receive the data transferred from the mobile phone and sync the app status from the phone.
The following figure shows the overall process.
Save the SDK package to the libs directory of the app and enter the following command in the build.gradle file of the app:
In addition, you are advised to change the value of minSdkVersion in the build.gradle file to 28, as shown in the following figure.
Click the synchronization icon, as shown in the following figure.
After the synchronization, the following screen is displayed.
AndroidManifest.xml
<uses-permission android:name="com.huawei.permission.ONEHOP" />
Note: You need to apply for permission before invoking the SDK API. Otherwise, the SDK API cannot be invoked.
<intent-filter>
<action android:name="com.huawei.onehop.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
MainActivity.java
/**
* Thread to register onehop
*
* @since 2019-11-11
*/
private class RegisterThread extends Thread {
@Override
public void run() {
int retCode = HwOneHopSdk.getInstance().registerOneHop(PKG_NAME,
HwOneHopSdk.ONEHOP_DATA_TYPE_BUSINESS_CONTINUITY, new IOneHopAppCallback.Stub() {
@Override
public void onOneHopReceived(String s) throws RemoteException {
// Invoke the OneHop synchronization API OneHopData to notify the tapping event.
if (TextUtils.isEmpty(s)) {
Log.e(TAG, "onOneHopReceived got wrong para");
return;
}
try {
JSONObject jsonObject = new JSONObject(s);
boolean isEvent;
if (jsonObject.has(HwOneHopSdk.ONEHOP_RECEIVE_TYPE)) {
int type = jsonObject.getInt(HwOneHopSdk.ONEHOP_RECEIVE_TYPE);
isEvent = type == HwOneHopSdk.ONEHOP_RECEIVE_TYPE_EVENT ? true : false;
} else {
Log.e(TAG, "the para is false " + s);
return;
}
if (!isEvent) {
// pad: receive parameters
handleReceive(s);
} else {
// phone: send parameters
handleSend();
}
} catch (JSONException e) {
Log.e(TAG, "JSON parsed failed " + e.getMessage());
}
}
});
if (retCode == HwOneHopSdk.ONEHOP_ERR) {
Log.e(TAG, "register OneHop error");
}
}
}
MainActivity.java
@Override
public void onOneHopReceived(String s) throws RemoteException {
// Invoke the HUAWEI OneHop synchronization API OneHopData to notify the current NFC tapping event.
if (TextUtils.isEmpty(s)) {
Log.e(TAG, "onOneHopReceived got wrong para");
return;
}
try {
JSONObject jsonObject = new JSONObject(s);
boolean isEvent;
if (jsonObject.has(HwOneHopSdk.ONEHOP_RECEIVE_TYPE)) {
int type = jsonObject.getInt(HwOneHopSdk.ONEHOP_RECEIVE_TYPE);
isEvent = type == HwOneHopSdk.ONEHOP_RECEIVE_TYPE_EVENT ? true : false;
} else {
Log.e(TAG, "the para is false " + s);
return;
}
if (!isEvent) {
// pad: receive parameters
handleReceive(s);
} else {
// phone: send parameters
handleSend();
}
} catch (JSONException e) {
Log.e(TAG, "JSON parsed failed " + e.getMessage());
}
}
MainActivity.java
private void handleSend() {
Map<String, Object> map = new HashMap<>();
map.put(DATA_STR, "Hello World! " + (count++));
int retCode = HwOneHopSdk.getInstance().oneHopSend(getPackageName(),
new JSONObject(map));
if (retCode == HwOneHopSdk.ONEHOP_ERR) {
Log.e(TAG, "send failed");
}
}
The receiving end further processes the data.MainActivity.java
private void handleReceive(String param) {
Message message = new Message();
message.what = MSG_RECEIVE;
Bundle bundle = new Bundle();
bundle.putString(MSG_DATA, param);
message.setData(bundle);
mHandler.sendMessage(message);
}
MainActivity.java
@Override
protected void onDestroy() {
unregister();
super.onDestroy();
}
private void unregister() {
int retCode = HwOneHopSdk.getInstance().unregisterOneHop(getPackageName(),
HwOneHopSdk.ONEHOP_DATA_TYPE_BUSINESS_CONTINUITY);
if (retCode == HwOneHopSdk.ONEHOP_ERR) {
Log.d(TAG, "unregister failed");
}
}
In this case, you need to install the demo on both the mobile phone and the tablet, and open the demo on the phone. Enable NFC on the phone, tap the phone against the NFC tag on the tablet to establish connection between them. The demo will then be opened on the tablet and the transferred parameters will be displayed.
You have successfully completed this codelab and learned:
Setting up an app demo and integrating the HUAWEI OneHop SDK into it.
Performing the event listening, processing, and synchronization processes.
For more information about HUAWEI OneHop, click the following links:
HUAWEI OneHop Introduction