[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Introduction to Nearby Connection:

Nearby Connection is one of the API of HUAWEI Nearby Service that allows applications to easily discover and connect to nearby devices, and exchange data with nearby devices without having Internet.

Example: We can chat to a friend and can share files such as photos and videos without connecting to the Internet.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200731120315.90281185383394730148995711367772:50510812014742:2800:47B1A11FBCDD6C647DDB5471B99E569B4BFF163DBF6E4C867C53D003861CDC71.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200731120315.90281185383394730148995711367772:50510812014742:2800:47B1A11FBCDD6C647DDB5471B99E569B4BFF163DBF6E4C867C53D003861CDC71.jpg[/IMG]​

Prerequisites:

Java JDK (1.8 or later)

Android (API level 21 or higher)

HMS Core (APK) 4.0.0.300 or later

Android Studio 3.X

Two Huawei phones (with USB cables) or Non Huawei Phone with updated version of HMS Core App for app running and debugging.

Integration:

1. Create a project in android studio and Huawei AGC.

2. Provide the SHA-256 Key in App Information Section.

3. Enable Huawei Nearby Service.

4. Download the agconnect-services.json from AGCand save into app directory.

5. In root build.gradle

Navigate to allprojects->repositories and buildscript->repositories and add the given line.

maven { url 'http://developer.huawei.com/repo/' }

In dependency add class path.

classpath 'com.huawei.agconnect:agcp:1.2.1.301'

6. In app build.gradle

Add compatibility mode to JDK 1.8

compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

Add Implementation

implementation 'com.huawei.hms:nearby:5.0.1.300'

Apply plugin

apply plugin: 'com.huawei.agconnect'

7. Permissions in Manifest

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Nearby Connection Process:

Nearby connection process can be divided into four phases.

1. Broadcast and scanning

2. Connection setup

3. Data transmission

4. Disconnection

Let us go to the details.

1) Broadcast and scanning

This is the initial phase, here the broadcaster starts broadcasting, and the discoverer starts scanning to discover the broadcaster.

We can classify the functionalities of the initial phase as given:

a) Starting Broadcasting:

The broadcaster calls startBroadcasting() to start broadcasting

public void doStartBroadcasting() {
BroadcastOption.Builder advBuilder = new BroadcastOption.Builder();
advBuilder.setPolicy(Policy.POLICY_STAR);
discoveryEngine.startBroadcasting(myNameStr, myServiceId, connectCallback, advBuilder.build());
}

b) Starting Scanning:

The discoverer calls startScan() to start scanning to discover nearby devices.

public void doStartScan() {
ScanOption.Builder discBuilder = new ScanOption.Builder();
discBuilder.setPolicy(Policy.POLICY_STAR);
discoveryEngine.startScan(myServiceId, scanEndpointCallback, discBuilder.build());
}

c) Notifies the scanning result:

The scanning listening callback class instance notifies the discoverer of the scanning result. For example, a device is discovered or a device is lost, onFound() and onLost() methods are used here for the purpose.

private ScanEndpointCallback scanEndpointCallback =
new ScanEndpointCallback() {
@Override
public void onFound(String endpointId, ScanEndpointInfo discoveryEndpointInfo) {
mEndpointId = endpointId;
discoveryEngine.requestConnect(myNameStr, mEndpointId, connectCallback);
}

@Override
public void onLost(String endpointId) {
Log.d(TAG, "Nearby Connection Demo app: Lost endpoint: " + endpointId);
}
};

d) Stopping Broadcasting:

To stop broadcasting, we can call stopBroadcasting() method. The broadcaster cannot receive a connection request from the discoverer afterward.

discoveryEngine.stopBroadcasting();

e) Stopping Scanning:

To stop scanning, we can call stopScan() method. Once you find the device you want to connect to, call stopScan() to stop scanning.

discoveryEngine.stopScan();

2) Connection setup

In this phase the discoverer initiates a connection and starts a symmetric authentication process, and the two endpoints independently accept or reject the connection request.

We can classify the functionalities of the second phase as given:

a) Initiating a Connection:

When nearby devices are found, the discoverer can call requestConnect() to initiate connections request to the broadcaster.

discoveryEngine.requestConnect(myNameStr, mEndpointId, connectCallback);

b) Confirming the Connection:

After the discoverer requests to set up a connection with the broadcaster, the discoverer notifies the two parties of the connection setup by calling back the onEstablish() method of connectCallback. Both parties must accept the connection by calling acceptConnect() or reject the connection by calling rejectConnect(). The connection can be set up only when both parties accept the connection. If one or both of them choose to reject the connection, the connection fails. In either mode, the connection result is notified through the onResult() method.

