-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference.md
Requests necessary permissions for Nearby Connections.
const response = await requestPermissionsAsync();
Returns: Promise<PermissionResponse>
Retrieves the current permission status.
const response = await getPermissionsAsync();
Returns: Promise<PermissionResponse>
Hook to request and check permissions status.
const [status, askPermission, getPermission] = usePermissions()
startAdvertisingName(endpointName: string, serviceId: string, advertisingOptions: AdvertisingOptions)
Starts advertising with a specified name.
startAdvertisingName("DeviceA", "com.example.service", advertisingOptions);
startAdvertisingInfo(endpointInfo: UInt8Array, serviceId: string, advertisingOptions: AdvertisingOptions)
Starts advertising with endpoint info as a byte array.
startAdvertisingInfo(new Uint8Array([1, 2, 3]), "com.example.service", advertisingOptions);
Stops advertising the device.
stopAdvertising();
Starts discovering nearby devices.
startDiscovering("com.example.service", discoveringOptions);
Stops discovering nearby devices.
stopDiscovering();
requestConnectionName(endpointName: string, endpointId: string, connectionOptions?: ConnectionOptions)
Requests a connection using an endpoint name.
requestConnectionName("DeviceA", "endpoint123", connectionOptions);
requestConnectionInfo(endpointInfo: UInt8Array, endpointId: string, connectionOptions?: ConnectionOptions)
Requests a connection using endpoint info.
requestConnectionInfo(new Uint8Array([1, 2, 3]), "endpoint123", connectionOptions);
Accepts an incoming connection request.
acceptConnection("endpoint123");
Rejects an incoming connection request.
rejectConnection("endpoint123");
Disconnects from a specific endpoint.
disconnectFromEndpoint("endpoint123");
Stops all active connections.
stopAllEndpoints();
Creates a new payload from a byte array.
const payloadId = newPayloadBytes(new Uint8Array([1, 2, 3]));
Returns: string
Creates a new payload from a file.
const payloadId = newPayloadFile("filePath", true, "file.txt", "documents");
Returns: string | null
Creates a new stream payload.
const payloadId = newPayloadStream(2048);
Returns: string | null
Sends a payload to a single endpoint.
sendPayloadToEndpoint("endpoint123", payloadId);
Sends a payload to multiple endpoints.
sendPayloadToEndpoints(["endpoint123", "endpoint456"], payloadId);
Retrieves details about a payload.
const info = getPayloadInfo(payloadId);
Returns: PayloadInfo
Gets a list of info of active payloads.
const payloadsInfo = getPayloadInfoList();
Returns: PayloadInfo[]
Gets a list of active payloads.
const payloads = getPayloadList();
Returns: string[]
Check if a payload exists.
const payloadExist = payloadExists(payloadId);
Returns: boolean
Set a specific property value to a payload (mostly on file payloads).
setPayloadProperty(payloadId, "fileName", "test.jpg");
Removes a payload by ID.
removePayload(payloadId);
Removes all stored payloads.
removePayloadAll();
Cancels a payload with a given ID.
cancelPayload(payloadId);
Writes a byte to a stream.
writeStreamByte(payloadId, 255);
writeStreamByteArray(payloadId: string, byteArray: UInt8Array, offset: number, length: number = byteArray.length)
Writes a byte array to a stream.
writeStreamByteArray(payloadId, [255, 123, 22], 0);
Reads a byte from a stream.
const byte = await readStreamByte(payloadId);
Returns: Promise<number>
Reads a byte array from a stream.
const bytes = await readStreamByteArray(payloadId, 0, 1024);
Returns: Promise<UInt8Array>
Gets the number of bytes available in the stream for a given payload ID.
const available = await availableStream(payloadId);
Returns: Promise<number>
Marks the current position in the stream for a given payload ID.
markStream(payloadId, 100);
Flushes a stream buffer.
flushStream(payloadId);
Checks if mark and reset are supported in the stream for a given payload ID.
if(isMarkSupportedStream(payloadId)) {
...
}
Resets the stream to the most recent mark for a given payload ID.
resetStream(payloadId);
Skips a number of bytes in a stream.
const skipped = await skipStream(payloadId, 10);
Returns: Promise<number>
Represents the quality of the available bandwidth.
enum BandwidthQuality {
UNKNOWN = 0,
LOW = 1,
MEDIUM = 2,
HIGH = 3,
}
Defines the type of connection.
enum ConnectionType {
BALANCED = 0,
DISRUPTIVE = 1,
NON_DISRUPTIVE = 2,
}
Defines the type of payload being transferred.
enum PayloadType {
BYTES = 1,
FILE = 2,
STREAM = 3,
}
Represents the status of a payload transfer.
enum PayloadTransferUpdateStatus {
SUCCESS = 1,
FAILURE = 2,
IN_PROGRESS = 3,
CANCELED = 4,
}
Properties associated with a payload.
enum PayloadProperty {
OFFSET = "OFFSET",
FILENAME = "FILENAME",
PARENTFOLDER = "PARENTFOLDER",
SENSITIVE = "SENSITIVE",
}
Defines the connection strategy.
type Strategy = "P2P_CLUSTER" | "P2P_POINT_TO_POINT" | "P2P_STAR";
Contains information about a payload.
type PayloadInfo = {
id: string;
type: PayloadType;
offset: number;
uri?: string;
size?: number;
bytes?: Uint8Array;
};
Options for managing a connection.
type ConnectionOptions = {
connectionType: ConnectionType;
lowPowerMode: boolean;
};
Options for advertising a device.
type AdvertisingOptions = ConnectionOptions & DiscoveringOptions;
Options for discovering nearby devices.
type DiscoveringOptions = {
strategy: Strategy;
};
Represents a generic endpoint event.
type EndpointEvent = {
endpointId: string;
};
Emitted when an endpoint is discovered.
type EndpointFound = EndpointEvent & {
endpointInfo: Uint8Array;
serviceId: string;
};
Represents the initial connection handshake.
type ConnectionInitiated = {
endpointId: string;
authenticationDigits: string;
authenticationStatus: number;
endpointInfo: Uint8Array;
endpointName: string;
rawAuthenticationToken: Uint8Array;
isIncomingConnection: boolean;
};
Contains the result of a connection attempt.
type ConnectionResult = {
endpointId: string;
status: string;
};
Event triggered when bandwidth quality changes.
type BandwidthChanged = {
endpointId: string;
quality: BandwidthQuality;
};
Represents a payload event.
type PayloadEvent = {
payloadId: string;
};
Contains information about an error.
type Exception = {
error: string;
stackTrace: string;
data: string;
};
Details about a received payload.
type PayloadReceivedInfo = null | {
endpointId: string;
payloadId: string;
type: PayloadType;
data: Uint8Array | string;
};
Represents an update on a payload transfer.
type PayloadTransferUpdate = {
payloadId: string;
status: PayloadTransferUpdateStatus;
bytesTransferred: number;
totalBytes: number;
};
Represents the sending of a payload.
type PayloadSend = {
endpointIds: string[] | string;
payloadId: string;
};
Import the default export from the module and register a listener.
import Nearby from 'expo-googlenearby-connection'
Nearby.addListener("EndpointFound", (event) => {
console.log("Endpoint found, Id: " + event.endpointId)
console.log("Endpoint found, service Id: " + event.serviceId)
});
Called when an endpoint is found.
EndpointFound(event: EndpointFound): void;
Called when an endpoint is lost.
EndpointLost(event: EndpointEvent): void;
Called when advertising starts.
AdvertisingStarted(): void;
Called when discovering starts.
DiscoveringStarted(): void;
Called when advertising stops.
AdvertisingStopped(): void;
Called when discovering stops.
DiscoveringStopped(): void;
Called when advertising fails.
AdvertisingFailed(event: Exception): void;
Called when discovering fails.
DiscoveringFailed(event: Exception): void;
Called when the bandwidth quality changes.
BandwidthChanged(event: BandwidthChanged): void;
Called when a connection is requested.
ConnectionRequested(event: EndpointEvent): void;
Called when a connection request fails.
ConnectionRequestFailed(event: Exception): void;
Called when a connection is accepted.
ConnectionAccepted(event: EndpointEvent): void;
Called when a connection acceptance fails.
ConnectionAcceptFailed(event: Exception): void;
Called when a connection is rejected.
ConnectionRejected(event: EndpointEvent): void;
Called when a connection rejection fails.
ConnectionRejectFailed(event: Exception): void;
Called when a connection is initiated.
ConnectionInitiated(event: ConnectionInitiated): void;
Called when a connection result is received.
ConnectionResult(event: ConnectionResult): void;
Called when disconnected from an endpoint.
DisconnectFromEndpoint(event: EndpointEvent): void;
Called when an endpoint is disconnected.
EndpointDisconnection(event: EndpointEvent): void;
Called when all endpoints are stopped.
StopAllEndpoints(): void;
Called when a payload is received.
PayloadReceived(event: PayloadReceivedInfo): void;
Called when there is an update on the payload transfer.
PayloadTransferUpdate(event: PayloadTransferUpdate): void;
Called when a payload is sent.
PayloadSend(event: PayloadSend): void;
Called when a payload send fails.
PayloadSendFailed(event: Exception): void;
Called when a payload is removed.
PayloadRemoved(event: PayloadEvent): void;
Called when all payloads are removed.
PayloadRemovedAll(): void;
Called when a payload removal fails.
PayloadRemoveFailed(event: Exception): void;
Called when a payload is canceled.
PayloadCanceled(event: PayloadEvent): void;
Called when a payload cancellation fails.
PayloadCancelFailed(event: Exception): void;
Called when a payload is not found.
PayloadNotFound(event: Exception): void;
Called when invalid arguments have been passed.
PayloadInvalidArguments(event: Exception): void;
Called when an IOException occurs.
PayloadIOException(event: Exception): void;
Called when a generic Exception occurs.
PayloadException(event: Exception): void;
Called when the location request is approved.
LocationRequestApproved(): void;
Called when the location request is denied.
LocationRequestDenied(): void;
Called when the location request status is unknown.
LocationRequestUnknown(): void;
Called when the location prompt is accepted.
LocationPromptAccepted(): void;
Called when the location prompt is rejected.
LocationPromptRejected(): void;
Called when an invalid strategy has been passed.
InvalidStrategy(event: Exception): void;
For usage examples, refer to the User Guide.
Copyright © 2025 Fabrizio Iacobucci, released under MIT License