android-components / mozilla.components.lib.nearby / NearbyConnection

NearbyConnection

class NearbyConnection : Observable<NearbyConnectionObserver> (source)

A class that can be run on two devices to allow them to connect. This supports sending a single message at a time in each direction. It contains internal synchronization and may be accessed from any thread.

Types

Name Summary
ConnectionState sealed class ConnectionState
The state of the connection. Changes in state are communicated to the client through NearbyConnectionObserver.onStateUpdated.

Constructors

Name Summary
<init> NearbyConnection(context: <ERROR CLASS>, name: String = Build.MODEL)
Another constructorNearbyConnection(connectionsClient: ConnectionsClient, name: String = Build.MODEL, delegate: ObserverRegistry<NearbyConnectionObserver> = ObserverRegistry())
Constructs a new connection, which will call NearbyConnectionObserver.onStateUpdated with an argument of type ConnectionState.Isolated. No further action will be taken unless other methods are called by the client.

Properties

Name Summary
connectionState var connectionState: ConnectionState

Functions

Name Summary
disconnect fun disconnect(): Unit
Breaks any connections to neighboring devices. This also stops advertising and discovering. The state will be updated to ConnectionState.Isolated. It is important to call this when the connection is no longer needed because of a leak in the GATT client.
register fun register(observer: NearbyConnectionObserver): Unit
fun register(observer: NearbyConnectionObserver, view: <ERROR CLASS>): Unit
fun register(observer: NearbyConnectionObserver, owner: LifecycleOwner, autoPause: Boolean): Unit
Registers an observer to get notified about changes.
sendMessage fun sendMessage(message: String): Long?
Sends a message to a connected device. If the current state is not ConnectionState.ReadyToSend, the message will not be sent.
startAdvertising fun startAdvertising(): Unit
Starts advertising this device. After calling this, the state will be updated to ConnectionState.Advertising or ConnectionState.Failure. If all goes well, eventually the state will be updated to ConnectionState.Authenticating. A client should call either startAdvertising or startDiscovering to make a connection, not both. To initiate a connection, one device must call startAdvertising and the other startDiscovering.
startDiscovering fun startDiscovering(): Unit
Starts trying to discover nearby advertising devices. After calling this, the state will be updated to ConnectionState.Discovering or ConnectionState.Failure. If all goes well, eventually the state will be updated to ConnectionState.Authenticating. A client should call either startAdvertising or startDiscovering to make a connection, not both. To initiate a connection, one device must call startAdvertising and the other startDiscovering.

Companion Object Properties

Name Summary
PERMISSIONS val PERMISSIONS: Array<String>
The permissions needed by NearbyConnection. It is the client’s responsibility to ensure that all are granted before constructing an instance of this class.

Extension Functions

Name Summary
loadResourceAsString fun Any.loadResourceAsString(path: String): String
Loads a file from the resources folder and returns its content as a string object.