private ConnectCallback connectCallback =
new ConnectCallback() {
@Override
public void onEstablish(String endpointId, ConnectInfo connectionInfo) {
transferEngine = Nearby.getTransferEngine(getApplicationContext());
discoveryEngine.acceptConnect(endpointId, dataCallback);
ToastUtil.showShortToastTop(getApplicationContext(), "Let's chat!");
sendBtn.setEnabled(true);
msgEt.setEnabled(true);
connectBtn.setEnabled(false);
connectTaskResult = StatusCode.STATUS_SUCCESS;
if (myNameStr.compareTo(friendNameStr) > 0) {
discoveryEngine.stopScan();
} else {
discoveryEngine.stopBroadcasting();
}
}

@Override
public void onResult(String endpointId, ConnectResult resolution) {
mEndpointId = endpointId;
}

@Override
public void onDisconnected(String endpointId) {
ToastUtil.showShortToastTop(getApplicationContext(), "Disconnect.");
connectTaskResult = StatusCode.STATUS_NOT_CONNECTED;
sendBtn.setEnabled(false);
connectBtn.setEnabled(true);
msgEt.setEnabled(false);
myNameEt.setEnabled(true);
friendNameEt.setEnabled(true);
}
};

3) Data transmission:

After the connection is set up, the two endpoints start data exchange in this phase.

We can classify the functionalities of the third phase as given:

a) Sending data:

After the connection is set up, both endpoints can call sendData() to send data to the remote endpoint.

private void sendMessage() {
msgStr = msgEt.getText().toString() ;
Data data = Data.fromBytes(msgStr.getBytes(Charset.defaultCharset()));
Log.d(TAG, "myEndpointId " + mEndpointId);
transferEngine.sendData(mEndpointId, data).addOnCompleteListener(task -> {
task.addOnSuccessListener(su -> {
Log.i(TAG, "sendData success. Message:" + msgStr);
}).addOnFailureListener(e -> {
Log.e(TAG, "sendData failed, Message:" + msgStr + "cause: " + e.getMessage());
});
});
MessageBean item = new MessageBean();
item.setMyName(myNameStr);
item.setFriendName(friendNameStr);
item.setMsg(msgStr.split(":")[0]);
item.setType(MessageBean.TYPE_SEND_TEXT);
msgList.add(item);
adapter.notifyDataSetChanged();
msgEt.setText("");
messageListView.setSelection(messageListView.getBottom());
}

b) Receiving data:

The data subscriber is notified by onReceived() that the data is received.

@Override
public void onReceived(String string, Data data) {
Log.d(TAG, "onPayloadReceived, payload.getType() = " + data.getType());
Log.d(TAG, "onPayloadReceived, string=" + string);
switch (data.getType()) {
case Data.Type.BYTES:
msgStr = new String(data.asBytes(), UTF_8);
Log.d(TAG, "onReceived success. msgSt: " + msgStr);
MessageBean item = new MessageBean();
item.setMyName(myNameStr);
item.setFriendName(friendNameStr);
item.setMsg(msgStr);
item.setType(MessageBean.TYPE_RECEIVE_TEXT);
msgList.add(item);
adapter.notifyDataSetChanged();
messageListView.setSelection(messageListView.getBottom());
break;
default:
Log.i(TAG, "the other Unknown data type.");
return;
}

c) Updating the Progress:

The onTransferUpdate() method provides the data sending or receiving progress update.

@Override
public void onTransferUpdate(String string, TransferStateUpdate update) {

long payloadId = update.getDataId();
Log.d(TAG, "onTransferUpdate.payloadId:" + payloadId);
if (update.getStatus() == TransferStateUpdate.Status.TRANSFER_STATE_SUCCESS) {
Log.d(TAG, "PayloadTransferUpdate.Status.SUCCESS");
Data payload = incomingFilePayloads.remove(payloadId);

if (payload != null) {

Log.d(TAG, "payload.getType() " + payload.getType());
completedFilePayloads.put(payloadId, payload);
}
} else {
Log.d(TAG, "PayloadTransferUpdate.Status=" + update.getStatus());
}
}

d) Cancelling Transmission:

To cancel the transmission during data receiving or sending, we can call the cancelDataTransfer() method of the TransferEngine class.

4) Disconnection

This is final phase, here we can call the disconnect() method of the DiscoveryEngine class to disconnect from the remote endpoint.

discoveryEngine.disconnectAll();

The remote endpoint is notified by onDisconnected() of the disconnection. Once this API is called, this endpoint cannot send or receive data.

@Override
public void onDisconnected(String endpointId) {
ToastUtil.showShortToastTop(getApplicationContext(), "Disconnect.");
connectTaskResult = StatusCode.STATUS_NOT_CONNECTED;
sendBtn.setEnabled(false);
connectBtn.setEnabled(true);
msgEt.setEnabled(false);
myNameEt.setEnabled(true);
friendNameEt.setEnabled(true);
}

Screenshots:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200731121251.28013505679231887029889343688311:50510812014742:2800:CA38F73357DF3EDC5C8EC6ECD53B87D7D37D4CAF0517CE47F2357D22434A0AAF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200731121251.28013505679231887029889343688311:50510812014742:2800:CA38F73357DF3EDC5C8EC6ECD53B87D7D37D4CAF0517CE47F2357D22434A0AAF.png[/IMG]

Reference:

https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/Nearby-Service-Nearby-Connection-Service-Nearby-Connection