cordova-plugin-networking-multipeer
This plugin provides Multipeer Connectivity for peer to peer networking between iOS devices, using infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks.
Installation
cordova plugin add cordova-plugin-networking-multipeer
Supported Platforms
- iOS
Namespace and API
All the functions and events described in this plugin reside in the networking.multipeer
namespace.
All the functions are asynchronous and have 2 callbacks as their last 2 parameters, the first being the success callback, and the second being the error callback.
All the events have the following methods:
Event
Adapter information
To obtain information of the local peer, use the getLocalPeerInfo
method:
var localPeer;networkingmultipeer;
Device discovery (browsing and advertising)
To begin discovery of nearby devices, use the startBrowsing
method.
Discovery can be resource intensive so you should call stopBrowsing
once the connection has been succesfully established.
You should call startBrowsing
whenever your app needs to discover nearby devices.
Information about each newly discovered device is received using the onFoundPeer
event.
If a previously discovered device is not reachable anymore, the onLostPeer
event will be fired.
Example:
var device_names = {};var last_peer_id;var { device_namespeerInfoid = peerInfoname; last_peer_id = peerInfoid;}; // Add listener to receive newly found devicesnetworkingmultipeeronFoundPeer; // Now begin the discovery process.var serviceType = 'xx-service'; // serviceType is a 1-15 character long string that can contain only ASCII lowercase letters, numbers, and hyphensnetworkingmultipeer; // If you want to stop discoveringnetworkingmultipeer;
To make the device discoverable, use the startAdvertising
function, that will make the device discoverable.
To stop making the device discoverable, for example once all the peers have been connected, use the stopAdvertising
function.
var serviceType = 'xx-service'; // serviceType is a 1-15 character long string that can contain only ASCII lowercase letters, numbers, and hyphensnetworkingmultipeer; // If you want to stop advertisingnetworkingmultipeer;
Connecting peers
With Multipeer Connectivity, the devices that are discovering (browsing) send an invitation to the devices that are discoverable (advertising), that in turn accept or decline the invitation.
In order to send an invitation, the device must be browsing, so be sure to send the invitation before calling the stopBrowsing
function.
Example:
networkingmultipeer;
On the other end, in order to accept or decline the invitation, the device must be advertising,
so be sure to have handled the invitation before calling the stopAdvertising
function.
Example:
networkingmultipeeronReceiveInvitation;
OPTIONAL: In order to implement automatic connection, the devices can be both browsing and advertising at the same time,
so, in order to have only one device sending the invitaton, the hash
attribute can be compared like this:
networkingmultipeeronFoundPeer;
A list of the currently connected peers can always be obtained by calling the getConnectedPeers
function.
networkingmultipeer;
Receiving and sending data
Receiving and sending data uses ArrayBuffer objects.
Sending data can be done either in reliable mode, or in unreliable mode.
To send data you have in arrayBuffer
use sendDataReliable
or sendDataUnreliable
:
// The first argument to sendDataReliable and sendDataUnreliable can be either a single// peer id, or an array containing all the peer ids that should receive the datanetworkingmultipeer
In contrast to the methods to send data, data is received in a single event (onReceiveData
).
networkingmultipeeronReceiveData;
Receiving connection/disconnection
To be notified of peer connection/disconnection, add a listener to the onChangeState
event.
networkingmultipeeronChangeState;
Disconnecting
To hang up the connection and disconnect use disconnect
.
networkingmultipeer;