mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
completed example app except sendPayload
This commit is contained in:
parent
fc6f7a55c6
commit
89a9403312
@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
@ -31,6 +33,9 @@ class Body extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyBodyState extends State<Body> {
|
class _MyBodyState extends State<Body> {
|
||||||
|
final String userName = Random().nextInt(1000).toString();
|
||||||
|
final Strategy strategy = Strategy.P2P_STAR;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// TODO: implement build
|
// TODO: implement build
|
||||||
@ -55,19 +60,54 @@ class _MyBodyState extends State<Body> {
|
|||||||
await Nearby().askPermission();
|
await Nearby().askPermission();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
Text("UserName: " + userName),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Start Advertising"),
|
child: Text("Start Advertising"),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
try {
|
||||||
bool a = await Nearby().startAdvertising(
|
bool a = await Nearby().startAdvertising(
|
||||||
"pkmn",
|
userName,
|
||||||
STRATEGY.P2P_STAR,
|
strategy,
|
||||||
|
onConnectionInitiated: (id, info) {
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
builder: (builder) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
children: <Widget>[
|
||||||
|
Text("id: " + id),
|
||||||
|
Text("Token: " + info.authenticationToken),
|
||||||
|
Text("Name" + info.endpointName),
|
||||||
|
Text("Incoming: " +
|
||||||
|
info.isIncomingConnection.toString()),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Accept Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Nearby().acceptConnection(id);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Reject Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Nearby().rejectConnection(id);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
showSnackbar(id);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
Scaffold.of(context)
|
showSnackbar(a);
|
||||||
.showSnackBar(SnackBar(content: Text(a.toString())));
|
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
Scaffold.of(context).showSnackBar(
|
showSnackbar(exception);
|
||||||
SnackBar(content: Text(exception.toString())));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -77,9 +117,103 @@ class _MyBodyState extends State<Body> {
|
|||||||
await Nearby().stopAdvertising();
|
await Nearby().stopAdvertising();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Start Discovery"),
|
||||||
|
onPressed: () async {
|
||||||
|
await Nearby().startDiscovery(
|
||||||
|
userName,
|
||||||
|
strategy,
|
||||||
|
onEndpointFound: (id, name, serviceId) {
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
builder: (builder) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
children: <Widget>[
|
||||||
|
Text("id: " + id),
|
||||||
|
Text("Name: " + name),
|
||||||
|
Text("ServiceId: " + serviceId),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Request Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Nearby().requestConnection(
|
||||||
|
userName,
|
||||||
|
id,
|
||||||
|
onConnectionInitiated: (id, info) {
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
builder: (builder) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
children: <Widget>[
|
||||||
|
Text("id: " + id),
|
||||||
|
Text("Token: " +
|
||||||
|
info.authenticationToken),
|
||||||
|
Text("Name" + info.endpointName),
|
||||||
|
Text("Incoming: " +
|
||||||
|
info.isIncomingConnection
|
||||||
|
.toString()),
|
||||||
|
RaisedButton(
|
||||||
|
child:
|
||||||
|
Text("Accept Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Nearby().acceptConnection(id);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child:
|
||||||
|
Text("Reject Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Nearby().rejectConnection(id);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
showSnackbar(id);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onEndpointLost: (id) {
|
||||||
|
showSnackbar(id);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Stop Discovery"),
|
||||||
|
onPressed: () async {
|
||||||
|
await Nearby().stopDiscovery();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Stop All Endpoints"),
|
||||||
|
onPressed: () async {
|
||||||
|
await Nearby().stopAllEndpoints();
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
void showSnackbar(dynamic a) {
|
||||||
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
|
content: Text(a.toString()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:flutter/foundation.dart';
|
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(
|
typedef void OnConnctionInitiated(
|
||||||
@ -130,7 +130,7 @@ class Nearby {
|
|||||||
|
|
||||||
Future<bool> startAdvertising(
|
Future<bool> startAdvertising(
|
||||||
String userNickName,
|
String userNickName,
|
||||||
STRATEGY strategy, {
|
Strategy strategy, {
|
||||||
@required OnConnctionInitiated onConnectionInitiated,
|
@required OnConnctionInitiated onConnectionInitiated,
|
||||||
@required OnConnectionResult onConnectionResult,
|
@required OnConnectionResult onConnectionResult,
|
||||||
@required OnDisconnected onDisconnected,
|
@required OnDisconnected onDisconnected,
|
||||||
@ -153,9 +153,9 @@ class Nearby {
|
|||||||
|
|
||||||
Future<bool> startDiscovery(
|
Future<bool> startDiscovery(
|
||||||
String userNickName,
|
String userNickName,
|
||||||
STRATEGY strategy, {
|
Strategy strategy, {
|
||||||
OnEndpointFound onEndpointFound,
|
@required OnEndpointFound onEndpointFound,
|
||||||
OnEndpointLost onEndpointLost,
|
@required OnEndpointLost onEndpointLost,
|
||||||
}) async {
|
}) async {
|
||||||
assert(userNickName != null && strategy != null);
|
assert(userNickName != null && strategy != null);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user