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()