目录

MessageEngine

Class Info

public abstract class MessageEngine extends Object

API class for Nearby Message. It equips your app with the functions of publishing and subscribing to messages between nearby devices, as well as discovering beacons and subscribing to attachment messages of beacons.

NOTE:
  • To use this API, you do not need to associate your app with a HUAWEI ID. But you must register a project on Huawei Developers, enable Nearby Service, and apply for an API key.
  • Except APIs that can be used in the background (for example, get()), all other APIs can be called only from a foreground activity. You need to symmetrically call put or unput, get or unget.

Public Method Summary

Qualifier and Type

Method Name and Description

abstract void

handleIntent(Intent intent, MessageHandler handler)

Processes the received Intent object when the registered PendingIntent of the message subscription API is triggered by Nearby Service. Then Nearby Service will trigger the corresponding callback function in the handler based on the current Intent.

abstract Task<Void>

put(Message message)

Publishes a message to the cloud and broadcasts a sharing code for nearby devices to scan.

abstract Task<Void>

put(Message message, PutOption option)

Publishes a message to the cloud and broadcasts a sharing code for nearby devices to scan.

abstract Task<Void>

registerStatusCallback(StatusCallback statusCallback)

Registers a status callback function, which will notify your app of key events.

abstract Task<Void>

get(MessageHandler handler, GetOption option)

Obtains the message published by a nearby device.

abstract Task<Void>

get(MessageHandler handler)

Obtains the message published by a nearby device.

abstract Task<Void>

get(PendingIntent pendingIntent, GetOption option)

Obtains the message published by a nearby device. This API can be called in the background.

abstract Task<Void>

get(PendingIntent pendingIntent)

Obtains the message published by a nearby device. This API can be called in the background.

abstract Task<Void>

unput(Message message)

Cancels a published message. The message will be deleted from the cloud.

abstract Task<Void>

unregisterStatusCallback(StatusCallback statusCallback)

Cancels the status callback registered before.

abstract Task<Void>

unget(MessageHandler handler)

Cancels a registered message subscription task.

abstract Task<Void>

unget(PendingIntent pendingIntent)

Cancels a registered message subscription task. This API can be called in the background.

Public Methods

[h2]handleIntent

Method

public abstract void handleIntent(Intent intent, MessageHandler handler)

Processes the received Intent object when the registered PendingIntent of the message subscription API is triggered by Nearby Service. Then Nearby Service will trigger the corresponding callback function in the handler based on the current Intent.

CAUTION:

Currently, only the onFound(Message) and onLost(Message) methods are supported.

Parameters

Name

Description

intent

Intent content.

handler

Callback processing class used for message subscription.

[h2]put(Message message)

Method

public abstract Task<Void> put(Message message)

Publishes a message to the cloud and broadcasts a sharing code for nearby devices to scan.

Parameters

Name

Description

message

Message published to the cloud.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]put(Message message, PutOption option)

Method

public abstract Task<Void> put(Message message, PutOption option)

Publishes a message to the cloud and broadcasts a sharing code for nearby devices to scan.

The message can be subscribed only by the app under the same project (Obtaining the Project ID) registered on Huawei Developers.

Parameters

Name

Description

message

Message published to the cloud. This message can be subscribed by nearby devices.

option

PutOption used in the current message publishing process.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]registerStatusCallback

Method

public abstract Task<Void> registerStatusCallback(StatusCallback statusCallback)

Registers a status callback function, which will notify your app of key events.

Parameters

Name

Description

statusCallback

Status callback function.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]get(MessageHandler handler, GetOption option)

Method

public abstract Task<Void> get(MessageHandler handler, GetOption option)

Obtains the message published by a nearby device. The following types of messages can be subscribed: messages published by the nearby devices to the cloud, beacon attachment messages that have been configured to HUAWEI CLOUD services, and original beacon broadcast messages. You can use the MessagePicker parameter of GetOption to filter target messages. The message can be subscribed only by the app under the same project (Obtaining the Project ID) registered on Huawei Developers.

