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.

What You Will Learn

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.

Hardware Requirements

Software Requirements

Required Knowledge

Basic development capability of the Android apps.

  1. Procedure
  2. 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.

  3. SDK Integration
  4. 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.

  5. Permission Statement
    The permission must be declared in the AndroidManifest.xml file.
    AndroidManifest.xml
    <uses-permission android:name="com.huawei.permission.ONEHOP" />
  6. Note: You need to apply for permission before invoking the SDK API. Otherwise, the SDK API cannot be invoked.

  7. <intent-filter> Configuration
    You must configure <intent-filter> for the activity that needs to use the HUAWEI OneHop function. Otherwise, the target device cannot perform the activity and the service fails.
    <intent-filter> <action android:name="com.huawei.onehop.ACTION_START" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
  8. Event Listening API Registration
    The app registers the HUAWEI OneHop listening API with the system and starts listening the event. Implement the onOneHopReceived(String s) method of the IOneHopAppCalback.Stub class. After the method is registered on the app, it will be called back on both the mobile phone and the tablet when an NFC tapping event occurs.
    The following figure shows the code.
    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"); } } }
  9. Event Processing
    After the NFC event is triggered, the onOneHopReceived(String s) method is called back. In this case, the system determines the service is at the sending end (mobile phone) or receiving end (tablet) based on the input parameters and enters the corresponding logic processing procedure.
    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()); } }
  10. Data Synchronization
    After determining the app is in the sending or receiving process, perform the corresponding procedure.
    If the service is at the sending end, invoke the onOnehopSend(String packageName, JSONObjectpara) API to transmit data. The code is as follows:
    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"); } }
  11. 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); }
  12. Deregistration
    Cancel the registration after the activity exits. The code is as follows:
    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

Download source code

Code copied