2019-08-11 15:41:40 +00:00
|
|
|
import 'dart:io';
|
2019-05-11 10:31:33 +00:00
|
|
|
import 'dart:math';
|
2019-05-11 19:13:34 +00:00
|
|
|
import 'dart:typed_data';
|
2019-05-11 10:31:33 +00:00
|
|
|
|
2019-05-10 06:54:05 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2019-08-11 15:41:40 +00:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2020-08-15 18:51:27 +00:00
|
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
2019-05-10 06:54:05 +00:00
|
|
|
|
|
|
|
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(
|
2019-08-16 05:04:57 +00:00
|
|
|
title: const Text('Nearby Connections example app'),
|
2019-05-10 06:54:05 +00:00
|
|
|
),
|
2019-05-10 12:54:38 +00:00
|
|
|
body: Body(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Body extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MyBodyState createState() => _MyBodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyBodyState extends State<Body> {
|
2019-08-15 20:20:51 +00:00
|
|
|
final String userName = Random().nextInt(10000).toString();
|
2019-05-11 10:31:33 +00:00
|
|
|
final Strategy strategy = Strategy.P2P_STAR;
|
2021-01-26 12:47:47 +00:00
|
|
|
Map<String, ConnectionInfo> endpointMap = Map();
|
2019-08-16 05:04:57 +00:00
|
|
|
|
|
|
|
File tempFile; //reference to the file currently being transferred
|
|
|
|
Map<int, String> map =
|
2021-01-27 09:40:00 +00:00
|
|
|
Map(); //store filename mapped to corresponding payloadId
|
2019-05-11 10:31:33 +00:00
|
|
|
|
2019-05-10 12:54:38 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Center(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
2019-08-16 05:04:57 +00:00
|
|
|
Text(
|
|
|
|
"Permissions",
|
|
|
|
),
|
2019-08-15 20:20:51 +00:00
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("checkLocationPermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().checkLocationPermission()) {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2019-08-15 20:20:51 +00:00
|
|
|
content: Text("Location permissions granted :)")));
|
|
|
|
} else {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2019-08-16 05:04:57 +00:00
|
|
|
content:
|
2021-01-27 09:40:00 +00:00
|
|
|
Text("Location permissions not granted :(")));
|
2019-08-15 20:20:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("askLocationPermission"),
|
2020-08-15 18:51:27 +00:00
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().askLocationPermission()) {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2020-08-15 18:51:27 +00:00
|
|
|
content: Text("Location Permission granted :)")));
|
|
|
|
} else {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2020-08-15 18:51:27 +00:00
|
|
|
content:
|
2021-01-27 09:40:00 +00:00
|
|
|
Text("Location permissions not granted :(")));
|
2020-08-15 18:51:27 +00:00
|
|
|
}
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("checkExternalStoragePermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().checkExternalStoragePermission()) {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2019-08-15 20:20:51 +00:00
|
|
|
content:
|
2021-01-27 09:40:00 +00:00
|
|
|
Text("External Storage permissions granted :)")));
|
2019-08-15 20:20:51 +00:00
|
|
|
} else {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2019-08-15 20:20:51 +00:00
|
|
|
content: Text(
|
|
|
|
"External Storage permissions not granted :(")));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("askExternalStoragePermission"),
|
2020-04-14 16:09:10 +00:00
|
|
|
onPressed: () {
|
|
|
|
Nearby().askExternalStoragePermission();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Divider(),
|
|
|
|
Text("Location Enabled"),
|
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2020-04-14 16:09:10 +00:00
|
|
|
child: Text("checkLocationEnabled"),
|
2019-08-15 20:20:51 +00:00
|
|
|
onPressed: () async {
|
2020-04-14 16:09:10 +00:00
|
|
|
if (await Nearby().checkLocationEnabled()) {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2020-04-22 08:49:07 +00:00
|
|
|
SnackBar(content: Text("Location is ON :)")));
|
2020-04-14 16:09:10 +00:00
|
|
|
} else {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2020-04-22 08:49:07 +00:00
|
|
|
SnackBar(content: Text("Location is OFF :(")));
|
2020-04-14 16:09:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2020-04-14 16:09:10 +00:00
|
|
|
child: Text("enableLocationServices"),
|
2020-08-15 12:32:14 +00:00
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().enableLocationServices()) {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2020-08-15 18:51:27 +00:00
|
|
|
content: Text("Location Service Enabled :)")));
|
2020-08-15 12:32:14 +00:00
|
|
|
} else {
|
2021-01-27 09:40:00 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2020-08-15 18:51:27 +00:00
|
|
|
content:
|
2021-01-27 09:40:00 +00:00
|
|
|
Text("Enabling Location Service Failed :(")));
|
2020-08-15 12:32:14 +00:00
|
|
|
}
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Divider(),
|
|
|
|
Text("User Name: " + userName),
|
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Start Advertising"),
|
|
|
|
onPressed: () async {
|
|
|
|
try {
|
|
|
|
bool a = await Nearby().startAdvertising(
|
|
|
|
userName,
|
|
|
|
strategy,
|
2019-08-16 05:04:57 +00:00
|
|
|
onConnectionInitiated: onConnectionInit,
|
2019-08-15 20:20:51 +00:00
|
|
|
onConnectionResult: (id, status) {
|
|
|
|
showSnackbar(status);
|
|
|
|
},
|
|
|
|
onDisconnected: (id) {
|
2021-01-27 09:40:00 +00:00
|
|
|
showSnackbar(
|
|
|
|
"Disconnected: ${endpointMap[id].endpointName}, id $id");
|
2021-01-26 12:47:47 +00:00
|
|
|
setState(() {
|
|
|
|
endpointMap.remove(id);
|
|
|
|
});
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
);
|
2020-04-14 16:09:10 +00:00
|
|
|
showSnackbar("ADVERTISING: " + a.toString());
|
2019-08-15 20:20:51 +00:00
|
|
|
} catch (exception) {
|
|
|
|
showSnackbar(exception);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Stop Advertising"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAdvertising();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Start Discovery"),
|
|
|
|
onPressed: () async {
|
|
|
|
try {
|
|
|
|
bool a = await Nearby().startDiscovery(
|
|
|
|
userName,
|
|
|
|
strategy,
|
|
|
|
onEndpointFound: (id, name, serviceId) {
|
2019-08-16 05:04:57 +00:00
|
|
|
// show sheet automatically to request connection
|
2019-08-15 20:20:51 +00:00
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (builder) {
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Text("id: " + id),
|
|
|
|
Text("Name: " + name),
|
|
|
|
Text("ServiceId: " + serviceId),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Request Connection"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
Nearby().requestConnection(
|
|
|
|
userName,
|
|
|
|
id,
|
|
|
|
onConnectionInitiated: (id, info) {
|
2019-08-16 05:04:57 +00:00
|
|
|
onConnectionInit(id, info);
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
onConnectionResult: (id, status) {
|
|
|
|
showSnackbar(status);
|
|
|
|
},
|
|
|
|
onDisconnected: (id) {
|
2021-01-26 12:47:47 +00:00
|
|
|
setState(() {
|
|
|
|
endpointMap.remove(id);
|
|
|
|
});
|
2021-01-27 09:40:00 +00:00
|
|
|
showSnackbar(
|
|
|
|
"Disconnected from: ${endpointMap[id].endpointName}, id $id");
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onEndpointLost: (id) {
|
2021-01-27 09:40:00 +00:00
|
|
|
showSnackbar(
|
|
|
|
"Lost discovered Endpoint: ${endpointMap[id].endpointName}, id $id");
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
);
|
2019-08-16 05:04:57 +00:00
|
|
|
showSnackbar("DISCOVERING: " + a.toString());
|
2019-08-15 20:20:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
showSnackbar(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Stop Discovery"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopDiscovery();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-01-26 12:47:47 +00:00
|
|
|
Text("Number of connected devices: ${endpointMap.length}"),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Stop All Endpoints"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAllEndpoints();
|
2021-01-26 12:47:47 +00:00
|
|
|
setState(() {
|
|
|
|
endpointMap.clear();
|
|
|
|
});
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
Divider(),
|
2019-08-16 05:04:57 +00:00
|
|
|
Text(
|
|
|
|
"Sending Data",
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Send Random Bytes Payload"),
|
|
|
|
onPressed: () async {
|
2021-01-26 12:47:47 +00:00
|
|
|
endpointMap.forEach((key, value) {
|
|
|
|
String a = Random().nextInt(100).toString();
|
|
|
|
|
|
|
|
showSnackbar("Sending $a to ${value.endpointName}, id: $key");
|
2021-01-27 09:40:00 +00:00
|
|
|
Nearby()
|
|
|
|
.sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
|
2021-01-26 12:47:47 +00:00
|
|
|
});
|
2019-08-15 20:20:51 +00:00
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-08-15 20:20:51 +00:00
|
|
|
child: Text("Send File Payload"),
|
|
|
|
onPressed: () async {
|
2021-01-27 09:40:00 +00:00
|
|
|
PickedFile file =
|
|
|
|
await ImagePicker().getImage(source: ImageSource.gallery);
|
2019-08-11 15:41:40 +00:00
|
|
|
|
2019-08-15 20:20:51 +00:00
|
|
|
if (file == null) return;
|
2019-08-11 15:41:40 +00:00
|
|
|
|
2021-01-27 09:40:00 +00:00
|
|
|
for (MapEntry<String, ConnectionInfo> m
|
|
|
|
in endpointMap.entries) {
|
|
|
|
int payloadId =
|
|
|
|
await Nearby().sendFilePayload(m.key, file.path);
|
2021-01-26 12:47:47 +00:00
|
|
|
showSnackbar("Sending file to ${m.key}");
|
|
|
|
Nearby().sendBytesPayload(
|
|
|
|
m.key,
|
|
|
|
Uint8List.fromList(
|
|
|
|
"$payloadId:${file.path.split('/').last}".codeUnits));
|
|
|
|
}
|
2019-08-15 20:20:51 +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) {
|
2021-01-26 12:47:47 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
2019-05-11 10:31:33 +00:00
|
|
|
content: Text(a.toString()),
|
|
|
|
));
|
|
|
|
}
|
2019-05-11 19:37:58 +00:00
|
|
|
|
2019-08-16 05:04:57 +00:00
|
|
|
/// Called upon Connection request (on both devices)
|
|
|
|
/// Both need to accept connection to start sending/receiving
|
|
|
|
void onConnectionInit(String id, ConnectionInfo info) {
|
2019-05-11 19:37:58 +00:00
|
|
|
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()),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-05-11 19:37:58 +00:00
|
|
|
child: Text("Accept Connection"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
2021-01-26 12:47:47 +00:00
|
|
|
setState(() {
|
|
|
|
endpointMap[id] = info;
|
|
|
|
});
|
2019-05-11 19:37:58 +00:00
|
|
|
Nearby().acceptConnection(
|
|
|
|
id,
|
2019-08-15 19:30:35 +00:00
|
|
|
onPayLoadRecieved: (endid, payload) async {
|
2019-08-11 15:41:40 +00:00
|
|
|
if (payload.type == PayloadType.BYTES) {
|
2019-08-15 19:30:35 +00:00
|
|
|
String str = String.fromCharCodes(payload.bytes);
|
|
|
|
showSnackbar(endid + ": " + str);
|
|
|
|
|
|
|
|
if (str.contains(':')) {
|
|
|
|
// used for file payload as file payload is mapped as
|
|
|
|
// payloadId:filename
|
|
|
|
int payloadId = int.parse(str.split(':')[0]);
|
|
|
|
String fileName = (str.split(':')[1]);
|
|
|
|
|
|
|
|
if (map.containsKey(payloadId)) {
|
|
|
|
if (await tempFile.exists()) {
|
|
|
|
tempFile.rename(
|
|
|
|
tempFile.parent.path + "/" + fileName);
|
|
|
|
} else {
|
2021-01-26 12:47:47 +00:00
|
|
|
showSnackbar("File doesn't exist");
|
2019-08-15 19:30:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//add to map if not already
|
|
|
|
map[payloadId] = fileName;
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 15:41:40 +00:00
|
|
|
} else if (payload.type == PayloadType.FILE) {
|
|
|
|
showSnackbar(endid + ": File transfer started");
|
2019-08-15 19:30:35 +00:00
|
|
|
tempFile = File(payload.filePath);
|
2019-08-11 15:41:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
|
|
|
if (payloadTransferUpdate.status ==
|
2021-01-27 09:40:00 +00:00
|
|
|
PayloadStatus.IN_PROGRESS) {
|
2019-08-11 15:41:40 +00:00
|
|
|
print(payloadTransferUpdate.bytesTransferred);
|
|
|
|
} else if (payloadTransferUpdate.status ==
|
|
|
|
PayloadStatus.FAILURE) {
|
|
|
|
print("failed");
|
|
|
|
showSnackbar(endid + ": FAILED to transfer file");
|
|
|
|
} else if (payloadTransferUpdate.status ==
|
|
|
|
PayloadStatus.SUCCESS) {
|
2019-08-15 20:20:51 +00:00
|
|
|
showSnackbar(
|
2021-01-26 12:47:47 +00:00
|
|
|
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
2019-08-16 05:04:57 +00:00
|
|
|
|
2019-08-15 19:30:35 +00:00
|
|
|
if (map.containsKey(payloadTransferUpdate.id)) {
|
|
|
|
//rename the file now
|
|
|
|
String name = map[payloadTransferUpdate.id];
|
|
|
|
tempFile.rename(tempFile.parent.path + "/" + name);
|
|
|
|
} else {
|
|
|
|
//bytes not received till yet
|
|
|
|
map[payloadTransferUpdate.id] = "";
|
|
|
|
}
|
2019-08-11 15:41:40 +00:00
|
|
|
}
|
2019-05-11 19:37:58 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2021-02-12 16:32:52 +00:00
|
|
|
ElevatedButton(
|
2019-05-11 19:37:58 +00:00
|
|
|
child: Text("Reject Connection"),
|
2019-05-11 20:00:07 +00:00
|
|
|
onPressed: () async {
|
2019-05-11 19:37:58 +00:00
|
|
|
Navigator.pop(context);
|
2019-05-11 20:00:07 +00:00
|
|
|
try {
|
|
|
|
await Nearby().rejectConnection(id);
|
|
|
|
} catch (e) {
|
|
|
|
showSnackbar(e);
|
|
|
|
}
|
2019-05-11 19:37:58 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-05-11 10:31:33 +00:00
|
|
|
}
|