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 的远程端点。
|
Stop()
|
void
断开与所有远程端点的连接;停止正在进行的任何通告或发现。
|
StopAdvertising()
|
void
停止通告本地端点。
|
StopDiscovery(const std::string & service_id)
|
void
停止查找先前指定的服务 ID 的远程端点。
|
类 |
|
---|---|
gpg:: |
Builder 类,用于构建 NearbyConnections 对象。 |
公共函数
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
。
断开连接
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
可以保留自定义消息,以便随连接请求一起发送。或者,您的应用也可以传递空字节矢量(而不是载荷)。此函数调用指定的回调函数来响应请求。如果操作成功,则会显示“连接已接受”或“连接已拒绝”响应。否则,会生成一条失败消息。在连接接受的情况下,应用可向远程端点发送消息,而应用会在收到消息或与远程端点断开连接时调用指定的监听器。
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
;此回调将在成功时接收端点信息或在失败时收到错误代码。当远程端点请求与应用端点的连接时,此函数会调用 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()
断开与所有远程端点的连接;停止正在进行的任何通告或发现。
清除内部状态。
StopAdvertising
void StopAdvertising()
停止通告本地端点。
这样做不会导致现有连接断开。
StopDiscovery
void StopDiscovery( const std::string & service_id )
停止查找先前指定的服务 ID 的远程端点。
~NearbyConnections
~NearbyConnections()