Update example to demonstrate transfer to multiple connections (#26)

The example app now will send data to all the connected endpoints.
This commit is contained in:
afarre 2021-01-26 13:47:47 +01:00 committed by GitHub
parent f35a484334
commit c2adf90962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 63 deletions

View File

@ -35,8 +35,8 @@ class Body extends StatefulWidget {
class _MyBodyState extends State<Body> { class _MyBodyState extends State<Body> {
final String userName = Random().nextInt(10000).toString(); final String userName = Random().nextInt(10000).toString();
final Strategy strategy = Strategy.P2P_STAR; final Strategy strategy = Strategy.P2P_STAR;
Map<String, ConnectionInfo> endpointMap = Map();
String cId = "0"; //currently connected device ID
File tempFile; //reference to the file currently being transferred File tempFile; //reference to the file currently being transferred
Map<int, String> map = Map<int, String> map =
Map(); //store filename mapped to corresponding payloadId Map(); //store filename mapped to corresponding payloadId
@ -148,7 +148,10 @@ class _MyBodyState extends State<Body> {
showSnackbar(status); showSnackbar(status);
}, },
onDisconnected: (id) { onDisconnected: (id) {
showSnackbar("Disconnected: " + id); showSnackbar("Disconnected: ${endpointMap[id].endpointName}, id $id");
setState(() {
endpointMap.remove(id);
});
}, },
); );
showSnackbar("ADVERTISING: " + a.toString()); showSnackbar("ADVERTISING: " + a.toString());
@ -199,7 +202,10 @@ class _MyBodyState extends State<Body> {
showSnackbar(status); showSnackbar(status);
}, },
onDisconnected: (id) { onDisconnected: (id) {
showSnackbar(id); setState(() {
endpointMap.remove(id);
});
showSnackbar("Disconnected from: ${endpointMap[id].endpointName}, id $id");
}, },
); );
}, },
@ -211,7 +217,7 @@ class _MyBodyState extends State<Body> {
); );
}, },
onEndpointLost: (id) { onEndpointLost: (id) {
showSnackbar("Lost Endpoint:" + id); showSnackbar("Lost discovered Endpoint: ${endpointMap[id].endpointName}, id $id");
}, },
); );
showSnackbar("DISCOVERING: " + a.toString()); showSnackbar("DISCOVERING: " + a.toString());
@ -228,10 +234,14 @@ class _MyBodyState extends State<Body> {
), ),
], ],
), ),
Text("Number of connected devices: ${endpointMap.length}"),
RaisedButton( RaisedButton(
child: Text("Stop All Endpoints"), child: Text("Stop All Endpoints"),
onPressed: () async { onPressed: () async {
await Nearby().stopAllEndpoints(); await Nearby().stopAllEndpoints();
setState(() {
endpointMap.clear();
});
}, },
), ),
Divider(), Divider(),
@ -241,9 +251,12 @@ class _MyBodyState extends State<Body> {
RaisedButton( RaisedButton(
child: Text("Send Random Bytes Payload"), child: Text("Send Random Bytes Payload"),
onPressed: () async { onPressed: () async {
endpointMap.forEach((key, value) {
String a = Random().nextInt(100).toString(); String a = Random().nextInt(100).toString();
showSnackbar("Sending $a to $cId");
Nearby().sendBytesPayload(cId, Uint8List.fromList(a.codeUnits)); showSnackbar("Sending $a to ${value.endpointName}, id: $key");
Nearby().sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
});
}, },
), ),
RaisedButton( RaisedButton(
@ -254,12 +267,14 @@ class _MyBodyState extends State<Body> {
if (file == null) return; if (file == null) return;
int payloadId = await Nearby().sendFilePayload(cId, file.path); for(MapEntry<String, ConnectionInfo> m in endpointMap.entries){
showSnackbar("Sending file to $cId"); int payloadId = await Nearby().sendFilePayload(m.key, file.path);
showSnackbar("Sending file to ${m.key}");
Nearby().sendBytesPayload( Nearby().sendBytesPayload(
cId, m.key,
Uint8List.fromList( Uint8List.fromList(
"$payloadId:${file.path.split('/').last}".codeUnits)); "$payloadId:${file.path.split('/').last}".codeUnits));
}
}, },
), ),
], ],
@ -269,7 +284,7 @@ class _MyBodyState extends State<Body> {
} }
void showSnackbar(dynamic a) { void showSnackbar(dynamic a) {
Scaffold.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(a.toString()), content: Text(a.toString()),
)); ));
} }
@ -291,7 +306,9 @@ class _MyBodyState extends State<Body> {
child: Text("Accept Connection"), child: Text("Accept Connection"),
onPressed: () { onPressed: () {
Navigator.pop(context); Navigator.pop(context);
cId = id; setState(() {
endpointMap[id] = info;
});
Nearby().acceptConnection( Nearby().acceptConnection(
id, id,
onPayLoadRecieved: (endid, payload) async { onPayLoadRecieved: (endid, payload) async {
@ -310,7 +327,7 @@ class _MyBodyState extends State<Body> {
tempFile.rename( tempFile.rename(
tempFile.parent.path + "/" + fileName); tempFile.parent.path + "/" + fileName);
} else { } else {
showSnackbar("File doesnt exist"); showSnackbar("File doesn't exist");
} }
} else { } else {
//add to map if not already //add to map if not already
@ -333,7 +350,7 @@ class _MyBodyState extends State<Body> {
} else if (payloadTransferUpdate.status == } else if (payloadTransferUpdate.status ==
PayloadStatus.SUCCESS) { PayloadStatus.SUCCESS) {
showSnackbar( showSnackbar(
"success, total bytes = ${payloadTransferUpdate.totalBytes}"); "$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
if (map.containsKey(payloadTransferUpdate.id)) { if (map.containsKey(payloadTransferUpdate.id)) {
//rename the file now //rename the file now

View File

@ -7,87 +7,115 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.4.2" version: "2.5.0-nullsafety.3"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.1.0-nullsafety.3"
characters: characters:
dependency: transitive dependency: transitive
description: description:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.0" version: "1.1.0-nullsafety.5"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.3" version: "1.2.0-nullsafety.3"
clock: clock:
dependency: transitive dependency: transitive
description: description:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.1" version: "1.1.0-nullsafety.3"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.14.13" version: "1.15.0-nullsafety.5"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:
name: cupertino_icons name: cupertino_icons
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.2" version: "0.1.3"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" version: "1.2.0-nullsafety.3"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.11"
flutter_test: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
image_picker: image_picker:
dependency: "direct main" dependency: "direct main"
description: description:
name: image_picker name: image_picker
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.1+2" version: "0.6.7+21"
image_picker_platform_interface:
dependency: transitive
description:
name: image_picker_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.8" version: "0.12.10-nullsafety.3"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.8" version: "1.3.0-nullsafety.6"
nearby_connections: nearby_connections:
dependency: "direct dev" dependency: "direct dev"
description: description:
@ -101,7 +129,21 @@ packages:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.7.0" version: "1.8.0-nullsafety.3"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.2"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -113,56 +155,56 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.7.0" version: "1.8.0-nullsafety.4"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.9.5" version: "1.10.0-nullsafety.6"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.1.0-nullsafety.3"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.5" version: "1.1.0-nullsafety.3"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" version: "1.2.0-nullsafety.3"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.17" version: "0.2.19-nullsafety.6"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0" version: "1.3.0-nullsafety.5"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" version: "2.1.0-nullsafety.5"
sdks: sdks:
dart: ">=2.9.0-14.0.dev <3.0.0" dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0" flutter: ">=1.20.0 <2.0.0"

View File

@ -7,49 +7,49 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.4.2" version: "2.5.0-nullsafety.3"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.1.0-nullsafety.3"
characters: characters:
dependency: transitive dependency: transitive
description: description:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.0" version: "1.1.0-nullsafety.5"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.3" version: "1.2.0-nullsafety.3"
clock: clock:
dependency: transitive dependency: transitive
description: description:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.1" version: "1.1.0-nullsafety.3"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.14.13" version: "1.15.0-nullsafety.5"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" version: "1.2.0-nullsafety.3"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -66,21 +66,21 @@ packages:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.8" version: "0.12.10-nullsafety.3"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.8" version: "1.3.0-nullsafety.6"
path: path:
dependency: transitive dependency: transitive
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.7.0" version: "1.8.0-nullsafety.3"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -92,56 +92,56 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.7.0" version: "1.8.0-nullsafety.4"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.9.5" version: "1.10.0-nullsafety.6"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.1.0-nullsafety.3"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.5" version: "1.1.0-nullsafety.3"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" version: "1.2.0-nullsafety.3"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.17" version: "0.2.19-nullsafety.6"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0" version: "1.3.0-nullsafety.5"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" version: "2.1.0-nullsafety.5"
sdks: sdks:
dart: ">=2.9.0-14.0.dev <3.0.0" dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0" flutter: ">=1.20.0 <2.0.0"