mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
completed dart portion, except sendPayload
This commit is contained in:
parent
31c5e0c165
commit
fc6f7a55c6
@ -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<bool> startAdvertising(String userNickName, STRATEGY strategy,
|
||||
{void onConnctionInitiated(
|
||||
String endpointId, ConnectionInfo connectionInfo),
|
||||
void onConnectionResult(String endpointId, Status status),
|
||||
void onDisconnected(String endpointId)}) async {
|
||||
Future<bool> 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', <String, dynamic>{
|
||||
'userNickName': userNickName,
|
||||
'strategy': strategy.index
|
||||
@ -90,11 +152,11 @@ class Nearby {
|
||||
}
|
||||
|
||||
Future<bool> 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', <String, dynamic>{
|
||||
@ -106,6 +168,53 @@ class Nearby {
|
||||
Future<void> stopDiscovery() async {
|
||||
await _channel.invokeMethod('stopDiscovery');
|
||||
}
|
||||
|
||||
Future<void> stopAllEndpoints() async {
|
||||
await _channel.invokeMethod('stopAllEndpoints');
|
||||
}
|
||||
|
||||
Future<void> disconnectFromEndpoint(String endpointId) async {
|
||||
await _channel.invokeMethod(
|
||||
'disconnectFromEndpoint', <String, dynamic>{'endpointId': endpointId});
|
||||
}
|
||||
|
||||
Future<bool> 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',
|
||||
<String, dynamic>{
|
||||
'userNickName': userNickName,
|
||||
'endpointId': endpointId,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> acceptConnection(String endpointId) async {
|
||||
return await _channel.invokeMethod(
|
||||
'acceptConnection',
|
||||
<String, dynamic>{
|
||||
'endpointId': endpointId,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> rejectConnection(String endpointId) async {
|
||||
return await _channel.invokeMethod(
|
||||
'acceptConnection',
|
||||
<String, dynamic>{
|
||||
'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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user