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';
|
|
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
2019-08-11 15:41:40 +00:00
|
|
|
import 'package:image_picker/image_picker.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;
|
2019-08-16 05:04:57 +00:00
|
|
|
|
2019-08-15 19:30:35 +00:00
|
|
|
String cId = "0"; //currently connected device ID
|
2019-08-16 05:04:57 +00:00
|
|
|
File tempFile; //reference to the file currently being transferred
|
|
|
|
Map<int, String> map =
|
|
|
|
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>[
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("checkLocationPermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().checkLocationPermission()) {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text("Location permissions granted :)")));
|
|
|
|
} else {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
2019-08-16 05:04:57 +00:00
|
|
|
content:
|
|
|
|
Text("Location permissions not granted :(")));
|
2019-08-15 20:20:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("askLocationPermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().askLocationPermission();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("checkExternalStoragePermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
if (await Nearby().checkExternalStoragePermission()) {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content:
|
|
|
|
Text("External Storage permissions granted :)")));
|
|
|
|
} else {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
|
|
|
"External Storage permissions not granted :(")));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("askExternalStoragePermission"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().askExternalStoragePermission();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Divider(),
|
|
|
|
Text("User Name: " + userName),
|
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
|
|
|
RaisedButton(
|
|
|
|
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) {
|
|
|
|
showSnackbar("Disconnected: " + id);
|
|
|
|
},
|
|
|
|
);
|
2019-08-16 05:04:57 +00:00
|
|
|
showSnackbar("ADVERTISING: "+a.toString());
|
2019-08-15 20:20:51 +00:00
|
|
|
} catch (exception) {
|
|
|
|
showSnackbar(exception);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop Advertising"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAdvertising();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Wrap(
|
|
|
|
children: <Widget>[
|
|
|
|
RaisedButton(
|
|
|
|
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),
|
|
|
|
RaisedButton(
|
|
|
|
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) {
|
|
|
|
showSnackbar(id);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onEndpointLost: (id) {
|
2019-08-16 05:04:57 +00:00
|
|
|
showSnackbar("Lost Endpoint:" + 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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop Discovery"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopDiscovery();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Stop All Endpoints"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAllEndpoints();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Divider(),
|
2019-08-16 05:04:57 +00:00
|
|
|
Text(
|
|
|
|
"Sending Data",
|
|
|
|
),
|
2019-08-15 20:20:51 +00:00
|
|
|
RaisedButton(
|
|
|
|
child: Text("Send Random Bytes Payload"),
|
|
|
|
onPressed: () async {
|
|
|
|
String a = Random().nextInt(100).toString();
|
|
|
|
showSnackbar("Sending $a to $cId");
|
|
|
|
Nearby().sendBytesPayload(cId, Uint8List.fromList(a.codeUnits));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Send File Payload"),
|
|
|
|
onPressed: () async {
|
|
|
|
File file =
|
|
|
|
await ImagePicker.pickImage(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
|
|
|
|
2019-08-15 20:20:51 +00:00
|
|
|
int payloadId = await Nearby().sendFilePayload(cId, file.path);
|
|
|
|
showSnackbar("Sending file to $cId");
|
|
|
|
Nearby().sendBytesPayload(
|
|
|
|
cId,
|
|
|
|
Uint8List.fromList(
|
|
|
|
"$payloadId:${file.path.split('/').last}".codeUnits));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
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()),
|
|
|
|
));
|
|
|
|
}
|
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()),
|
|
|
|
RaisedButton(
|
|
|
|
child: Text("Accept Connection"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
cId = id;
|
|
|
|
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 {
|
|
|
|
showSnackbar("File doesnt exist");
|
|
|
|
}
|
|
|
|
} 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 ==
|
|
|
|
PayloadStatus.IN_PROGRRESS) {
|
|
|
|
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(
|
2019-08-11 15:41:40 +00:00
|
|
|
"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
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
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
|
|
|
}
|