Parameters

Name

Description

handler

Callback processing class used for message subscription.

option

GetOption used in the current message subscription process.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

Sample code:

private void subscribe(String namespace, String type, int ttlSeconds, OnCompleteListener<Void> listener,
        GetCallback callback) {
        Policy policy = new Policy.Builder().setTtlSeconds(ttlSeconds).build();
        MessagePicker picker = new MessagePicker.Builder().includeNamespaceType(namespace, type).build();
        GetOption.Builder builder = new GetOption.Builder().setPolicy(policy).setPicker(picker);
        if (callback != null) {
            builder.setCallback(callback);
        }
        Nearby.getMessageEngine(getActivity()).get(mMessageHandler, builder.build()).addOnCompleteListener(listener);
    }

[h2]get(MessageHandler handler)

Method

public abstract Task<Void> get(MessageHandler handler)

Obtains the message published by a nearby device. The following types of messages can be subscribed: messages published by the nearby devices to the cloud, beacon attachment messages that have been configured to HUAWEI CLOUD services, and original beacon broadcast messages. In this case, the GetOption is DEFAULT.

Parameters

Name

Description

handler

Callback processing class used for message subscription.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]get(PendingIntent pendingIntent, GetOption option)

Method

public abstract Task<Void> get(PendingIntent pendingIntent, GetOption option)

Obtains the message published by a nearby device. This API can be called in the background. The following types of messages can be subscribed: beacon attachment messages that have been configured to HUAWEI CLOUD services, and original beacon broadcast messages. You can use the MessagePicker parameter of GetOption to filter target messages. Scanning is going on no matter whether your app runs in the background or foreground. The scanning and message subscription stop only after the app stop running.

Parameters

Name

Description

pendingIntent

When the target message is subscribed, Intent callback is triggered. The app needs to call handleIntent to process the Intent.

option

GetOption used in the current message subscription process.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]get(PendingIntent pendingIntent)

Method

public abstract Task<Void> get(PendingIntent pendingIntent)

Obtains the message published by a nearby device. This API can be called in the background. The following types of messages can be subscribed: beacon attachment messages that have been configured to HUAWEI CLOUD services, and original beacon broadcast messages. In this case, the GetOption is DEFAULT. Scanning is going on no matter whether your app runs in the background or foreground. The scanning and message subscription stop only after the app stop running.

Parameters

Name

Description

pendingIntent

When the target message is subscribed, Intent callback is triggered. The app needs to call handleIntent to process the Intent.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]unput

Method

public abstract Task<Void> unput(Message message)

Cancels a published message. The message will be deleted from the cloud.

Parameters

Name

Description

message

Published message.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

Sample code:

private void unpublish(String namespace, String type, OnCompleteListener<Void> listener) {
        Message message = new Message(JsonUtils.object2Json(mCardInfo).getBytes(Charset.forName("UTF-8")),
                type, namespace);
        Nearby.getMessageEngine(getActivity()).unput(message)
                .addOnCompleteListener(listener);
}

[h2]unregisterStatusCallback

Method

public abstract Task<Void> unregisterStatusCallback(StatusCallback statusCallback)

Cancels the status callback registered before.

Parameters

Name

Description

statusCallback

Status callback registered before.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]unget(MessageHandler handler)

Method

public abstract Task<Void> unget(MessageHandler handler)

Cancels a registered message subscription task.

Parameters

Name

Description

handler

Callback processing class registered by the current task when the target message is subscribed.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.

[h2]unget(PendingIntent pendingIntent)

Method

public abstract Task<Void> unget(PendingIntent pendingIntent)

Cancels a registered message subscription task. An app can register a callback to obtain the result details.

Parameters

Name

Description

pendingIntent

Current pending intent that is registered.

Returns

Type

Description

Task<Void>

Encapsulated API calling result in a task. An app can register a callback to obtain the result details.