2019-05-10 06:54:05 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2019-05-10 21:18:45 +00:00
|
|
|
enum STRATEGY { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
|
2019-05-11 08:34:12 +00:00
|
|
|
enum Status { CONNECTED, REJECTED, ERROR }
|
2019-05-10 21:18:45 +00:00
|
|
|
|
2019-05-10 12:54:38 +00:00
|
|
|
class Nearby {
|
|
|
|
//for maintaining only 1 instance of this class
|
2019-05-11 08:34:12 +00:00
|
|
|
static Nearby _instance;
|
|
|
|
|
|
|
|
factory Nearby() {
|
|
|
|
if (_instance == null) {
|
|
|
|
_instance = Nearby._();
|
|
|
|
}
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Nearby._() {
|
|
|
|
_channel.setMethodCallHandler((handler) {
|
|
|
|
Map<String, dynamic> args = handler.arguments;
|
|
|
|
print("=====================");
|
|
|
|
print(handler.method);
|
|
|
|
args.forEach((s, d) {
|
|
|
|
print(s + " : " + d.toString());
|
|
|
|
});
|
|
|
|
print("=====================");
|
|
|
|
switch (handler.method) {
|
|
|
|
case "ad.onConnectionInitiated":
|
|
|
|
String endpointId = args['endpointId'];
|
|
|
|
String endpointName = args['endpointName'];
|
|
|
|
String authenticationToken = args['authenticationToken'];
|
|
|
|
bool isIncomingConnection = args['isIncomingConnection'];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
case "ad.onConnectionResult":
|
|
|
|
String endpointId = args['endpointId'];
|
|
|
|
Status statusCode = Status.values[args['statusCode']];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
case "ad.onDisconnected":
|
|
|
|
String endpointId = args['endpointId'];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "dis.onEndpointFound":
|
|
|
|
String endpointId = args['endpointId'];
|
|
|
|
String endpointName = args['endpointName'];
|
|
|
|
String serviceId = args['serviceId'];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
case "dis.onEndpointLost":
|
|
|
|
String endpointId = args['endpointId'];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-05-10 12:54:38 +00:00
|
|
|
|
2019-05-10 06:54:05 +00:00
|
|
|
static const MethodChannel _channel =
|
|
|
|
const MethodChannel('nearby_connections');
|
|
|
|
|
2019-05-10 12:54:38 +00:00
|
|
|
Future<bool> checkPermissions() async => await _channel.invokeMethod(
|
|
|
|
'checkPermissions',
|
|
|
|
);
|
2019-05-10 21:18:45 +00:00
|
|
|
|
|
|
|
Future<void> askPermission() async {
|
|
|
|
await _channel.invokeMethod(
|
|
|
|
'askPermissions',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-11 08:34:12 +00:00
|
|
|
Future<bool> startAdvertising(String userNickName, STRATEGY strategy,
|
|
|
|
{void onConnctionInitiated(
|
|
|
|
String endpointId, ConnectionInfo connectionInfo),
|
|
|
|
void onConnectionResult(String endpointId, Status status),
|
|
|
|
void onDisconnected(String endpointId)}) async {
|
2019-05-10 21:18:45 +00:00
|
|
|
assert(userNickName != null && strategy != null);
|
|
|
|
|
|
|
|
return await _channel.invokeMethod('startAdvertising', <String, dynamic>{
|
|
|
|
'userNickName': userNickName,
|
|
|
|
'strategy': strategy.index
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> stopAdvertising() async {
|
|
|
|
await _channel.invokeMethod('stopAdvertising');
|
|
|
|
}
|
2019-05-11 08:34:12 +00:00
|
|
|
|
|
|
|
Future<bool> startDiscovery(
|
|
|
|
String userNickName,
|
|
|
|
STRATEGY strategy,
|
|
|
|
void onEndpointFound(
|
|
|
|
String endpointId, String endpointName, String serviceId),
|
|
|
|
void onEndpointLost(String endpointId)) async {
|
|
|
|
assert(userNickName != null && strategy != null);
|
|
|
|
|
|
|
|
return await _channel.invokeMethod('startDiscovery', <String, dynamic>{
|
|
|
|
'userNickName': userNickName,
|
|
|
|
'strategy': strategy.index
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> stopDiscovery() async {
|
|
|
|
await _channel.invokeMethod('stopDiscovery');
|
|
|
|
}
|
2019-05-10 21:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ConnectionLifecycleCallback {
|
2019-05-11 08:34:12 +00:00
|
|
|
void onConnectionInitiated(String endpointId, ConnectionInfo connectionInfo);
|
|
|
|
void onConnectionResult(String endpointId, Status status);
|
|
|
|
void onDisconnected(String endpointId);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class EndpointDiscoveryCallback {
|
|
|
|
void onEndpointFound(
|
|
|
|
String endpointId, String endpointName, String serviceId);
|
|
|
|
void onEndpointLost(String endpointId);
|
2019-05-10 21:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectionInfo {
|
|
|
|
String endPointName, authenticationToken;
|
|
|
|
bool isIncomingConnection;
|
|
|
|
|
|
|
|
ConnectionInfo(
|
|
|
|
this.endPointName, this.authenticationToken, this.isIncomingConnection);
|
2019-05-10 06:54:05 +00:00
|
|
|
}
|