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

The example app now will send data to all the connected endpoints.
multipeer_ios
afarre 3 years ago committed by GitHub
parent f35a484334
commit c2adf90962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,11 +35,11 @@ class Body extends StatefulWidget {
class _MyBodyState extends State<Body> {
final String userName = Random().nextInt(10000).toString();
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
Map<int, String> map =
Map(); //store filename mapped to corresponding payloadId
Map(); //store filename mapped to corresponding payloadId
@override
Widget build(BuildContext context) {
@ -62,7 +62,7 @@ class _MyBodyState extends State<Body> {
} else {
Scaffold.of(context).showSnackBar(SnackBar(
content:
Text("Location permissions not granted :(")));
Text("Location permissions not granted :(")));
}
},
),
@ -75,7 +75,7 @@ class _MyBodyState extends State<Body> {
} else {
Scaffold.of(context).showSnackBar(SnackBar(
content:
Text("Location permissions not granted :(")));
Text("Location permissions not granted :(")));
}
},
),
@ -85,7 +85,7 @@ class _MyBodyState extends State<Body> {
if (await Nearby().checkExternalStoragePermission()) {
Scaffold.of(context).showSnackBar(SnackBar(
content:
Text("External Storage permissions granted :)")));
Text("External Storage permissions granted :)")));
} else {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(
@ -126,7 +126,7 @@ class _MyBodyState extends State<Body> {
} else {
Scaffold.of(context).showSnackBar(SnackBar(
content:
Text("Enabling Location Service Failed :(")));
Text("Enabling Location Service Failed :(")));
}
},
),
@ -148,7 +148,10 @@ class _MyBodyState extends State<Body> {
showSnackbar(status);
},
onDisconnected: (id) {
showSnackbar("Disconnected: " + id);
showSnackbar("Disconnected: ${endpointMap[id].endpointName}, id $id");
setState(() {
endpointMap.remove(id);
});
},
);
showSnackbar("ADVERTISING: " + a.toString());
@ -199,7 +202,10 @@ class _MyBodyState extends State<Body> {
showSnackbar(status);
},
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) {
showSnackbar("Lost Endpoint:" + id);
showSnackbar("Lost discovered Endpoint: ${endpointMap[id].endpointName}, id $id");
},
);
showSnackbar("DISCOVERING: " + a.toString());
@ -228,10 +234,14 @@ class _MyBodyState extends State<Body> {
),
],
),
Text("Number of connected devices: ${endpointMap.length}"),
RaisedButton(
child: Text("Stop All Endpoints"),
onPressed: () async {
await Nearby().stopAllEndpoints();
setState(() {
endpointMap.clear();
});
},
),
Divider(),
@ -241,25 +251,30 @@ class _MyBodyState extends State<Body> {
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));
endpointMap.forEach((key, value) {
String a = Random().nextInt(100).toString();
showSnackbar("Sending $a to ${value.endpointName}, id: $key");
Nearby().sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
});
},
),
RaisedButton(
child: Text("Send File Payload"),
onPressed: () async {
File file =
await ImagePicker.pickImage(source: ImageSource.gallery);
await ImagePicker.pickImage(source: ImageSource.gallery);
if (file == null) return;
int payloadId = await Nearby().sendFilePayload(cId, file.path);
showSnackbar("Sending file to $cId");
Nearby().sendBytesPayload(
cId,
Uint8List.fromList(
"$payloadId:${file.path.split('/').last}".codeUnits));
for(MapEntry<String, ConnectionInfo> m in endpointMap.entries){
int payloadId = await Nearby().sendFilePayload(m.key, file.path);
showSnackbar("Sending file to ${m.key}");
Nearby().sendBytesPayload(
m.key,
Uint8List.fromList(
"$payloadId:${file.path.split('/').last}".codeUnits));
}
},
),
],
@ -269,7 +284,7 @@ class _MyBodyState extends State<Body> {
}
void showSnackbar(dynamic a) {
Scaffold.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(a.toString()),
));
}
@ -291,7 +306,9 @@ class _MyBodyState extends State<Body> {
child: Text("Accept Connection"),
onPressed: () {
Navigator.pop(context);
cId = id;
setState(() {
endpointMap[id] = info;
});
Nearby().acceptConnection(
id,
onPayLoadRecieved: (endid, payload) async {
@ -310,7 +327,7 @@ class _MyBodyState extends State<Body> {
tempFile.rename(
tempFile.parent.path + "/" + fileName);
} else {
showSnackbar("File doesnt exist");
showSnackbar("File doesn't exist");
}
} else {
//add to map if not already
@ -333,7 +350,7 @@ class _MyBodyState extends State<Body> {
} else if (payloadTransferUpdate.status ==
PayloadStatus.SUCCESS) {
showSnackbar(
"success, total bytes = ${payloadTransferUpdate.totalBytes}");
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
if (map.containsKey(payloadTransferUpdate.id)) {
//rename the file now

@ -7,87 +7,115 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.2"
version: "2.5.0-nullsafety.3"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.3"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.1.0-nullsafety.5"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
version: "1.2.0-nullsafety.3"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "1.1.0-nullsafety.3"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.13"
version: "1.15.0-nullsafety.5"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2"
version: "0.1.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0-nullsafety.3"
flutter:
dependency: "direct main"
description: flutter
source: sdk
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:
dependency: "direct dev"
description: flutter
source: sdk
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:
dependency: "direct main"
description:
name: image_picker
url: "https://pub.dartlang.org"
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:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.8"
version: "0.12.10-nullsafety.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
version: "1.3.0-nullsafety.6"
nearby_connections:
dependency: "direct dev"
description:
@ -101,7 +129,21 @@ packages:
name: path
url: "https://pub.dartlang.org"
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:
dependency: transitive
description: flutter
@ -113,56 +155,56 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0-nullsafety.4"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.5"
version: "1.10.0-nullsafety.6"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.3"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
version: "1.1.0-nullsafety.3"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0-nullsafety.3"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.17"
version: "0.2.19-nullsafety.6"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0-nullsafety.5"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.1.0-nullsafety.5"
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"

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

Loading…
Cancel
Save