Nearby Cross
A flutter plugin that uses Google's Nearby Connections to connect devices across multiple operative systems
Getting Started
Add the dependency:
flutter pub get nearby_cross
And follow the steps to make it work in iOS
- You need to request the access to protected resources in the following link
- Run
pod install
inside theios
folder - Open XCode and install nearby using SPM and add all packages to the plugin nearby_cross
- Done!
Models
Connector
- Functions that are shared between discoverer and advertiser
Advertiser
- The advertiser has the functions to
advertise
andstopAdvertising
and all the Connector functions - To advertise you need to indicate the service id for example
com.example.nearbyCrossExample
used in the example app - Its basically the one starting the network
- You can choose between different strategies:
- Star
- Cluster
- Point To Point
final advertiser = Advertiser()
/* Future<void> advertise(
String serviceId, {
bool manualAcceptConnections = false,
NearbyStrategies strategy = NearbyStrategies.star,
})
*/
advertiser.startAdvertising('com.example.nearbyCrossExample', false, NearbyStrategies.star)
//
// ... send or receive connections
//
advertiser.stopAdvertising()
Discoverer
-
The discoverer that connects to the advertiser, its the one connecting to the advertiser
You can set the following callbacks:
Function(Device) callbackOnDeviceFound = (_) => {}; Function(Device) callbackOnDeviceLost = (_) => {};
That will run the function that you pass it through when those actions happen
ConnectionManager
- This class contains all callbacks generated for in between actions, device disconnected, new data received etc. You must register callback to be executed in every flow you are interested, specially when receiving a message.
Authenticated connections
This plugin also allows to stablish secure conections using ECDSA cryptographic algorithm to generate digital signatures. If you want to include this feature in your application, an authenticationManager
must be provided to the ConnectionsManager
instance when creating it for the first time, for example:
var experimentalAuthManager = ExperimentalAuthManager();
experimentalAuthManager.setIdentifier('test_experimental');
ConnectionsManager(authenticationManager: experimentalAuthManager);
This "Authentication Manager" must extend AuthenticationManager
abstract class:
abstract class AuthenticationManager {
/// SiginigManager instance.
/// It will be used to sign messages before sending it to the connected peer
late SigningManager signingManager;
/// Identifier for this device (eg: uuid)
late String identifier;
/// Initializer function for this class. It will be executed when ConnectionsManager
/// is initialized.
/// Use this to set-up your authentication flow, like setting up the signingManager instance.
void initialize();
/// Signs NearbyMessage using signingManager instance
void sign(NearbyMessage message) {
message.signMessage(signingManager);
}
/// Starts handshake protocol.
/// This will be executed every new device connection
void startHandshake(Device device);
/// Process a handshake message
/// This will be executed when a NearbyMessageType.handshake message is received
void processHandshake(Device device, NearbyMessage message);
/// Sets identifier
void setIdentifier(String newId);
}
Example for authentication flow
ExperimentalAuthManager
is an example that implements a possible authentication flow using ECDSA and Handshake protocol:
In experimental mode, public keys are being shared in handshake process during device connection. These public keys are being loaded before stablishing connection and sending messages, and it is later used to verify messages signatures. More complex flows can be implemented using this interface and implementing other types of trustworthy sources to store public keys.
Example
In the example folder there's a chat implemented utilizing the plugin which you can share messages and stop and start the connection choosing which strategy you want
More links
Libraries
- constants/app
- constants/nearby_constraints
- constants/nearby_strategies
- errors/device_info_too_large.error
- helpers/bytes_utils
- helpers/permission_manager
- helpers/platform_utils
- helpers/string_utils
- models/advertiser_model
- models/connections_manager_model
- models/connector_model
- models/device_model
- models/discoverer_model
- models/message_model
- models/peer_info_manager
- modules/authentication/authentication_manager
- modules/authentication/experimental/experimental_auth_manager
- modules/authentication/signing_manager
- nearby_cross
- nearby_cross_methods
- types/item_type