2019-05-11 10:31:33 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2019-05-10 06:54:05 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
|
|
|
|
|
|
|
void main() => runApp(MyApp());
|
|
|
|
|
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MyAppState createState() => _MyAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Plugin example app'),
|
|
|
|
),
|
2019-05-10 12:54:38 +00:00
|
|
|
body: Body(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Body extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MyBodyState createState() => _MyBodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyBodyState extends State<Body> {
|
2019-05-11 10:31:33 +00:00
|
|
|
final String userName = Random().nextInt(1000).toString();
|
|
|
|
final Strategy strategy = Strategy.P2P_STAR;
|
|
|
|
|
2019-05-10 12:54:38 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
// TODO: implement build
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("checkPermission"),
|
|
|
|
onPressed: () async {
|
2019-05-11 08:34:12 +00:00
|
|
|
if (await Nearby().checkPermissions()) {
|
2019-05-10 12:54:38 +00:00
|
|
|
Scaffold.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text("yes")));
|
|
|
|
} else {
|
|
|
|
Scaffold.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text("No")));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
2019-05-11 10:47:02 +00:00
|
|
|
child: Text("askPermission(location)"),
|
2019-05-10 12:54:38 +00:00
|
|
|
onPressed: () async {
|
2019-05-11 08:34:12 +00:00
|
|
|
await Nearby().askPermission();
|
2019-05-10 21:18:45 +00:00
|
|
|
},
|
|
|
|
),
|
2019-05-11 10:31:33 +00:00
|
|
|
Text("UserName: " + userName),
|
2019-05-10 21:18:45 +00:00
|
|
|
RaisedButton(
|
|
|
|
child: Text("Start Advertising"),
|
|
|
|
onPressed: () async {
|
|
|
|
try {
|
2019-05-11 08:34:12 +00:00
|
|
|
bool a = await Nearby().startAdvertising(
|
2019-05-11 10:31:33 +00:00
|
|
|
userName,
|
|
|
|
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);
|
|
|
|
},
|
2019-05-11 08:34:12 +00:00
|
|
|
);
|
2019-05-11 10:31:33 +00:00
|
|
|
showSnackbar(a);
|
2019-05-10 21:18:45 +00:00
|
|
|
} catch (exception) {
|
2019-05-11 10:31:33 +00:00
|
|
|
showSnackbar(exception);
|
2019-05-10 21:18:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop Advertising"),
|
|
|
|
onPressed: () async {
|
2019-05-11 08:34:12 +00:00
|
|
|
await Nearby().stopAdvertising();
|
2019-05-10 12:54:38 +00:00
|
|
|
},
|
|
|
|
),
|
2019-05-11 10:31:33 +00:00
|
|
|
RaisedButton(
|
|
|
|
child: Text("Start Discovery"),
|
|
|
|
onPressed: () async {
|
2019-05-11 10:47:02 +00:00
|
|
|
try {
|
|
|
|
bool a = 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);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
showSnackbar(a);
|
|
|
|
} catch (e) {
|
|
|
|
showSnackbar(e);
|
|
|
|
}
|
2019-05-11 10:31:33 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop Discovery"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopDiscovery();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop All Endpoints"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAllEndpoints();
|
|
|
|
},
|
|
|
|
),
|
2019-05-10 12:54:38 +00:00
|
|
|
],
|
2019-05-10 06:54:05 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2019-05-11 08:34:12 +00:00
|
|
|
|
2019-05-11 10:31:33 +00:00
|
|
|
void showSnackbar(dynamic a) {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(a.toString()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|