A modified version of this npm package
- Add this project as a submodule to your react-native project
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
[this submodule's path]
➜react-native-multipeer
and addRCTMultipeerConnectivity.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRCTMultipeerConnectivity.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Click
RCTMultipeerConnectivity.xcodeproj
in the project navigator and go theBuild Settings
tab. Make sure 'All' is toggled on (instead of 'Basic'). Look forHeader Search Paths
and make sure it contains both$(SRCROOT)/../react-native/React
and$(SRCROOT)/../../React
- mark both asrecursive
. - Add
reducers/MultiPeer.reducer.js
to your main project's reducer list, i.e. thecombineReducers
function - Add
middlewares/middlewares.js
to your main project's middleware list, i.e. theapplyMiddleware
function - Run your main project
You can perform the supported methods by dispatching actions in actions/MultiPeer.action.js
For more detailed usages, please take a look at the following components of MultiPeerTest Project: PeerList AdvertiseControl
Following redux pattern, there is a state object recording all the operational status of this submodule. The UI should be in sync with this state, i.e. connect
to the state and update according to it. The state object is of the following shape:
const multipeerState = {
selfName: 'User-default', // String, name of the device itself
peers: { // An object containing (id: peer) pairs
'xxxxxxxxxx': { // Instance of Peer class
id: 'xxxxxxxxxx', // id of the peer; given by the underlying MultipeerConnectivity Protocol
name: 'Foo', // name of the peer; grabbing from the peer's info
connected: false, // whether the peer is connected with this device
invited: false, // whether this device invited the peer
invitationId: '', // if this device is invited by the peer but not responses yet, this property will contain a value; otherwise it will be an empty string
},
},
isBrowsing: false, // A boolean flag recording if this device is browsing
isAdvertising: false, // A boolean flag recording if this device is advertising
};
To update the state above and further refresh UI, all you need is to dispatch
actions via following action-creators, which are defined in action/MultiPeer.action.js
.
Initialize the underlying MultipeerConnectivity agent. This will be called programmatically when the app starts. (see classes/MultipeerConnectionInit.js
)
Browse for nearby peers that are advertising. When peers are found, they will be inserted into peers
in the state
object.
The cancellation of browse()
.
Allow discovery of yourself as a peer in the neighborhood. info
is an object containing data which will be passed to other peers when they find you; it should at least contain 'name'
property, which is your selfName
. When you are advertising, you will be found by nearby devices that have already called browse()
action creator. If they find you and send an invitation to you, they will be inserted into peers
in your state
object, with an invitationId
.
The cancellation of advertise()
. Deny discovery of yourself as a peer. If you call this, all the peers that have ever invite
you will be lost from peers
in your state
, no matter you two are connected or not.
Invite a peer into your session. You can only call this when you find someone via browse
and you two are not connected yet. myInfo
should at least contain 'name'
property, which is your selfName
. callback
will be executed after the invitation is sent.
Response an invitation from sender
, which should be the sender's name
. You can only call this when the sender's invitationId
in your state
is not empty, i.e., the sender has sent an invitation to you. accept
is a boolean. If accept === true
, You two will become connected. callback
will be executed after the response is sent.
Request peer's info. This is called programmatically when peer's are connected; since they probably don't have each other's info. (see classes/MultipeerConnectionInit.js
)
Return self's info when requested by peers. This is called programmatically when the peer send info-request to you. (see middlewares/middlewares.js
)
Send data
, which is a json object, to recipients
, which is an array of Peer
or peerId. callback
will be executed after the data are sent.
Send data
, which is a json object, to all of your connected peers. callback
will be executed after the data are sent.
This is not implemented yet. Create data streaming to a peer.
Disconnect with all of peers. callback
will be executed when all peers are disconnected.