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 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
enum STRATEGY { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
|
enum STRATEGY { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
|
||||||
enum Status { CONNECTED, REJECTED, ERROR }
|
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 {
|
class Nearby {
|
||||||
//for maintaining only 1 instance of this class
|
//for maintaining only 1 instance of this class
|
||||||
static Nearby _instance;
|
static Nearby _instance;
|
||||||
@ -32,15 +42,50 @@ class Nearby {
|
|||||||
String authenticationToken = args['authenticationToken'];
|
String authenticationToken = args['authenticationToken'];
|
||||||
bool isIncomingConnection = args['isIncomingConnection'];
|
bool isIncomingConnection = args['isIncomingConnection'];
|
||||||
|
|
||||||
|
_advertConnectionInitiated?.call(
|
||||||
|
endpointId,
|
||||||
|
ConnectionInfo(
|
||||||
|
endpointName, authenticationToken, isIncomingConnection));
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
case "ad.onConnectionResult":
|
case "ad.onConnectionResult":
|
||||||
String endpointId = args['endpointId'];
|
String endpointId = args['endpointId'];
|
||||||
Status statusCode = Status.values[args['statusCode']];
|
Status statusCode = Status.values[args['statusCode']];
|
||||||
|
|
||||||
|
_advertConnectionResult?.call(endpointId, statusCode);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
case "ad.onDisconnected":
|
case "ad.onDisconnected":
|
||||||
String endpointId = args['endpointId'];
|
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;
|
return null;
|
||||||
|
|
||||||
case "dis.onEndpointFound":
|
case "dis.onEndpointFound":
|
||||||
@ -48,10 +93,14 @@ class Nearby {
|
|||||||
String endpointName = args['endpointName'];
|
String endpointName = args['endpointName'];
|
||||||
String serviceId = args['serviceId'];
|
String serviceId = args['serviceId'];
|
||||||
|
|
||||||
|
_onEndpointFound?.call(endpointId, endpointName, serviceId);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
case "dis.onEndpointLost":
|
case "dis.onEndpointLost":
|
||||||
String endpointId = args['endpointId'];
|
String endpointId = args['endpointId'];
|
||||||
|
|
||||||
|
_onEndpointLost?.call(endpointId);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
return null;
|
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 =
|
static const MethodChannel _channel =
|
||||||
const MethodChannel('nearby_connections');
|
const MethodChannel('nearby_connections');
|
||||||
|
|
||||||
@ -72,13 +128,19 @@ class Nearby {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> startAdvertising(String userNickName, STRATEGY strategy,
|
Future<bool> startAdvertising(
|
||||||
{void onConnctionInitiated(
|
String userNickName,
|
||||||
String endpointId, ConnectionInfo connectionInfo),
|
STRATEGY strategy, {
|
||||||
void onConnectionResult(String endpointId, Status status),
|
@required OnConnctionInitiated onConnectionInitiated,
|
||||||
void onDisconnected(String endpointId)}) async {
|
@required OnConnectionResult onConnectionResult,
|
||||||
|
@required OnDisconnected onDisconnected,
|
||||||
|
}) async {
|
||||||
assert(userNickName != null && strategy != null);
|
assert(userNickName != null && strategy != null);
|
||||||
|
|
||||||
|
this._advertConnectionInitiated = onConnectionInitiated;
|
||||||
|
this._advertConnectionResult = onConnectionResult;
|
||||||
|
this._advertDisconnected = onDisconnected;
|
||||||
|
|
||||||
return await _channel.invokeMethod('startAdvertising', <String, dynamic>{
|
return await _channel.invokeMethod('startAdvertising', <String, dynamic>{
|
||||||
'userNickName': userNickName,
|
'userNickName': userNickName,
|
||||||
'strategy': strategy.index
|
'strategy': strategy.index
|
||||||
@ -91,10 +153,10 @@ class Nearby {
|
|||||||
|
|
||||||
Future<bool> startDiscovery(
|
Future<bool> startDiscovery(
|
||||||
String userNickName,
|
String userNickName,
|
||||||
STRATEGY strategy,
|
STRATEGY strategy, {
|
||||||
void onEndpointFound(
|
OnEndpointFound onEndpointFound,
|
||||||
String endpointId, String endpointName, String serviceId),
|
OnEndpointLost onEndpointLost,
|
||||||
void onEndpointLost(String endpointId)) async {
|
}) async {
|
||||||
assert(userNickName != null && strategy != null);
|
assert(userNickName != null && strategy != null);
|
||||||
|
|
||||||
return await _channel.invokeMethod('startDiscovery', <String, dynamic>{
|
return await _channel.invokeMethod('startDiscovery', <String, dynamic>{
|
||||||
@ -106,6 +168,53 @@ class Nearby {
|
|||||||
Future<void> stopDiscovery() async {
|
Future<void> stopDiscovery() async {
|
||||||
await _channel.invokeMethod('stopDiscovery');
|
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 {
|
abstract class ConnectionLifecycleCallback {
|
||||||
@ -121,9 +230,9 @@ abstract class EndpointDiscoveryCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ConnectionInfo {
|
class ConnectionInfo {
|
||||||
String endPointName, authenticationToken;
|
String endpointName, authenticationToken;
|
||||||
bool isIncomingConnection;
|
bool isIncomingConnection;
|
||||||
|
|
||||||
ConnectionInfo(
|
ConnectionInfo(
|
||||||
this.endPointName, this.authenticationToken, this.isIncomingConnection);
|
this.endpointName, this.authenticationToken, this.isIncomingConnection);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user