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::NearbyConnections::Builder

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(),探索功能會無限期持續執行,直到應用程式呼叫 Stop()StopDiscovery()如果指定的服務 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()