gpg::NearbyConnections

#include <nearby_connections.h>

用于在同一本地网络上的应用之间创建连接和进行通信的 API。

摘要

构造函数和析构函数

NearbyConnections()
~NearbyConnections()

公共函数

AcceptConnectionRequest(const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload, IMessageListener *listener)
void
接受连接请求。
AcceptConnectionRequest(const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload, MessageListenerHelper helper)
void
接受连接请求。
Disconnect(const std::string & remote_endpoint_id)
void
断开与指定 ID 的远程端点的连接。
RejectConnectionRequest(const std::string & remote_endpoint_id)
void
拒绝连接请求。
SendConnectionRequest(const std::string & name, const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload, ConnectionResponseCallback callback, IMessageListener *listener)
void
请求与远程端点建立连接。
SendConnectionRequest(const std::string & name, const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload, ConnectionResponseCallback callback, MessageListenerHelper helper)
void
请求连接到远程端点。
SendReliableMessage(const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload)
void
向具有指定 ID 的远程端点发送可靠消息。
SendReliableMessage(const std::vector< std::string > & remote_endpoint_ids, const std::vector< uint8_t > & payload)
void
向具有指定 ID 的远程端点发送可靠消息。
SendUnreliableMessage(const std::string & remote_endpoint_id, const std::vector< uint8_t > & payload)
void
向具有指定 ID 的远程端点发送不可靠消息。
SendUnreliableMessage(const std::vector< std::string > & remote_endpoint_ids, const std::vector< uint8_t > & payload)
void
向具有指定 ID 的远程端点发送不可靠消息。
StartAdvertising(const std::string & name, const std::vector< AppIdentifier > & app_identifiers, Duration duration, StartAdvertisingCallback start_advertising_callback, ConnectionRequestCallback request_callback)
void
开始为本地应用宣传端点。
StartDiscovery(const std::string & service_id, Duration duration, IEndpointDiscoveryListener *listener)
void
查找具有指定服务 ID 的远程端点,并在应用发现和失去端点时重复调用监听器。
StartDiscovery(const std::string & service_id, Duration duration, EndpointDiscoveryListenerHelper helper)
void
查找具有指定服务 ID 的远程端点。
停止()
void
断开与所有远程端点的连接;停止正在进行的任何宣传或发现。
停止宣传()
void
停止宣传本地端点。
StopDiscovery(const std::string & service_id)
void
停止查找先前指定的具有服务 ID 的远程端点。

gpg::NearbyConnections::Builder

用于构建 NearbyConnections 对象的 Builder 类。

公共函数

AcceptConnectionRequest

void AcceptConnectionRequest(
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload,
  IMessageListener *listener
)

接受连接请求。

随后,应用可以向指定端点发送消息并接收来自该端点的消息。应用可以使用 listener 执行此操作,直到与另一个端点断开连接为止。remote_endpoint_id 必须与请求连接的远程端点的 ID 匹配。ConnectionRequestCallback 提供该 ID。payload 可以包含随连接响应发送的消息。listener 指定一个监听器,以便在此连接发生事件时收到通知。

AcceptConnectionRequest

void AcceptConnectionRequest(
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload,
  MessageListenerHelper helper
)

接受连接请求。

此函数与 AcceptConnectionRequest 的唯一区别在于它使用 MessageListenerHelper,而不是 IMessageListener

Disconnect

void Disconnect(
  const std::string & remote_endpoint_id
)

断开与指定 ID 的远程端点的连接。

NearbyConnections

 NearbyConnections()

RejectConnectionRequest

void RejectConnectionRequest(
  const std::string & remote_endpoint_id
)

拒绝连接请求。

remote_endpoint_id 必须与请求连接的远程端点的 ID 匹配。ConnectionRequestCallback 提供该 ID。

SendConnectionRequest

void SendConnectionRequest(
  const std::string & name,
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload,
  ConnectionResponseCallback callback,
  IMessageListener *listener
)

请求与远程端点建立连接。

