From fc6f7a55c6046557ea5ecde802aa939d4b46c1c4 Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Sat, 11 May 2019 15:16:11 +0530 Subject: [PATCH] completed dart portion, except sendPayload --- lib/nearby_connections.dart | 135 ++++++++++++++++++++++++++++++++---- 1 file changed, 122 insertions(+), 13 deletions(-) diff --git a/lib/nearby_connections.dart b/lib/nearby_connections.dart index 4ea9117..67d06e5 100644 --- a/lib/nearby_connections.dart +++ b/lib/nearby_connections.dart @@ -1,10 +1,20 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; enum STRATEGY { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT } enum Status { CONNECTED, REJECTED, ERROR } +typedef void OnConnctionInitiated( + String endpointId, ConnectionInfo connectionInfo); +typedef void OnConnectionResult(String endpointId, Status status); +typedef void OnDisconnected(String endpointId); + +typedef void OnEndpointFound( + String endpointId, String endpointName, String serviceId); +typedef void OnEndpointLost(String endpointId); + class Nearby { //for maintaining only 1 instance of this class static Nearby _instance; @@ -31,16 +41,51 @@ class Nearby { String endpointName = args['endpointName']; String authenticationToken = args['authenticationToken']; bool isIncomingConnection = args['isIncomingConnection']; - + + _advertConnectionInitiated?.call( + endpointId, + ConnectionInfo( + endpointName, authenticationToken, isIncomingConnection)); + return null; case "ad.onConnectionResult": String endpointId = args['endpointId']; Status statusCode = Status.values[args['statusCode']]; + _advertConnectionResult?.call(endpointId, statusCode); + return null; case "ad.onDisconnected": String endpointId = args['endpointId']; + _advertDisconnected?.call(endpointId); + + return null; + + case "dis.onConnectionInitiated": + String endpointId = args['endpointId']; + String endpointName = args['endpointName']; + String authenticationToken = args['authenticationToken']; + bool isIncomingConnection = args['isIncomingConnection']; + + _discoverConnectionInitiated?.call( + endpointId, + ConnectionInfo( + endpointName, authenticationToken, isIncomingConnection)); + + return null; + case "dis.onConnectionResult": + String endpointId = args['endpointId']; + Status statusCode = Status.values[args['statusCode']]; + + _discoverConnectionResult?.call(endpointId, statusCode); + + return null; + case "dis.onDisconnected": + String endpointId = args['endpointId']; + + _discoverDisconnected?.call(endpointId); + return null; case "dis.onEndpointFound": @@ -48,10 +93,14 @@ class Nearby { String endpointName = args['endpointName']; String serviceId = args['serviceId']; + _onEndpointFound?.call(endpointId, endpointName, serviceId); + return null; case "dis.onEndpointLost": String endpointId = args['endpointId']; + _onEndpointLost?.call(endpointId); + return null; default: return null; @@ -59,6 +108,13 @@ class Nearby { }); } + OnConnctionInitiated _advertConnectionInitiated, _discoverConnectionInitiated; + OnConnectionResult _advertConnectionResult, _discoverConnectionResult; + OnDisconnected _advertDisconnected, _discoverDisconnected; + + OnEndpointFound _onEndpointFound; + OnEndpointLost _onEndpointLost; + static const MethodChannel _channel = const MethodChannel('nearby_connections'); @@ -72,13 +128,19 @@ class Nearby { ); } - Future startAdvertising(String userNickName, STRATEGY strategy, - {void onConnctionInitiated( - String endpointId, ConnectionInfo connectionInfo), - void onConnectionResult(String endpointId, Status status), - void onDisconnected(String endpointId)}) async { + Future startAdvertising( + String userNickName, + STRATEGY strategy, { + @required OnConnctionInitiated onConnectionInitiated, + @required OnConnectionResult onConnectionResult, + @required OnDisconnected onDisconnected, + }) async { assert(userNickName != null && strategy != null); + this._advertConnectionInitiated = onConnectionInitiated; + this._advertConnectionResult = onConnectionResult; + this._advertDisconnected = onDisconnected; + return await _channel.invokeMethod('startAdvertising', { 'userNickName': userNickName, 'strategy': strategy.index @@ -90,11 +152,11 @@ class Nearby { } Future startDiscovery( - String userNickName, - STRATEGY strategy, - void onEndpointFound( - String endpointId, String endpointName, String serviceId), - void onEndpointLost(String endpointId)) async { + String userNickName, + STRATEGY strategy, { + OnEndpointFound onEndpointFound, + OnEndpointLost onEndpointLost, + }) async { assert(userNickName != null && strategy != null); return await _channel.invokeMethod('startDiscovery', { @@ -106,6 +168,53 @@ class Nearby { Future stopDiscovery() async { await _channel.invokeMethod('stopDiscovery'); } + + Future stopAllEndpoints() async { + await _channel.invokeMethod('stopAllEndpoints'); + } + + Future disconnectFromEndpoint(String endpointId) async { + await _channel.invokeMethod( + 'disconnectFromEndpoint', {'endpointId': endpointId}); + } + + Future requestConnection( + String userNickName, + String endpointId, { + @required OnConnctionInitiated onConnectionInitiated, + @required OnConnectionResult onConnectionResult, + @required OnDisconnected onDisconnected, + }) async { + this._discoverConnectionInitiated = onConnectionInitiated; + this._discoverConnectionResult = onConnectionResult; + this._discoverDisconnected = onDisconnected; + + return await _channel.invokeMethod( + 'requestConnection', + { + 'userNickName': userNickName, + 'endpointId': endpointId, + }, + ); + } + + Future acceptConnection(String endpointId) async { + return await _channel.invokeMethod( + 'acceptConnection', + { + 'endpointId': endpointId, + }, + ); + } + + Future rejectConnection(String endpointId) async { + return await _channel.invokeMethod( + 'acceptConnection', + { + 'endpointId': endpointId, + }, + ); + } } abstract class ConnectionLifecycleCallback { @@ -121,9 +230,9 @@ abstract class EndpointDiscoveryCallback { } class ConnectionInfo { - String endPointName, authenticationToken; + String endpointName, authenticationToken; bool isIncomingConnection; ConnectionInfo( - this.endPointName, this.authenticationToken, this.isIncomingConnection); + this.endpointName, this.authenticationToken, this.isIncomingConnection); }