name 是应用可以向其他设备上的用户显示的名称,用于标识此端点。如果您指定空字符串,则使用设备名称。remote_endpoint_id 是此应用发送连接请求的远程端点的 ID。payload 可以包含随连接请求发送的自定义消息。或者,您的应用可以传递空字节向量而不是 payload。此函数响应请求调用指定的 callback。如果操作成功,它会生成“Connection Accepted”(连接已接受)或“Connection Rejected”(连接已拒绝)响应。否则,它会生成失败消息。如果连接被接受,应用可以向远程端点发送消息,并且在收到消息或与远程端点断开连接时,应用会调用指定的监听器。

SendConnectionRequest

void SendConnectionRequest(
  const std::string & name,
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload,
  ConnectionResponseCallback callback,
  MessageListenerHelper helper
)

请求连接到远程端点。

SendConnectionRequest 的唯一区别在于它使用 MessageListenerHelper,而不是 IMessageListener

SendReliableMessage

void SendReliableMessage(
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload
)

向具有指定 ID 的远程端点发送可靠消息。

SendReliableMessage

void SendReliableMessage(
  const std::vector< std::string > & remote_endpoint_ids,
  const std::vector< uint8_t > & payload
)

向具有指定 ID 的远程端点发送可靠消息。

SendUnreliableMessage

void SendUnreliableMessage(
  const std::string & remote_endpoint_id,
  const std::vector< uint8_t > & payload
)

向具有指定 ID 的远程端点发送不可靠消息。

SendUnreliableMessage

void SendUnreliableMessage(
  const std::vector< std::string > & remote_endpoint_ids,
  const std::vector< uint8_t > & payload
)

向具有指定 ID 的远程端点发送不可靠消息。

StartAdvertising

void StartAdvertising(
  const std::string & name,
  const std::vector< AppIdentifier > & app_identifiers,
  Duration duration,
  StartAdvertisingCallback start_advertising_callback,
  ConnectionRequestCallback request_callback
)

开始为本地应用宣传端点。

name 可以是应用向用户显示的名称,用于标识端点。如果您指定空字符串,则使用设备名称。如果指定,app_identifiers 会指定如何在不同平台上安装或启动此应用。duration 指定宣传将运行的时长(以毫秒为单位),除非应用在时长到期前调用 StopAdvertising()Stop()。如果 duration 的值等于 gpg::Duration::zero(),则宣传会无限期持续,直到应用调用 StopAdvertising()。此函数在宣传开始或失败时调用 start_advertising_callback;此 callback 在成功时接收端点信息,在失败时接收错误代码。当远程端点请求与应用端点建立连接时,此函数会调用 request_callback。此函数会持续宣传此端点的存在,直到应用调用 StopAdvertising 或时长到期。如果已有一个正在宣传的端点,则此调用会失败。

StartDiscovery

void StartDiscovery(
  const std::string & service_id,
  Duration duration,
  IEndpointDiscoveryListener *listener
)

查找具有指定服务 ID 的远程端点,并在应用发现和失去端点时重复调用监听器。

持续执行此操作,直到应用使用指定的服务 ID 调用 StopDiscovery 为止。service_id 应与应用通过 StartAdvertising 进行宣传时提供的值匹配。duration 指定发现功能运行的最大时长(以毫秒为单位)(如果应用调用 StopDiscovery(),则可能提前停止)。如果 Duration 的值等于 gpg::Duration::zero(),则发现会无限期持续,直到应用调用 StopDiscovery()Stop()。如果已注册监听器来查找指定服务 ID 的端点,则此调用会失败。

StartDiscovery

void StartDiscovery(
  const std::string & service_id,
  Duration duration,
  EndpointDiscoveryListenerHelper helper
)

查找具有指定服务 ID 的远程端点。

此函数与 StartDiscovery 的唯一区别在于它使用 EndpointDiscoveryListenerHelper,而不是 IEndpointDiscoveryListener

停止

void Stop()

断开与所有远程端点的连接;停止正在进行的任何宣传或发现。

清理内部状态。

停止宣传

void StopAdvertising()

停止宣传本地端点。

这样做并不会导致现有连接断开。

StopDiscovery

void StopDiscovery(
  const std::string & service_id
)

停止查找先前指定的具有服务 ID 的远程端点。

~NearbyConnections

 ~NearbyConnections()